Manager.php 2.0 KB

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