Queue.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 app\admin\queue;
  15. use think\console\Input;
  16. use think\console\Output;
  17. use think\Db;
  18. /**
  19. * 异步任务基类
  20. * Class Queue
  21. * @package app\admin\queue
  22. */
  23. abstract class Queue
  24. {
  25. /**
  26. * 当前任务ID
  27. * @var integer
  28. */
  29. public $jobid = 0;
  30. /**
  31. * 当前任务标题
  32. * @var string
  33. */
  34. public $title = '';
  35. /**
  36. * 判断是否WIN环境
  37. * @return boolean
  38. */
  39. protected function isWin()
  40. {
  41. return PATH_SEPARATOR === ';';
  42. }
  43. /**
  44. * 重发异步任务记录
  45. * @param integer $wait 等待时间
  46. * @return boolean
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. * @throws \think\exception\DbException
  50. */
  51. protected function redo($wait = 0)
  52. {
  53. if ($this->jobid > 0) {
  54. if ($queue = Db::name('SystemQueue')->where(['id' => $this->jobid])->find()) {
  55. $queue['time'] = time() + $wait;
  56. $queue['title'] .= " - 来自任务{$this->jobid} 重发任务";
  57. unset($queue['id'], $queue['create_at'], $queue['desc']);
  58. return Db::name('SystemQueue')->insert($queue) !== false;
  59. }
  60. }
  61. return false;
  62. }
  63. /**
  64. * 执行异步任务
  65. * @param Input $input 输入对象
  66. * @param Output $output 输出对象
  67. * @param array $data 任务参数
  68. * @return mixed
  69. */
  70. abstract function execute(Input $input, Output $output, array $data = []);
  71. }