当前位置:首页 > PHP > 正文内容

thinkphp6定时任务,tp6定时任务,thinkphp定时任务,php定时任务,php定时器

高老师5年前 (2020-05-07)PHP7809

本教程使用的定时任务基于EasyTak,EasyTask官方文档https://gitee.com/392223903/EasyTask

(1).安装tp6

composer create-project topthink/think tp

(2).安装定时任务composer包

composer require easy-task/easy-task

(3).创建命令行处理类文件

php think make:command Task  task

会生成文件:tp\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://blog.20230611.cn/?id=327';
            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');
        }
    }
}

(4).配置tp\config\console.php文件

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

(5).执行命令(windows请使用cmd):

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

提示:后台执行失败可修改为前台启动查看问题或者查看日志文件,有问题可以在qq群反馈bug,记得用星星支持我们哦

扫描二维码推送至手机访问。

版权声明:本文由高久峰个人博客发布,如需转载请注明出处。

本文链接:https://blog.20230611.cn/post/137.html

分享给朋友:

“thinkphp6定时任务,tp6定时任务,thinkphp定时任务,php定时任务,php定时器” 的相关文章

php创建webservice,php搭建webservice,php编写webservice

php创建webservice,php搭建webservice,php编写webservice

第一步:服务端文件<?php $wsdlfile='webservice.wsdl'; ini_set('soap.wsdl_cache_enabled','0');    //关闭WSDL缓存 //001...

PHP二维数组排序,PHP多维数组排序, array_multisort()

PHP二维数组排序,PHP多维数组排序, array_multisort()

使用php函数array_multisort()即可实现和SQL一样的order by排序. 例如我们需要对会员表按照主键降序排列,年龄升序排列://会员表数据 $list = []; $list[] = ['mid' =>&n...

php new class

php new class

<?php $member = new class {     public function getInfo()     {    ...

php限制方法返回值类型

php限制方法返回值类型

php7新增的特性(1).强制限制只能返回一种类型<?php class task { } //must return an integer function add(): int {    &nb...

php定义常量数组

php定义常量数组

<?php //php7+ define('CONFIG', [     'MYSQL' => '127.0.0.1',     ...

php mcrypt扩展被废弃的解决方案

php mcrypt扩展被废弃的解决方案

使用openssl扩展对应替换mcrypt的函数,(比较麻烦,但是openssl是未来趋势)在新版php中编译mcrypt扩展使用一个纯php代码实现的mcrypt扩展库,git地址为https://github.com/phpseclib/mcrypt_compat,每个mcrypt的方法都已经实...