Guest User

Untitled

a guest
Jul 20th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. <?php
  2.  
  3. class TO_LocalJobDispatcher implements TO_IJobDispatcher
  4. {
  5. const START_JOB_CLI_COMMAND = 'AGAVI_ENVIRONMENT=%1$s setsid %2$s %3$s job_queue.execute --job %4$d &> /dev/null & echo $!';
  6.  
  7. const KILL_JOB_CLI_COMMAND = 'kill %1$s';
  8.  
  9. const ENDPOINT_NOTIFICATION_ROUTE = 'tonline.job_handler.notify';
  10.  
  11. public function dispatchProcessRequest(TO_BaseJob $job, $action)
  12. {
  13. if ($action === TO_IJobDispatcher::ACTION_EXECUTE)
  14. {
  15. return $this->executeJob($job);
  16. }
  17.  
  18. return $this->cancelJob($job);
  19. }
  20.  
  21. public function dispatchEndPointNotification(TO_BaseJob $job, $event)
  22. {
  23. /**
  24. * Create forward container here. Don't need to call an url
  25. * and bootstrap from the scratch.
  26. */
  27. $url = $this->getContext()->getRouting()->gen(
  28. self::ENDPOINT_NOTIFICATION_ROUTE,
  29. array(
  30. $job->getId(),
  31. $event
  32. )
  33. );
  34. }
  35.  
  36. protected function cancelJob(TO_IJob $job)
  37. {
  38. $output = array();
  39.  
  40. $command = sprintf(
  41. self::KILL_JOB_CLI_COMMAND,
  42. $job_execution->getJobPid()
  43. );
  44.  
  45. exec($command, $output);
  46.  
  47. /*
  48. * @todo If output>1 we are dealing with a possible error.
  49. */
  50. return true;
  51. }
  52.  
  53. protected function executeJob(TO_IJob $job)
  54. {
  55. $output = array();
  56.  
  57. exec(
  58. $this->buildExecuteJobCliCommandString($job),
  59. $output
  60. );
  61.  
  62. /*
  63. * @todo If output>1 we are dealing with a possible error.
  64. */
  65. return (int) $output[0];
  66. }
  67.  
  68. protected function buildExecuteJobCliCommandString(TO_BaseJob $job)
  69. {
  70. $php_cli_path = AgaviConfig::get("exo_cms.cli_path");
  71.  
  72. $agavi_cli_entry_point = dirname(
  73. AgaviConfig::get('core.app_dir')
  74. ).DIRECTORY_SEPARATOR."bin".DIRECTORY_SEPARATOR."cli.php";
  75.  
  76. return sprintf(
  77. self::START_JOB_CLI_COMMAND,
  78. AgaviConfig::get('core.environment'),
  79. $php_cli_path,
  80. $agavi_cli_entry_point,
  81. $job->getId()
  82. );
  83. }
  84. }
  85.  
  86. ?>
Add Comment
Please, Sign In to add comment