当前位置: 代码网 > it编程>编程语言>Php > 使用ThinkPHP框架(thinkphp8.0)创建定时任的操作步骤

使用ThinkPHP框架(thinkphp8.0)创建定时任的操作步骤

2024年05月18日 Php 我要评论
1、安装定时任务composer包composer require easy-task/easy-task2、创建命令行处理类文件php think make:command task task会生

1、安装定时任务composer包

composer require easy-task/easy-task

2、创建命令行处理类文件

php think make:command task  task

会生成文件:app\command\task.php

将task.php文件内容修改如下:

<?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 task extends command
{
    protected function configure()
    {
        //设置名称为task
        $this->setname('task')
            //增加一个命令参数
            ->addargument('action', argument::optional, "action", '')
            ->addargument('force', argument::optional, "force", '');
    }
 
    protected function execute(input $input, output $output)
    {
        //获取输入参数
        $action = trim($input->getargument('action'));
        $force = trim($input->getargument('force'));
 
        // 配置任务,每隔20秒访问2次网站
        $task = new \easytask\task();
        $task->setruntimepath('./runtime/');
        $task->addfunc(function () {
            $url = 'https://www.wushengyong.com/';
            file_get_contents($url);
        }, 'request', 20, 2);;
 
        // 根据命令执行
        if ($action == 'start')
        {
            $task->start();
        }
        elseif ($action == 'status')
        {
            $task->status();
        }
        elseif ($action == 'stop')
        {
            $force = ($force == 'force'); //是否强制停止
            $task->stop($force);
        }
        else
        {
            exit('command is not exist');
        }
    }
}

3、配置config\console.php文件

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

4、执行命令(windows请使用cmd):

php think task start  启动命令
php think task status 查询命令
php think task stop   关闭命令
php  think  task  stop  force   强制关闭命令

以上就是使用thinkphp框架(thinkphp8.0)创建定时任的操作步骤的详细内容,更多关于thinkphp框架创建定时任务的资料请关注代码网其它相关文章!

(0)

相关文章:

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

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com