default_method = $this->method; } /** * 实例化当前队列 * @return static */ public static function instance() { if (is_null(self::$instance)) { self::$instance = new static(); } return self::$instance; } /** * 设置队列名称 * @param string $queue_name * @return $this */ public function setQueueName(string $queue_name) { $this->queue_name = $queue_name; return $this; } /** * 加入队列 * @param array|null $data * @return bool */ public function push(?array $data = null) { if (!$this->job) { return $this->setError('JOB_NOT_EXISTS'); } $jodValue = $this->getValues($data); //todo 队列扩展策略调度, $res = ThinkQueue::{$this->action()}(...$jodValue); if (!$res) { $res = ThinkQueue::{$this->action()}(...$jodValue); if (!$res) { Log::error('队列推送失败,参数:' . json_encode($jodValue, JSON_THROW_ON_ERROR)); } } $this->clean(); return $res; } /** * 清除数据 */ public function clean() { $this->secs = 0; $this->data = []; $this->queue_name = null; $this->error_count = 3; $this->method = $this->default_method; } /** * 获取任务方式 * @return string */ protected function action() { return $this->secs ? 'later' : 'push'; } /** * 获取参数 * @param $data * @return array */ protected function getValues($data) { $jobData['data'] = $data ?: $this->data; $jobData['method'] = $this->method; $jobData['error_count'] = $this->error_count; if ($this->method != $this->default_method) { $this->job .= '@' . $this->method; } if ($this->secs) { return [$this->secs, $this->job, $jobData, $this->queue_name]; } else { return [$this->job, $jobData, $this->queue_name]; } } /** * 不可访问时调用 * @param $method * @param $arguments * @return $this * @throws Exception * @throws Exception * @throws Exception */ public function __call($method, $arguments) { if (in_array($method, $this->allow_function)) { if ($method === 'data') { $this->{$method} = $arguments; } else { $this->{$method} = $arguments[0] ?? null; } return $this; } else { throw new Exception('Method does not exist' . __CLASS__ . '->' . $method . '()'); } } /** * 设置错误信息 * @param string|null $error * @return bool */ protected function setError(?string $error = null) { $this->error = $error; return false; } /** * 获取错误信息 * @return string */ public function getError() { $error = $this->error; $this->error = null; return $error; } }