💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
> 定时器任务功能类似于Linux下的crontab;因为使用了开源的[cron-expression](https://github.com/dragonmantank/cron-expression)来解析运行周期,所以zcswoole目前最小粒度只支持到分钟;swoole可以实现毫秒级别的定时器,所以如果你需要更小粒度的定时器需求,那么需要自己实现解析运行周期的方法; #### 原理: zcswoole的定时器为1秒轮询所有任务,将到期的任务丢到task进程处理,task进程不会影响工作进程,所以理论上误差不会很大 #### 创建任务表: ```sql DROP TABLE IF EXISTS contrab; CREATE TABLE contrab ( `id` INT(11) NOT NULL AUTO_INCREMENT, `timer` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '定时周期', `command` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '命令', `remark` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '备注', `create_time` DATETIME DEFAULT NULL, `status` TINYINT(2) NOT NULL DEFAULT '0' COMMENT '0开启,1关闭', PRIMARY KEY (`id`) ) ENGINE = InnoDB CHARSET = utf8 COLLATE utf8_general_ci COMMENT = '定时器'; ``` #### 代码: zcswoole在http服务已内置定时器功能,在http服务启动时,会在第一个进程设置一个定时器,每秒轮询所有任务 ```php /** * http服务,继承zcswoole的httpServer服务后,覆盖父类方法可以扩展自己功能 * Class HttpCommand * @package command */ class HttpCommand extends HttpServerCommand { // demo,为http服务添加一个定时器功能,重写父类onEvent方法 public function onEvent() { parent::onEvent(); $this->server->on('receive', [$this, 'receive']); $this->server->on('task', [$this, 'task']); $this->server->on('finish', [$this, 'finish']); } /** * 开始进程,重写父类workerStart方法 * @param $server * @param $workerID */ public function workerStart($server, $workerID) { // 为第一个进程安装定时器 if (!$this->server->taskworker && $workerID == 0) { Console::msg('crontab task start......'); swoole_timer_tick(1000, function(){ $records = MysqliDB::instance()->where('status', 0)->get('crontab'); foreach ($records as $record) { $nextTime = CronExpression::factory($record['timer'])->getNextRunDate()->getTimestamp() - 1; if (time() == $nextTime) { $this->server->task($record['command']); } /*else { Console::msg("no run; next time is ".$nextTime.", now time is ". time()); }*/ } }); } } /** * 处理任务 * @param Server $server * @param $taskID * @param $workerID * @param $data * @return string */ public function task(Server $server, $taskID, $workerID, $data) { exec($data); return $data . 'success'; } /** * 结束任务回调 * @param Server $server * @param $taskID * @param $data */ public function finish(Server $server, $taskID, $data) { Console::success('has run ' . $data); } } ``` #### 效果: - 任务管理页面: ![](https://box.kancloud.cn/fc598a5c55dd828b064c775f47c45ac6_1356x534.png) - 后台执行: ![](https://box.kancloud.cn/b751c9a7187c66c559dc1bb7b8b1b316_583x141.png)