[ 'params' => $params, 'class' => get_class($this) ], ]); if ($row->save()) { $this->twid = $row->id; } } /** * 重写投递:非 Swoole 运行时(artisan/测试)无 swoole 绑定,无法投递异步任务,跳过(与 AbstractObserver 守卫一致) * @param mixed $task * @return bool */ protected function task($task) { if (!app()->bound('swoole')) { return false; } return parent::task($task); } /** * 开始执行任务 */ abstract public function start(); /** * 任务完成事件 */ abstract public function end(); /** * 重写执行过程 */ final public function handle() { TaskWorker::whereId($this->twid)->update(['start_at' => Carbon::now()]); // try { $this->start(); } catch (\Throwable $e) { $this->failed("start", $e); } } /** * 重写完成事件 */ final public function finish() { TaskWorker::whereId($this->twid)->update(['end_at' => Carbon::now()]); // try { $this->end(); TaskWorker::whereId($this->twid)->delete(); } catch (\Throwable $e) { $this->failed("end", $e); } } /** * 任务失败事件 * @param string $type * @param \Throwable $e */ public function failed(string $type, \Throwable $e) { info($type); info($e); // TaskWorker::whereId($this->twid)->update(['error' => Base::array2json([ 'time' => Carbon::now(), 'type' => $type, 'code' => $e->getCode(), 'file' => $e->getFile(), 'message' => $e->getMessage(), ])]); } }