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

thinkphp5定时任务,tp5定时任务,thinkphp定时任务,php定时任务,php定时器

高老师6年前 (2020-03-01)PHP7774

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

(1).安装tp5.0或者5.1

composer create-project topthink/think=5.0.* tp5  --prefer-dist

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

composer require easy-task/easy-task

(3).创建命令行处理类文件application/common/command/Task.php

<?php
namespace app\common\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
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'));

        // 配置任务
        $task = new \EasyTask\Task();
        $task->setRunTimePath('./runtime/');
        $task->addFunc(function () {
            $url = 'https://blog.20230611.cn/?id=319';
            file_get_contents($url);
        }, 'request', 10, 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).将上面创建的Task.php在配置文件application/command.php中配置一下

return [
    'app\common\command\Task',
];

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

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

上面创建的定时任务是每隔10秒访问2次网站地址。

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

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

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

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

分享给朋友:

“ thinkphp5定时任务,tp5定时任务,thinkphp定时任务,php定时任务,php定时器” 的相关文章

php 将数组键值转为变量

php 将数组键值转为变量

<?php $data=array('a'=>1,'b'=>2,'c'=>3,'d'=>4); extract($data); var_dump($a,$b,$c,$d); ?>在人人商城中捡到的...

php迭代器学习

php迭代器学习

php官方已经提供了Iterator(迭代器)接口,通过网上资料的学习,目前看适合超大集合或者数组提取使用。学习一个函数的实现对比内存占用差距.使用迭代器和普通循环实现range()函数。(1).普通循环实现range()函数。function newrange($low, $h...

Thinkphp Call Stack,PHP调用栈Call Stack的获取

Thinkphp Call Stack,PHP调用栈Call Stack的获取

ThinkPHP中有一个debug调试功能,能输出报错文件的信息,并能看到这个函数被哪些函数调用,从框架的启动开始记录,特别方便调试。于是研究了下它的底层给予了实现。<?php //--框架核心--Start //框架内置错误处理 function errDealWith($er...

php finally使用

php finally使用

<?php /**  * @throws Exception  */ function curl() {     throw  new \Exception('err...

php jsonp转json,php jsonp转数组,php jsonp转对象

php jsonp转json,php jsonp转数组,php jsonp转对象

将jsonp转为PHP数组和对象。/**  * jsonp转数组|Jsonp转json  * @param string $jsonp jsonp字符串  * @param bool $as...

tp orm事务提交未执行的教训和总结

tp orm事务提交未执行的教训和总结

最近在项目中处理一个关于商品数据重复需要删除多余的商品记录,但是删除一条商品必然要把关联的其他表商品的id和其他商品信息更换为正确的,删除一个商品记录,同时要去修改100多张表的关联商品数据,在项目中引用了tp orm 1.2版本,由于项目是php5.6版本,没法使用最新orm,在代码中每处理1个商...