微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

workman 定时任务

tp6

 

第一步:

创建自定义指令

PHP think make:command Hello hello

生成一个app\command\Hello命令行指令类,我们修改内容如下:
第二步,配置config/console.PHP文件
<?PHP
return [
    'commands' => [
        'hello' => 'app\command\Hello',
    ]
];


第三步,测试-命令帮助-命令行下运行

PHP think hello

安装workman
cmd到根目录下:
composer require workerman/workerman
安装workman

app\command\Hello中代码

<?PHP

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;

use Workerman\Lib\Timer;
use Workerman\Worker;


class Hello extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('hello');
        // 设置参数
        
    }

    protected function execute(Input $input, Output $output)
    {
        $task = new Worker();
        // 开启多少个进程运行定时任务,注意业务是否在多进程有并发问题
        $task->count = 1;
        $task->onWorkerStart = function(Worker $task)
        {
            // 每2.5秒执行一次
            $time_interval = 2.5;
            Timer::add($time_interval, function()
            {
                echo "task run\n";
            });
        };

        // 运行worker
        Worker::runAll();
    	// 指令输出
    	$output->writeln('hello');
    }
}

  

到此为止,直接在tp根目录运行,PHP think hello  每隔2.5秒执行一遍脚本,输出task run




terminal 运行tp方法 

PHP public/index.PHP index/index/hello

























































版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐