DatabaseQueue.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxVps\Core\Queue;
  3. class DatabaseQueue implements QueueInterface
  4. {
  5. /**
  6. * @var int
  7. */
  8. protected $retryLimit = 100;
  9. /**
  10. * @param $job
  11. * @param string $data
  12. * @param int $parentId
  13. * @param null $relType
  14. * @param null $relId
  15. * @param null $customId
  16. */
  17. public function push($job, $data = '', $parentId = null, $relType = null, $relId = null, $customId = null)
  18. {
  19. $model = new \ModulesGarden\Servers\ProxmoxVps\Core\Queue\Models\Job();
  20. $model->job = $job;
  21. $model->data = $data;
  22. $model->parent_id = $parentId;
  23. $model->rel_type = $relType;
  24. $model->rel_id = $relId;
  25. $model->custom_id = $customId;
  26. $model->save();
  27. return $model;
  28. }
  29. public function pushRaw($payload, $queue = null, array $options = [])
  30. {
  31. }
  32. public function later($delay, $job, $data = '', $queue = null)
  33. {
  34. }
  35. public function pushOn($queue, $job, $data = '')
  36. {
  37. }
  38. public function laterOn($queue, $delay, $job, $data = '')
  39. {
  40. }
  41. /**
  42. * @return mixed
  43. */
  44. public function pop()
  45. {
  46. return $this->query()->first();
  47. }
  48. /**
  49. * @return mixed
  50. */
  51. public function count()
  52. {
  53. return $this->query()->count();
  54. }
  55. /**
  56. * @return \ModulesGarden\Servers\ProxmoxVps\Core\Queue\Models\Job
  57. */
  58. protected function query()
  59. {
  60. $table = (new\ModulesGarden\Servers\ProxmoxVps\Core\Queue\Models\Job)->getTable();
  61. $query = \ModulesGarden\Servers\ProxmoxVps\Core\Queue\Models\Job
  62. ::select("{$table}.*")
  63. ->leftJoin($table.' as parent', function($join) use($table) {
  64. $join->on($table.'.parent_id', '=', 'parent.id');
  65. })
  66. ->where(function($query) use($table){
  67. $query->where("{$table}.status", '=', '')
  68. ->orWhere(function($query) use($table){
  69. $query->where("{$table}.status", '=', \ModulesGarden\Servers\ProxmoxVps\Core\Queue\Models\Job::STATUS_ERROR);
  70. $query->whereRaw("{$table}.retry_after < NOW()");
  71. $query->where("{$table}.retry_count", '<', $this->retryLimit);
  72. })
  73. ->orWhere(function($query) use($table){
  74. $query->where("{$table}.status", '=', \ModulesGarden\Servers\ProxmoxVps\Core\Queue\Models\Job::STATUS_WAITING);
  75. $query->whereRaw("{$table}.retry_after < NOW()");
  76. $query->where("{$table}.retry_count", '<', $this->retryLimit);
  77. });
  78. })
  79. ->where(function($query) use($table){
  80. $query->whereRaw("{$table}.parent_id IS NULL");
  81. $query->orWhere("parent.status", \ModulesGarden\Servers\ProxmoxVps\Core\Queue\Models\Job::STATUS_FINISHED);
  82. });
  83. return $query;
  84. }
  85. }