Manager.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. $message = $ex->getMessage();
  34. if (strpos($message,"snapshot") !== false && strpos($message,"already") !== false && strpos($message,"used") !== false) {
  35. $this->job->setFinished();
  36. //add log message
  37. $this->log->error($ex->getMessage(), debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));
  38. } else {
  39. //Set error in job
  40. $this->job->setError();
  41. $this->job->setRetryAfter(date('Y-m-d H:i:s', strtotime('+ 60 seconds')));
  42. $this->job->increaseRetryCount();
  43. //add log message
  44. $this->log->error($ex->getMessage(), debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));
  45. }
  46. }
  47. }
  48. /**
  49. * @param $job
  50. * @param $data
  51. * @return mixed
  52. */
  53. protected function resolveAndFire($job, $data) {
  54. list($class, $method) = $this->parseJob($job);
  55. $instance = $this->resolve($class);
  56. return call_user_func_array([$instance, $method], unserialize($data));
  57. }
  58. /**
  59. * @param $class
  60. * @return mixed
  61. */
  62. protected function resolve($class) {
  63. return new $class($this->job, $this->log);
  64. }
  65. /**
  66. * Parse the job declaration into class and method.
  67. *
  68. * @param string $job
  69. * @return array
  70. */
  71. protected function parseJob($job) {
  72. $segments = explode('@', $job);
  73. return count($segments) > 1 ? $segments : array($segments[0], 'fire');
  74. }
  75. }