Queue.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace think\admin;
  15. use think\admin\extend\CodeExtend;
  16. use think\admin\service\ProcessService;
  17. use think\App;
  18. /**
  19. * 基础任务基类
  20. * Class Queue
  21. * @package think\admin
  22. */
  23. class Queue
  24. {
  25. /**
  26. * 应用实例
  27. * @var App
  28. */
  29. protected $app;
  30. /**
  31. * 当前任务编号
  32. * @var integer
  33. */
  34. protected $code = 0;
  35. /**
  36. * 当前任务标题
  37. * @var string
  38. */
  39. protected $title = '';
  40. /**
  41. * 当前任务参数
  42. * @var array
  43. */
  44. protected $data = [];
  45. /**
  46. * 当前任务数据
  47. * @var array
  48. */
  49. protected $queue = [];
  50. /**
  51. * Queue constructor.
  52. * @param App $app
  53. * @param int $code
  54. * @throws \think\Exception
  55. * @throws \think\db\exception\DataNotFoundException
  56. * @throws \think\db\exception\DbException
  57. * @throws \think\db\exception\ModelNotFoundException
  58. */
  59. public function __construct(App $app, $code = 0)
  60. {
  61. $this->app = $app;
  62. if ($code > 0) $this->init($code);
  63. }
  64. /**
  65. * 静态获取实例
  66. * @param App $app
  67. * @param int $code
  68. * @return static
  69. * @throws \think\Exception
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\DbException
  72. * @throws \think\db\exception\ModelNotFoundException
  73. */
  74. public static function instance(App $app, $code = 0)
  75. {
  76. return new static($app, $code);
  77. }
  78. /**
  79. * 数据初始化
  80. * @param integer $code
  81. * @return Queue
  82. * @throws \think\Exception
  83. * @throws \think\db\exception\DataNotFoundException
  84. * @throws \think\db\exception\DbException
  85. * @throws \think\db\exception\ModelNotFoundException
  86. */
  87. public function init($code = 0)
  88. {
  89. if ($code > 0) {
  90. $this->queue = $this->app->db->name('SystemQueue')->where(['code' => $this->code])->find();
  91. if (empty($this->queue)) throw new \think\Exception("Queue {$code} Not found.");
  92. $this->code = $this->queue['code'];
  93. $this->title = $this->queue['title'];
  94. $this->data = json_decode($this->queue['exec_data'], true) ?: [];
  95. }
  96. return $this;
  97. }
  98. /**
  99. * 判断是否WIN环境
  100. * @return boolean
  101. */
  102. protected function iswin()
  103. {
  104. return ProcessService::instance()->iswin();
  105. }
  106. /**
  107. * 重发异步任务
  108. * @param integer $wait 等待时间
  109. * @return Queue
  110. * @throws \think\Exception
  111. * @throws \think\db\exception\DataNotFoundException
  112. * @throws \think\db\exception\DbException
  113. * @throws \think\db\exception\ModelNotFoundException
  114. */
  115. public function reset($wait = 0)
  116. {
  117. if (empty($this->queue)) throw new \think\Exception('Queue data cannot be empty!');
  118. $this->app->db->name('SystemQueue')->where(['code' => $this->code])->failException(true)->update([
  119. 'exec_time' => time() + $wait, 'attempts' => $this->queue['attempts'] + 1, 'status' => '1',
  120. ]);
  121. return $this->init($this->code);
  122. }
  123. /**
  124. * 注册异步处理任务
  125. * @param string $title 任务名称
  126. * @param string $command 执行内容
  127. * @param integer $later 延时执行时间
  128. * @param array $data 任务附加数据
  129. * @param integer $rscript 任务多开
  130. * @return Queue
  131. * @throws \think\Exception
  132. * @throws \think\db\exception\DataNotFoundException
  133. * @throws \think\db\exception\DbException
  134. * @throws \think\db\exception\ModelNotFoundException
  135. */
  136. public function register($title, $command, $later = 0, $data = [], $rscript = 1)
  137. {
  138. $map = [['title', '=', $title], ['status', 'in', ['1', '2']]];
  139. if (empty($rscript) && $this->app->db->name('SystemQueue')->where($map)->count() > 0) {
  140. throw new \think\Exception('该任务已经创建,请耐心等待处理完成!');
  141. }
  142. $this->code = CodeExtend::uniqidDate(16);
  143. $this->app->db->name('SystemQueue')->failException(true)->insert([
  144. 'code' => $this->code,
  145. 'title' => $title,
  146. 'command' => $command,
  147. 'attempts' => '0',
  148. 'rscript' => intval(boolval($rscript)),
  149. 'exec_data' => json_encode($data, JSON_UNESCAPED_UNICODE),
  150. 'exec_time' => $later > 0 ? time() + $later : time(),
  151. 'enter_time' => '0',
  152. 'outer_time' => '0',
  153. ]);
  154. return $this->init($this->code);
  155. }
  156. /**
  157. * 执行任务处理
  158. * @param array $data 任务参数
  159. * @return mixed
  160. */
  161. public function execute(array $data = [])
  162. {
  163. }
  164. }