Queue.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace fec\component\redisqueue;
  3. use Yii;
  4. use yii\base\Component;
  5. abstract class Queue extends Component
  6. {
  7. /**
  8. * @var string Queue prefix
  9. */
  10. public $queuePrefix = 'queue';
  11. /** @var bool Debug mode */
  12. public $debug = false;
  13. /**
  14. * Builds queue prefix
  15. *
  16. * @param string|null $name Queue name
  17. * @return string
  18. */
  19. public function buildPrefix($name = null)
  20. {
  21. if (empty($name)) {
  22. $name = 'default';
  23. } elseif ($name && preg_match('/[^[:alnum:]]/', $name)) {
  24. $name = md5($name);
  25. }
  26. return $this->queuePrefix . ':' . $name;
  27. }
  28. /**
  29. * Push job to the queue
  30. *
  31. * @param string $job Fully qualified class name of the job
  32. * @param mixed $data Data for the job
  33. * @param string|null $queue Queue name
  34. * @return string ID of the job
  35. */
  36. public function push($job, $data = null, $queue = null, $options = [])
  37. {
  38. return $this->pushInternal($this->createPayload($job, $data), $queue, $options);
  39. }
  40. /**
  41. * Get job from the queue
  42. *
  43. * @param string|null $queue Queue name
  44. * @return mixed
  45. */
  46. public function pop($queue = null)
  47. {
  48. return $this->popInternal($queue);
  49. }
  50. /**
  51. * Create job array
  52. *
  53. * @param string $job Fully qualified class name of the job
  54. * @param mixed $data Data for the job
  55. * @return array
  56. */
  57. protected function createPayload($job, $data)
  58. {
  59. $payload = [
  60. 'job' => $job,
  61. 'data' => $data
  62. ];
  63. $payload = $this->setMeta($payload, 'id', $this->getRandomId());
  64. return $payload;
  65. }
  66. /**
  67. * Set additional meta on a payload string.
  68. *
  69. * @param string $payload
  70. * @param string $key
  71. * @param string $value
  72. * @return string
  73. */
  74. protected function setMeta($payload, $key, $value)
  75. {
  76. $payload[$key] = $value;
  77. return json_encode($payload);
  78. }
  79. /**
  80. * Get random ID.
  81. *
  82. * @return string
  83. */
  84. protected function getRandomId()
  85. {
  86. return Yii::$app->security->generateRandomString();
  87. }
  88. /**
  89. * Get prefixed queue name
  90. *
  91. * @param $queue Queue name
  92. * @return string
  93. */
  94. protected function getQueue($queue)
  95. {
  96. return $this->buildPrefix($queue);
  97. }
  98. /**
  99. * Class-specific realisation of adding the job to the queue
  100. *
  101. * @param array $payload Job data
  102. * @param string|null $queue Queue name
  103. * @param array $options
  104. *
  105. * @return mixed
  106. */
  107. abstract protected function pushInternal($payload, $queue = null, $options = []);
  108. /**
  109. * Class-specific realisation of getting the job to the queue
  110. *
  111. * @param string|null $queue Queue name
  112. *
  113. * @return mixed
  114. */
  115. abstract protected function popInternal($queue = null);
  116. }