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

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

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

本教程使用的定时任务基于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 base64保存为图片偷懒版本

php base64保存为图片偷懒版本

<?php $base64_body = substr(strstr($_POST[base64],','),1); $data= base64_decode($base64_body); file_put_contents($_SERVER[&q...

php多进程,php多进程处理任务,php多进程应用场景

php多进程,php多进程处理任务,php多进程应用场景

php多进程应用场景主要是非web端,fpm下是不支持多进程的,非类linux操作系统都不支持,请在cli模式使用.可以使用多进程做任务分发,批量计算,批量文件处理,批量爬虫,网络运维等等。下面看一份简单的入门demo//创建子进程 $pid=pcntl_fork(); //返回-1,创建失败,不...

编写php com组件,php调用.net dll,php源码保护

编写php com组件,php调用.net dll,php源码保护

开发com组件可以用c++,vc++,net,我比较熟悉net,演示用dnet(1).创建项目:启动vs2017,新建项目,选择Visual C# ->Windows桌面->类库通用windows(2). 修改Com项目:点击 项目->项目属性,再点击应用程序->程序集信息,...

php soap 捕获异常,使用try catch 捕获Soap 异常

php soap 捕获异常,使用try catch 捕获Soap 异常

项目中使用服务来执行webservice,由于对方系统api不稳定,经常导致服务崩溃,只能重启,一个月差不多要重启一次。初期的解决办法是捕获异常,然后continue掉。<?php try {     $url = 'http...

php执行慢原因查找

php执行慢原因查找

今天帮朋友查询wordpress执行超级慢的原因,特此记录开启fpm的慢日志,记录执行超过30秒的脚本request_slowlog_timeout = 30 slowlog = var/log/slow.log查看日志[23-May-2019 17...

php限制方法返回值类型

php限制方法返回值类型

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