ThinkPHP 6.1 定时任务实现

在ThinkPHP中实现定时任务处理,需要使用的是官方文档中的创建自定义指令功能。

目录

创建文件

文件内容

修改描述

业务处理

控制台配置

测试-命令帮助

执行任务

命令参数

参数描述

添加参数

执行多个任务

总结


创建文件

通过think 命令行操作

php think make:command cronTest test

提示创建成功

 

文件内容

打开cronTest文件,可看到以下内容:

<?php
declare (strict_types = 1);

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;

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

    protected function execute(Input $input, Output $output)
    {
        // 指令输出
        $output->writeln('test');
    }
}

修改描述

protected function configure()
{
    // 指令配置
    $this->setName('test')
        ->setDescription('执行测试的定时任务');
}

业务处理

设置获取所有状态正常的用户并打印输出

protected function execute(Input $input, Output $output)
{
    $user = Db::name('user')->where('isdel', 0)->select()->toArray();
    foreach ($user as $k => $v) {
        print_r($v);
    }

    // 指令输出
    $output->writeln('test');
}

控制台配置

配置config/console.php文件

把刚才创建和编辑好的任务文件在这里注册。

<?php
// +----------------------------------------------------------------------
// | 控制台配置
// +----------------------------------------------------------------------
return [
    // 指令定义
    'commands' => [
        \app\command\cronTest::class
    ],
];

测试-命令帮助

命令行下运行

php think

可看到有之前设置的自定义指令test。

执行任务

命令

php think test

可看到打印的用户信息,已执行成功。

命令参数

参数描述

use think\console\input\Argument;


// 必传参数
Argument::REQUIRED = 1;
// 可选参数
Argument::OPTIONAL = 2;
// 数组参数
Argument::IS_ARRAY = 4;

添加参数

指令设置中添加一个name参数

/**
 * 指令配置
 */
protected function configure()
{
    $this->setName('test')
        ->addArgument('name', Argument::OPTIONAL, "命令参数name")
        ->setDescription('执行测试的定时任务');
}

命令执行中接收name参数

/**
 * 命令执行
 * @param Input $input
 * @param Output $output
 * @return int|void|null
 */
protected function execute(Input $input, Output $output)
{
    $name = trim($input->getArgument('name'));
    $name = $name ?: '';

    $output->writeln("ThinkPHP 6.1," . $name . '!');
}

执行结果

 

执行多个任务

可改为type参数

protected function configure()
{
    $this->setName('test')
        ->addArgument('type', Argument::OPTIONAL, "命令参数name")
        ->setDescription('执行测试的定时任务');
}

在执行时通过type参数判断来分别处理

protected function execute(Input $input, Output $output)
{
    $type = intval($input->getArgument('type'));
    $type = $type ?: 3;
    switch ($type) {
        case 1:
            $content = '执行任务1';
            break;
        case 2:
            $content = '执行任务2';
            break;
        default:
            $content = '未执行任务';
            break;
    }

    $output->writeln("ThinkPHP 6.1," . $content . '!');
}

执行效果:

总结

至此已经结束,剩下的就是在linux中使用crontab设置定时执行了。其实还可以在控制器中调用任务,比较简单可以查看官方文档,就不再鏖述。实现与ThinkPHP5定时任务实现区别不大。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JSON_L

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值