🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
一、安装workman 1composer&nbsp;require&nbsp;workerman/workerman 二、创建 Timer 命令 1php think make:command Timers&nbsp; 三、实现Timer [![复制代码](https://common.cnblogs.com/images/copycode.gif)](javascript:void(0); "复制代码") ~~~ <?php declare (strict_types = 1); namespace app\command; use app\controller\Index; use think\console\Command; use think\console\Input; use think\console\input\Argument; use think\console\input\Option; use think\console\Output; use Workerman\Worker; class Timer extends Command { protected $timer; protected $interval =2; protected function configure() { // 指令配置 $this->setName('timer') ->addArgument('status', Argument::REQUIRED, 'start/stop/reload/status/connections') ->addOption('d', null, Option::VALUE_NONE, 'daemon(守护进程)方式启动') ->addOption('i', null, Option::VALUE_OPTIONAL, '多长时间执行一次') ->setDescription('开启/关闭/重启 定时任务'); } protected function init(Input $input, Output $output) { global $argv; if ($input->hasOption('i')) $this->interval = floatval($input->getOption('i')); $argv[1] = $input->getArgument('status') ?: 'start'; if ($input->hasOption('d')) { $argv[2] = '-d'; } else { unset($argv[2]); } } protected function execute(Input $input, Output $output) { $this->init($input, $output); //创建定时器任务 new Worker('websocket://0.0.0.0:2346'); $task = new Worker(); $task->count = 1; //每个子进程启动时都会执行$this->start方法 $task->onWorkerStart = [$this, 'start']; //每个子进程连接时都会执行,浏览器127.0.0.1:2346,就能调用方法 $task->onConnect = function ($connection) { echo "nihao\n"; }; $task->runAll(); } public function stop() { //手动暂停定时器 \Workerman\Lib\Timer::del($this->timer); } public function start() { // workerman的Timer定时器类 add ,$time_interval是多长时间执行一次 $time_interval = 2.5; \Workerman\Lib\Timer::add($time_interval, function() { // 运行控制器Index的index echo Index::index(); }); // 下面是网上找的内容 $last = time(); $task = [6 => $last, 10 => $last, 30 => $last, 60 => $last, 180 => $last, 300 => $last]; $this->timer = \Workerman\Lib\Timer::add($this->interval, function () use (&$task) { //每隔2秒执行一次 try { $now = time(); foreach ($task as $sec => $time) { if ($now - $time >= $sec) { //每隔$sec秒执行一次 $task[$sec] = $now; } } } catch (\Throwable $e) { } }); } } ~~~ [![复制代码](https://common.cnblogs.com/images/copycode.gif)](javascript:void(0); "复制代码") 四、注册 Timer 命令 app/config/console.php 修改 console.php 文件 [![复制代码](https://common.cnblogs.com/images/copycode.gif)](javascript:void(0); "复制代码") ~~~ <?php // +---------------------------------------------------------------------- // | 控制台配置 // +---------------------------------------------------------------------- return [ // 指令定义 'commands' => [ //定时任务命令 'timer'=>\app\command\Timer::class, ], ]; ~~~ [![复制代码](https://common.cnblogs.com/images/copycode.gif)](javascript:void(0); "复制代码") 五、启动定时器 ~~~ php think timer start ~~~ 六、关闭定时器 ~~~ php think timer stop ~~~