123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- <?php
- namespace crmeb\utils;
- use crmeb\traits\LogicTrait;
- use think\facade\Queue as QueueJob;
- /**
- * Class Queue
- * @package crmeb\utils
- * @method $this setJobClassName(string $jobClassName); 设置任务类名
- * @method $this setQueue(string $queue); 设置任务名
- * @method $this setDelay(int $delay); 设置延迟时间
- * @method $this setJobData(array $jobData); 设置任务参数
- */
- class Line
- {
- use LogicTrait;
- /**
- * 任务类名
- * @var string
- */
- protected $jobClassName = \crmeb\jobs\LineJob::class;
- /**
- * 多任务
- * @var null
- */
- protected $task = null;
- /**
- * 任务参数
- * @var array
- */
- protected $jobData = [
- 'beforeMethod' => 'doBeforeMethod', //默认任务执行前执行的方法
- 'method' => 'doDefaultJod', //默认任务执行方法
- 'data' => null, //执行任务需要的参数
- 'errorTimes' => 3, //任务执行错误最大次数
- 'release' => 0,//延迟执行秒数
- ];
- /**
- * 任务名称
- * @var string
- */
- protected $queue = 'line';
- /**
- * 延迟执行秒数
- * @var int
- */
- protected $delay = 0;
- /**
- * 规则
- * @var array
- */
- protected $propsRule = [
- 'jobClassName' => null,
- 'queue' => null,
- 'delay' => null,
- 'jobData' => null,
- ];
- /**
- * 创建定时执行任务
- * @param $data
- * @return mixed
- */
- public function push($data = null)
- {
- $this->merge($data);
- return QueueJob::push($this->jobClassName, $this->jobData, $this->queue);
- }
- /**
- * 创建延迟执行任务
- * @param null $data
- * @return mixed
- */
- public function later($data = null)
- {
- $this->merge($data);
- return QueueJob::later($this->delay, $this->jobClassName, $this->jobData, $this->queue);
- }
- /**
- * 合并处理参数
- * @param $data
- */
- protected function merge($data)
- {
- if ($data) {
- $this->jobData['data'] = $data;
- }
- if ($this->delay && !$this->jobData['release']) {
- $this->jobData['release'] = $this->delay;
- }
- $this->jobClassName = $this->task ? $this->jobClassName . '@' . $this->task : $this->jobClassName;
- }
- /**
- * 创建任务
- * @param null $data
- * @return mixed
- */
- public static function create($data = null)
- {
- $instance = self::instance();
- if ($instance->delay) {
- return $instance->later($data);
- } else {
- return $instance->push($data);
- }
- }
- }
|