Manager.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\Core\Queue;
  3. use ModulesGarden\ProxmoxAddon\Core\DependencyInjection\DependencyInjection;
  4. use ModulesGarden\ProxmoxAddon\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\ProxmoxAddon\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. logModuleCall(
  58. 'ProxmoxAddon',
  59. __FUNCTION__,
  60. [$instance, $method],
  61. 'debug1',
  62. unserialize($data)
  63. );
  64. return call_user_func_array([$instance, $method], unserialize($data));
  65. }
  66. /**
  67. * @param $class
  68. * @return mixed
  69. */
  70. protected function resolve($class)
  71. {
  72. return new $class($this->job, $this->log);
  73. }
  74. /**
  75. * Parse the job declaration into class and method.
  76. *
  77. * @param string $job
  78. * @return array
  79. */
  80. protected function parseJob($job)
  81. {
  82. $segments = explode('@', $job);
  83. return count($segments) > 1 ? $segments : array($segments[0], 'fire');
  84. }
  85. }