tp6 使用workerman 实现定时任务

composer require workerman/workerman 
php think make:command Timer 
<?php


namespace app\command;

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
{
    /**
     * @var int
     */
    protected $timer;
    /**
     * @var int|float
     */
    protected $interval = 1;
    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);
        //创建定时器任务
        $task = new Worker();
        $task->count = 1;
        $task->onWorkerStart = [$this, 'start'];
        $task->runAll();
    }
    public function stop()
    {
        //手动暂停定时器
        \Workerman\Lib\Timer::del($this->timer);
    }
    public function start()
    {

        $this->Timer= \Workerman\Lib\Timer::add($this->interval, function () {
            //每隔1秒执行一次
            //亲测有效,只需在这个方法写逻辑代码......
        });
    }
}

需要在 config/console.php  文件中定义这个定时器

'commands' => [
    // '定时器名称' => '文件路径'
     'timer'=>\app\command\Timer::class
], 

启动定时任务

php think timer start 

php think timer start --d (守护进程)

如果服务器重启了,一定记得手动启动下定时任务 !

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在 ThinkPHP 6 中使用 Workerman 实现定时器可以通过添加自定义命令来实现。下面是一个简单的实现步骤: 1. 首先,确保你已经安装了 Workerman 和 think-worker 扩展。可以通过执行以下命令来安装它们: ``` composer require workerman/workerman think-worker ``` 2. 创建一个自定义的命令类来处理定时任务。在 app/command 目录下创建一个名为 Timer.php 的文件,并在该文件中编写以下代码: ```php <?php namespace app\command; use think\console\Command; use think\console\Input; use think\console\Output; class Timer extends Command { protected function configure() { $this->setName('timer:work')->setDescription('Workerman Timer'); } protected function execute(Input $input, Output $output) { $worker = new \Workerman\Worker(); $worker->onWorkerStart = function($worker) { // 在这里编写定时任务的处理逻辑 \Workerman\Lib\Timer::add(1, function() { echo "定时任务执行\n"; }); }; \Workerman\Worker::runAll(); } } ``` 3. 注册自定义命令。在 config/console.php 中的 commands 数组中添加命令类的命名空间路径: ```php 'commands' => [ 'app\command\Timer', ], ``` 4. 运行定时任务。通过执行以下命令来运行定时任务: ``` php think timer:work ``` 这样,定时任务就会在后台运行,并每秒钟执行一次。你可以在 `$worker->onWorkerStart` 回调函数中编写具体的定时任务逻辑。请根据自己的需求进行修改和扩展。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值