随笔 - 612, 文章 - 0, 评论 - 31, 阅读 - 74万

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

TP6:使用Workerman执行定时任务

Posted on   eastson  阅读(2242)  评论(1编辑  收藏  举报

使用Workerman的Timer类,可以定时执行某些任务。

1. 建立app/test/command/Hello2.php。

<?php
declare (strict_types = 1);

namespace app\test\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use Workerman\Worker;
use Workerman\Lib\Timer;

class Hello2 extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('hello2')
            ->setDescription('the hello2 command');
    }

    protected function execute(Input $input, Output $output)
    {
        $worker = new Worker();

        $worker->name = $this->getName();

        $worker->onWorkerStart = function() use ($output)
        {
            // 每隔3秒钟执行一次
            Timer::add(3, array($this, 'sayHello'), array($output));
        };

        Worker::runAll();
    }

    public function sayHello(Output $output)
    {
        $output->writeln('Hello ' . date('Y-m-d H:i:s'));
    }
}

2. 配置config\console.php。

<?php

return [
    'commands' => [
        'app\test\command\Hello2',
    ],
];

3. 测试命令执行结果。

相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
历史上的今天:
2014-05-10 How to authenticate a user by uid and password?
2014-05-10 LDAP 中 CN,OU,DC 的含意
2014-05-10 Apache Directory Studio
2014-05-10 LDAP编辑器 LDAPAdmin
2013-05-10 Windows XP下获取OpenERP源码
2013-05-10 关于OpenERP客户端字体问题的一点心得
点击右上角即可分享
微信分享提示