Queue.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace core\util;
  12. use Exception;
  13. use think\cache\driver\Redis;
  14. use think\facade\Cache;
  15. use think\facade\Log;
  16. use think\facade\Queue as ThinkQueue;
  17. /**
  18. * Class Queue
  19. * @package core\util
  20. * @method $this method(string $method) 设置任务执行方法
  21. * @method $this job(string $job) 设置任务执行类名
  22. * @method $this errorCount(int $error_count) 执行失败次数
  23. * @method $this data(...$data) 执行数据
  24. * @method $this secs(int $secs) 延迟执行秒数
  25. */
  26. class Queue
  27. {
  28. /**
  29. * 错误信息
  30. * @var string
  31. */
  32. protected $error;
  33. /**
  34. * 任务执行方法
  35. * @var string
  36. */
  37. protected $method = 'doJob';
  38. /**
  39. * 默认任务执行方法名
  40. * @var string
  41. */
  42. protected $default_method;
  43. /**
  44. * 任务类名
  45. * @var string
  46. */
  47. protected $job;
  48. /**
  49. * 数据
  50. * @var array|string
  51. */
  52. protected $data;
  53. /**
  54. * 队列名称
  55. * @var null
  56. */
  57. protected $queue_name = null;
  58. /**
  59. * 延迟执行秒数
  60. * @var int
  61. */
  62. protected $secs = 0;
  63. /**
  64. * 允许的方法或属性
  65. * @var array
  66. */
  67. protected $allow_function = ['method', 'data', 'error_count', 'job', 'secs'];
  68. /**
  69. * 当前实例
  70. * @var static
  71. */
  72. protected static $instance;
  73. protected function __construct()
  74. {
  75. $this->default_method = $this->method;
  76. }
  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 string $queue_name
  91. * @return $this
  92. */
  93. public function setQueueName(string $queue_name)
  94. {
  95. $this->queue_name = $queue_name;
  96. return $this;
  97. }
  98. /**
  99. * 加入队列
  100. * @param array|null $data
  101. * @return bool
  102. */
  103. public function push()
  104. {
  105. if (!$this->job) {
  106. return $this->setError('JOB_NOT_EXISTS');
  107. }
  108. $jodValue = $this->getValues();
  109. $res = $this->send(...$jodValue);
  110. if (!$res) {
  111. $res = $this->send(...$jodValue);
  112. if (!$res) {
  113. Log::error('队列推送失败,参数:' . json_encode($jodValue, JSON_THROW_ON_ERROR));
  114. }
  115. }
  116. // //todo 队列扩展策略调度,
  117. $this->clean();
  118. return $res;
  119. }
  120. /**
  121. * 向队列发送一条消息
  122. * @param $queue
  123. * @param $data
  124. * @param $delay
  125. * @return mixed
  126. */
  127. public function send($queue, $data, $delay = 0)
  128. {
  129. // $redis = Cache::store('redis')->handler();
  130. $pre_queue = md5(root_path()); //1.0.5版本之前为redis-queue
  131. $queue_waiting = $pre_queue.'{redis-queue}-waiting'; //1.0.5版本之前为redis-queue-waiting
  132. $queue_delay = $pre_queue.'{redis-queue}-delayed';//1.0.5版本之前为redis-queue-delayed
  133. $now = time();
  134. if (extension_loaded('redis')) {
  135. try {
  136. $redis = new \Redis();
  137. $redis->connect(env('redis.redis_hostname'), env('redis.port'), 8);
  138. if (env('redis.redis_password', '')) {
  139. $redis->auth(env('redis.redis_password', ''));
  140. }
  141. $redis->select(env('redis.select'));
  142. if(!$redis->ping()){
  143. $redis->connect(env('redis.redis_hostname'), env('redis.port'), 8);
  144. if (env('redis.redis_password', '')) {
  145. $redis->auth(env('redis.redis_password', ''));
  146. }
  147. $redis->select(env('redis.select'));
  148. }
  149. $package_str = json_encode([
  150. 'id' => rand(),
  151. 'time' => $now,
  152. 'delay' => $delay,
  153. 'attempts' => 0,
  154. 'queue' => $queue,
  155. 'data' => $data
  156. ]);
  157. if ($delay) {
  158. if(!$redis->zAdd($queue_delay, ($now + $delay), $package_str)){
  159. $res = $redis->zAdd($queue_delay, ($now + $delay), $package_str);
  160. }
  161. return true;
  162. }
  163. if(!$redis->lPush($queue_waiting . $queue, $package_str)){
  164. $res = $redis->lPush($queue_waiting . $queue, $package_str);
  165. Log::write($res);
  166. }
  167. return true;
  168. } catch ( Throwable $e ) {
  169. return false;
  170. }
  171. }else{
  172. return false;
  173. }
  174. }
  175. /**
  176. * 清除数据
  177. */
  178. public function clean()
  179. {
  180. $this->secs = 0;
  181. $this->data = [];
  182. $this->queue_name = null;
  183. $this->method = $this->default_method;
  184. }
  185. /**
  186. * 获取参数
  187. * @param $data
  188. * @return array
  189. */
  190. protected function getValues()
  191. {
  192. return [$this->job, ['method' => $this->method, 'data' => $this->data], $this->secs];
  193. }
  194. /**
  195. * 不可访问时调用
  196. * @param $method
  197. * @param $arguments
  198. * @return $this
  199. * @throws Exception
  200. * @throws Exception
  201. * @throws Exception
  202. */
  203. public function __call($method, $arguments)
  204. {
  205. if (in_array($method, $this->allow_function)) {
  206. if ($method === 'data') {
  207. $this->{$method} = $arguments;
  208. } else {
  209. $this->{$method} = $arguments[0] ?? null;
  210. }
  211. return $this;
  212. } else {
  213. throw new Exception('Method does not exist' . __CLASS__ . '->' . $method . '()');
  214. }
  215. }
  216. /**
  217. * 设置错误信息
  218. * @param string|null $error
  219. * @return bool
  220. */
  221. protected function setError(?string $error = null)
  222. {
  223. $this->error = $error;
  224. return false;
  225. }
  226. /**
  227. * 获取错误信息
  228. * @return string
  229. */
  230. public function getError()
  231. {
  232. $error = $this->error;
  233. $this->error = null;
  234. return $error;
  235. }
  236. }