Manager.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. * @var Models\Job
  8. */
  9. protected $job;
  10. /**
  11. * @var Log
  12. */
  13. protected $log;
  14. /**
  15. * Manager constructor.
  16. * @param Models\Job $job
  17. */
  18. public function __construct(\ModulesGarden\ProxmoxAddon\Core\Queue\Models\Job $job) {
  19. $this->job = $job;
  20. $this->log = new Log($this->job);
  21. }
  22. /**
  23. * snapshot name 'Snapshot_320_08_30_02' already used
  24. */
  25. public function fire(){
  26. try {
  27. $this->job->setRunning();
  28. $ret = $this->resolveAndFire($this->job->job, $this->job->data);
  29. if($ret !== false) {
  30. $this->job->setFinished();
  31. }
  32. } catch(\Exception $ex) {
  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. /**
  42. * @param $job
  43. * @param $data
  44. * @return mixed
  45. */
  46. protected function resolveAndFire($job, $data) {
  47. list($class, $method) = $this->parseJob($job);
  48. $instance = $this->resolve($class);
  49. return call_user_func_array([$instance, $method], unserialize($data));
  50. }
  51. /**
  52. * @param $class
  53. * @return mixed
  54. */
  55. protected function resolve($class) {
  56. return new $class($this->job, $this->log);
  57. }
  58. /**
  59. * Parse the job declaration into class and method.
  60. *
  61. * @param string $job
  62. * @return array
  63. */
  64. protected function parseJob($job) {
  65. $segments = explode('@', $job);
  66. return count($segments) > 1 ? $segments : array($segments[0], 'fire');
  67. }
  68. }