public function beforeRun(TaskInterface $task) { if ($this->canClose) { $closed = Redis::cacheRedis()->hDel('closed_tasks', $task->parentId > 0 ? $task->parentId : $task->id); if ($closed) { return TaskInterface::STATUS_CLOSED_BY_CLIENT; } } if ($this->runAt > $task->beginTime) { return TaskInterface::STATUS_NOT_READY; } return true; }
function run($worker, $data) { //读取定时任务 $currentTime = time(); $data = Redis::queueRedis()->lPop('tarth-timer-' . $worker->tarthTimerLastTime); if ($data == false) { //在不超过当前时间情况下,获取下一秒的数据 if ($worker->tarthTimerLastTime < $currentTime) { $worker->tarthTimerLastTime++; } return false; } $task = AbstractTask::createTask(json_decode($data, true)); return $task->push(true); }
public function beforeRun(TaskInterface $task) { if (isset($this->_units[$this->unit]) == false) { $this->unit = self::UNIT_MINUTE; } $timeString = date($this->_units[$this->unit][0]) . $this->unit; $cacheKey = sprintf(self::CACHE_KEY, $task->key(), $timeString); $currentCount = Redis::cacheRedis()->incrBy($cacheKey, 1); if ($currentCount == 1) { $timeout = $this->_units[$this->unit][1]; Redis::cacheRedis()->setTimeout($cacheKey, $timeout); } if ($currentCount > $this->max) { return TaskInterface::STATUS_SKIP_BY_LIMIT; } return true; }
public function push($toQueue = false) { try { if ($toQueue == false) { $runTimeInFuture = $this->_runAtFuture(); } else { $runTimeInFuture = false; } if ($runTimeInFuture) { if (Redis::queueRedis()->rPush('tarth-timer-' . $runTimeInFuture, (string) $this)) { return $this->id; } } else { if (Redis::queueRedis()->rPush('tarth-queue-' . $this->priority, (string) $this)) { return $this->id; } } } catch (Exception $e) { Log::logger()->addError($e->getMessage); } return false; }
public function afterRun(TaskInterface $task) { Redis::cacheRedis()->delete($this->_key); }
function init($worker) { Redis::setCacheServer(get_option_value($worker->config, 'resources.cache', '127.0.0.1:6379')); Redis::setQueueServer(get_option_value($worker->config, 'resources.queue', '127.0.0.1:6379')); }
<?php require_once __DIR__ . '/../vendor/autoload.php'; \Tarth\Tool\Redis::setCacheServer('127.0.0.1:6379'); \Tarth\Tool\Redis::setQueueServer('127.0.0.1:6379'); $task = \Tarth\Tool\Task::createApiTask('http://alleria.mcp.wap.grid.sina.com.cn/test/normal'); echo \Tarth\Tool\Task::exec();
/** * 关闭等待中的定时任务 * @param integer $taskId 任务ID * @return [type] [description] */ public static function closeTask($taskId) { return Redis::cacheRedis()->hSet('closed_tasks', $taskId, 1); }