Queue.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\utils;
  12. use crmeb\traits\ErrorTrait;
  13. use think\exception\ValidateException;
  14. use think\facade\Config;
  15. use think\facade\Queue as QueueThink;
  16. use think\facade\Log;
  17. /**
  18. * Class Queue
  19. * @package crmeb\utils
  20. * @method $this do(string $do) 设置任务执行方法
  21. * @method $this job(string $job) 设置任务执行类名
  22. * @method $this errorCount(int $errorCount) 执行失败次数
  23. * @method $this data(...$data) 执行数据
  24. * @method $this secs(int $secs) 延迟执行秒数
  25. * @method $this log($log) 记录日志
  26. */
  27. class Queue
  28. {
  29. use ErrorTrait;
  30. /**
  31. * 任务执行
  32. * @var string
  33. */
  34. protected $do = 'doJob';
  35. /**
  36. * 默认任务执行方法名
  37. * @var string
  38. */
  39. protected $defaultDo;
  40. /**
  41. * 任务类名
  42. * @var string
  43. */
  44. protected $job;
  45. /**
  46. * 错误次数
  47. * @var int
  48. */
  49. protected $errorCount = 3;
  50. /**
  51. * 数据
  52. * @var array|string
  53. */
  54. protected $data;
  55. /**
  56. * 队列名
  57. * @var null
  58. */
  59. protected $queueName = null;
  60. /**
  61. * 延迟执行秒数
  62. * @var int
  63. */
  64. protected $secs = 0;
  65. /**
  66. * 记录日志
  67. * @var string|callable|array
  68. */
  69. protected $log;
  70. /**
  71. * @var array
  72. */
  73. protected $rules = ['do', 'data', 'errorCount', 'job', 'secs', 'log'];
  74. /**
  75. * @var static
  76. */
  77. protected static $instance;
  78. /**
  79. * Queue constructor.
  80. */
  81. protected function __construct()
  82. {
  83. $this->defaultDo = $this->do;
  84. }
  85. /**
  86. * @return static
  87. */
  88. public static function instance()
  89. {
  90. if (is_null(self::$instance)) {
  91. self::$instance = new static();
  92. }
  93. return self::$instance;
  94. }
  95. /**
  96. * 设置列名
  97. * @param string $queueName
  98. * @return $this
  99. */
  100. public function setQueueName(string $queueName)
  101. {
  102. $this->queueName = $queueName;
  103. return $this;
  104. }
  105. /**
  106. * 放入消息队列
  107. * @param array|null $data
  108. * @return mixed
  109. */
  110. public function push(?array $data = null)
  111. {
  112. if (!$this->job) {
  113. return $this->setError('需要执行的队列类必须存在');
  114. }
  115. $jodValue = $this->getValues($data);
  116. $res = QueueThink::{$this->action()}(...$jodValue);
  117. if (!$res) {
  118. $res = QueueThink::{$this->action()}(...$jodValue);
  119. if (!$res) {
  120. Log::error('加入队列失败,参数:' . json_encode($jodValue));
  121. }
  122. }
  123. $this->clean();
  124. return $res;
  125. }
  126. /**
  127. * 清除数据
  128. */
  129. public function clean()
  130. {
  131. $this->secs = 0;
  132. $this->data = [];
  133. $this->log = null;
  134. $this->queueName = null;
  135. $this->errorCount = 3;
  136. $this->do = $this->defaultDo;
  137. }
  138. /**
  139. * 获取任务方式
  140. * @return string
  141. */
  142. protected function action()
  143. {
  144. return $this->secs ? 'later' : 'push';
  145. }
  146. /**
  147. * 获取参数
  148. * @param $data
  149. * @return array
  150. */
  151. protected function getValues($data)
  152. {
  153. $jobData['data'] = $data ?: $this->data;
  154. $jobData['do'] = $this->do;
  155. $jobData['errorCount'] = $this->errorCount;
  156. $jobData['log'] = $this->log;
  157. if (!class_exists($this->job)) {
  158. throw new ValidateException('需要执行的队列类不存在');
  159. }
  160. if ($this->do != $this->defaultDo) {
  161. $this->job .= '@' . Config::get('queue.prefix', 'eb_') . $this->do;
  162. }
  163. if ($this->secs) {
  164. return [$this->secs, $this->job, $jobData, $this->queueName];
  165. } else {
  166. return [$this->job, $jobData, $this->queueName];
  167. }
  168. }
  169. /**
  170. * @param $name
  171. * @param $arguments
  172. * @return $this
  173. */
  174. public function __call($name, $arguments)
  175. {
  176. if (in_array($name, $this->rules)) {
  177. if ($name === 'data') {
  178. $this->{$name} = $arguments;
  179. } else {
  180. $this->{$name} = $arguments[0] ?? null;
  181. }
  182. return $this;
  183. } else {
  184. throw new \RuntimeException('Method does not exist' . __CLASS__ . '->' . $name . '()');
  185. }
  186. }
  187. }