Queue.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2021 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://gitee.com/zoujingli/ThinkLibrary
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkLibrary
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkLibrary
  13. // +----------------------------------------------------------------------
  14. declare (strict_types=1);
  15. namespace think\admin;
  16. use think\admin\service\ProcessService;
  17. use think\admin\service\QueueService;
  18. use think\App;
  19. /**
  20. * 任务基础类
  21. * Class Queue
  22. * @package think\admin
  23. */
  24. abstract class Queue
  25. {
  26. /**
  27. * 应用实例
  28. * @var App
  29. */
  30. protected $app;
  31. /**
  32. * 任务控制服务
  33. * @var QueueService
  34. */
  35. protected $queue;
  36. /**
  37. * 进程控制服务
  38. * @var ProcessService
  39. */
  40. protected $process;
  41. /**
  42. * Queue constructor.
  43. * @param App $app
  44. * @param ProcessService $process
  45. */
  46. public function __construct(App $app, ProcessService $process)
  47. {
  48. $this->app = $app;
  49. $this->process = $process;
  50. }
  51. /**
  52. * 初始化任务数据
  53. * @param QueueService $queue
  54. * @return $this
  55. */
  56. public function initialize(QueueService $queue): Queue
  57. {
  58. $this->queue = $queue;
  59. return $this;
  60. }
  61. /**
  62. * 执行任务处理内容
  63. * @param array $data
  64. */
  65. abstract public function execute(array $data = []);
  66. /**
  67. * 设置失败的消息
  68. * @param string $message 消息内容
  69. * @throws Exception
  70. */
  71. protected function setQueueError(string $message): void
  72. {
  73. $this->queue->error($message);
  74. }
  75. /**
  76. * 设置成功的消息
  77. * @param string $message 消息内容
  78. * @throws Exception
  79. */
  80. protected function setQueueSuccess(string $message): void
  81. {
  82. $this->queue->success($message);
  83. }
  84. /**
  85. * 更新任务进度
  86. * @param integer $total 记录总和
  87. * @param integer $count 当前记录
  88. * @param string $message 文字描述
  89. * @param integer $backline 回退行数
  90. * @return static
  91. */
  92. protected function setQueueMessage(int $total, int $count, string $message = '', int $backline = 0): Queue
  93. {
  94. $this->queue->message($total, $count, $message, $backline);
  95. return $this;
  96. }
  97. /**
  98. * 设置任务的进度
  99. * @param null|string $message 进度消息
  100. * @param null|string $progress 进度数值
  101. * @param integer $backline 回退行数
  102. * @return Queue
  103. */
  104. protected function setQueueProgress(?string $message = null, ?string $progress = null, int $backline = 0): Queue
  105. {
  106. $this->queue->progress(2, $message, $progress, $backline);
  107. return $this;
  108. }
  109. }