getLogger('notice'); $tasks = $this->findTasks(500); if ($tasks->count() == 0) { return; } foreach ($tasks as $task) { try { switch ($task->item_type) { case TaskModel::TYPE_NOTICE_ACCOUNT_LOGIN: $this->handleAccountLoginNotice($task); break; case TaskModel::TYPE_NOTICE_LIVE_BEGIN: $this->handleLiveBeginNotice($task); break; case TaskModel::TYPE_NOTICE_ORDER_FINISH: $this->handleOrderFinishNotice($task); break; case TaskModel::TYPE_NOTICE_REFUND_FINISH: $this->handleRefundFinishNotice($task); break; case TaskModel::TYPE_NOTICE_CONSULT_REPLY: $this->handleConsultReplyNotice($task); break; } $task->status = TaskModel::STATUS_FINISHED; $task->update(); } catch (\Exception $e) { $task->try_count += 1; $task->priority += 1; if ($task->try_count > self::TRY_COUNT) { $task->status = TaskModel::STATUS_FAILED; } $task->update(); $logger->info('Notice Process Exception ' . kg_json_encode([ 'code' => $e->getCode(), 'message' => $e->getMessage(), 'task' => $task->toArray(), ])); } } } protected function handleAccountLoginNotice(TaskModel $task) { $notice = new AccountLoginNotice(); return $notice->handleTask($task); } protected function handleLiveBeginNotice(TaskModel $task) { $notice = new LiveBeginNotice(); return $notice->handleTask($task); } protected function handleOrderFinishNotice(TaskModel $task) { $notice = new OrderFinishNotice(); return $notice->handleTask($task); } protected function handleRefundFinishNotice(TaskModel $task) { $notice = new RefundFinishNotice(); return $notice->handleTask($task); } protected function handleConsultReplyNotice(TaskModel $task) { $notice = new ConsultReplyNotice(); return $notice->handleTask($task); } /** * @param int $limit * @return ResultsetInterface|Resultset|TaskModel[] */ protected function findTasks($limit = 100) { $itemTypes = [ TaskModel::TYPE_NOTICE_ACCOUNT_LOGIN, TaskModel::TYPE_NOTICE_LIVE_BEGIN, TaskModel::TYPE_NOTICE_ORDER_FINISH, TaskModel::TYPE_NOTICE_REFUND_FINISH, TaskModel::TYPE_NOTICE_CONSULT_REPLY, ]; $status = TaskModel::STATUS_PENDING; $tryCount = self::TRY_COUNT; return TaskModel::query() ->inWhere('item_type', $itemTypes) ->andWhere('status = :status:', ['status' => $status]) ->andWhere('try_count < :try_count:', ['try_count' => $tryCount + 1]) ->orderBy('priority ASC') ->limit($limit) ->execute(); } }