Queue.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * @author: liaofei<136327134@qq.com>
  4. * @day: 2020/5/28
  5. */
  6. namespace crmeb\utils;
  7. use crmeb\traits\ErrorTrait;
  8. use think\facade\Config;
  9. use think\facade\Queue as QueueThink;
  10. /**
  11. * Class Queue
  12. * @package crmeb\utils
  13. * @method $this do(string $do) 设置任务执行方法
  14. * @method $this job(string $job) 设置任务执行类名
  15. * @method $this errorCount(int $errorCount) 执行失败次数
  16. * @method $this data(...$data) 执行数据
  17. * @method $this secs(int $secs) 延迟执行秒数
  18. * @method $this log($log) 记录日志
  19. */
  20. class Queue
  21. {
  22. use ErrorTrait;
  23. /**
  24. * 任务执行
  25. * @var string
  26. */
  27. protected $do = 'doJob';
  28. /**
  29. * 默认任务执行方法名
  30. * @var string
  31. */
  32. protected $defaultDo;
  33. /**
  34. * 任务类名
  35. * @var string
  36. */
  37. protected $job;
  38. /**
  39. * 错误次数
  40. * @var int
  41. */
  42. protected $errorCount = 3;
  43. /**
  44. * 数据
  45. * @var array|string
  46. */
  47. protected $data;
  48. /**
  49. * 任务名
  50. * @var null
  51. */
  52. protected $queueName = null;
  53. /**
  54. * 延迟执行秒数
  55. * @var int
  56. */
  57. protected $secs = 0;
  58. /**
  59. * 记录日志
  60. * @var string|callable|array
  61. */
  62. protected $log;
  63. /**
  64. * @var array
  65. */
  66. protected $rules = ['do', 'data', 'errorCount', 'job', 'secs', 'log'];
  67. /**
  68. * @var static
  69. */
  70. protected static $instance;
  71. /**
  72. * Queue constructor.
  73. */
  74. protected function __construct()
  75. {
  76. $this->defaultDo = $this->do;
  77. }
  78. /**
  79. * @return static
  80. */
  81. public static function instance()
  82. {
  83. if (is_null(self::$instance)) {
  84. self::$instance = new static();
  85. }
  86. return self::$instance;
  87. }
  88. /**
  89. * 放入消息队列
  90. * @param array|null $data
  91. * @return mixed
  92. */
  93. public function push(?array $data = null)
  94. {
  95. if (!$this->job) {
  96. return $this->setError('需要执行的队列类必须存在');
  97. }
  98. $res = QueueThink::{$this->action()}(...$this->getValues($data));
  99. $this->clean();
  100. return $res;
  101. }
  102. /**
  103. * 清除数据
  104. */
  105. public function clean()
  106. {
  107. $this->secs = 0;
  108. $this->data = [];
  109. $this->log = null;
  110. $this->queueName = null;
  111. $this->errorCount = 3;
  112. $this->do = $this->defaultDo;
  113. }
  114. /**
  115. * 获取任务方式
  116. * @return string
  117. */
  118. protected function action()
  119. {
  120. return $this->secs ? 'later' : 'push';
  121. }
  122. /**
  123. * 获取参数
  124. * @param $data
  125. * @return array
  126. */
  127. protected function getValues($data)
  128. {
  129. $jobData['data'] = $data ?: $this->data;
  130. $jobData['do'] = $this->do;
  131. $jobData['errorCount'] = $this->errorCount;
  132. $jobData['log'] = $this->log;
  133. if ($this->do != $this->defaultDo) {
  134. $this->job .= '@' . Config::get('queue.prefix', 'eb_') . $this->do;
  135. }
  136. if ($this->secs) {
  137. return [$this->secs, $this->job, $jobData, $this->queueName];
  138. } else {
  139. return [$this->job, $jobData, $this->queueName];
  140. }
  141. }
  142. /**
  143. * @param $name
  144. * @param $arguments
  145. * @return $this
  146. */
  147. public function __call($name, $arguments)
  148. {
  149. if (in_array($name, $this->rules)) {
  150. if ($name === 'data') {
  151. $this->{$name} = $arguments;
  152. } else {
  153. $this->{$name} = $arguments[0] ?? null;
  154. }
  155. return $this;
  156. } else {
  157. throw new \RuntimeException('Method does not exist' . __CLASS__ . '->' . $name . '()');
  158. }
  159. }
  160. }