Manager.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Core\Queue;
  3. use ThurData\Servers\KerioEmail\Core\DependencyInjection\DependencyInjection;
  4. use ThurData\Servers\KerioEmail\Core\Queue\Services\Log;
  5. class Manager
  6. {
  7. /**
  8. * @var Models\Job
  9. */
  10. protected $job;
  11. /**
  12. * @var Log
  13. */
  14. protected $log;
  15. public function __construct(\ThurData\Servers\KerioEmail\Core\Queue\Models\Job $job)
  16. {
  17. $this->job = $job;
  18. $this->log = new Log($this->job);
  19. }
  20. public function fire()
  21. {
  22. try
  23. {
  24. $this->job->setRunning();
  25. $ret = $this->resolveAndFire($this->job->job, $this->job->data);
  26. if($ret !== false)
  27. {
  28. $this->job->setFinished();
  29. }
  30. }
  31. catch(\Exception $ex)
  32. {
  33. //Set error in job
  34. $this->job->setError();
  35. $this->job->setRetryAfter(date('Y-m-d H:i:s', strtotime('+ 60 seconds')));
  36. $this->job->increaseRetryCount();
  37. //add log message
  38. $this->log->error($ex->getMessage(), debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));
  39. }
  40. }
  41. protected function resolveAndFire($job, $data)
  42. {
  43. list($class, $method) = $this->parseJob($job);
  44. $instance = $this->resolve($class);
  45. if(method_exists($instance, 'setJobModel'))
  46. {
  47. $instance->setJobModel($this->job);
  48. }
  49. return call_user_func_array([$instance, $method], unserialize($data));
  50. }
  51. /**
  52. * Resolve the given job handler.
  53. *
  54. * @param string $class
  55. * @return mixed
  56. */
  57. protected function resolve($class)
  58. {
  59. return DependencyInjection::create($class);
  60. }
  61. /**
  62. * Parse the job declaration into class and method.
  63. *
  64. * @param string $job
  65. * @return array
  66. */
  67. protected function parseJob($job)
  68. {
  69. $segments = explode('@', $job);
  70. return count($segments) > 1 ? $segments : array($segments[0], 'fire');
  71. }
  72. }