/** * 获取一组任务 * * @return array */ private function fetchTasks() { $tasks = Model_House_UpDown_Task::data_access(date('Ymd', strtotime($this->cursor->date)))->filter_by_op('id', '>', $this->cursor->id)->filter('flag', 0)->limit($this->per)->sort('id', 'asc')->find(); if (empty($tasks) && strtotime($this->cursor->date) < strtotime(date('Y-m-d 00:00:00'))) { $this->cursor->date = date('Y-m-d', strtotime('+1 day', strtotime($this->cursor->date))); $this->cursor->id = 0; $this->cursor->save(); } return $tasks; }
public function handle_request() { // 初始化开始和结束日期 $startDate = $this->cursor->date; $endDate = $this->cursor->date; // 获取截止日期 $redisQueueBll = Bll_House_UpDown_RedisQueueBll::get_instance(); $queues = $redisQueueBll->queues(); foreach ($queues as $queue) { $result = $redisQueueBll->clear($queue); if (isset($result['old']) && $result['old']) { $rawColumnTask = json_decode($result['old'], true); $tmpEndDate = date('Y-m-d', $rawColumnTask['add_date']); if ($this->compareDate($endDate, $tmpEndDate) > 0) { $endDate = $tmpEndDate; } } } // 获取开始ID $startId = Model_House_UpDown_Task::data_access(date('Ymd', strtotime($startDate)))->filter('flag', 1)->max('id'); $startId += 1; do { $tasks = Model_House_UpDown_Task::data_access(date('Ymd', strtotime($startDate)))->filter_by_op('id', '<', $startId)->filter('flag', 1)->limit($this->per)->sort('id', 'desc')->find_all(); if (empty($tasks)) { // 如果 StartDate > EndDate,更新游标 if ($this->compareDate($startDate, $endDate) > 0) { $startDate = date('Y-m-d', strtotime('-1 day', strtotime($startDate))); $startId = Model_House_UpDown_Task::data_access(date('Ymd', strtotime($startDate)))->filter('flag', 1)->max('id'); $startId += 1; // 更新游标 $this->cursor->setDate($startDate)->setId($startId)->save(); } else { // 如果 StartDate 和 EndDate 相等,说明初始化结束 $this->cursor->setId($startId - 1)->save(); break; } } foreach ($tasks as $task) { $task->flag = 0; $task->save(); // 更新游标 $startId = $task->id; $this->cursor->setId($startId)->save(); } } while (true); }
/** * DB获取未处理队列数 * * @return array */ private function getDBNoDealTasksNum() { return Model_House_UpDown_Task::data_access(date('Ymd'))->filter('flag', 0)->count(); }
/** * DB获取未处理队列数 * @return array */ private function getDBNoDealTasksNum() { $dateYmd = date('Ymd', $this->currentTime); return Model_House_UpDown_Task::data_access($dateYmd)->filter('flag', array(0, 1))->count(); }