thinkphp6开启定时任务

thinkphp6开启定时任务

猿掌柜
2021-03-20 / 1 评论 / 127 阅读 / 未收录,推送中... 🔋充电中 100%

本教程使用的定时任务基于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://www.gaojiufeng.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   强制关闭命令

{message type="info"}后台执行失败可修改为前台启动查看问题或者查看日志文件,有问题可以在qq群反馈bug,记得用星星支持我们哦{/message}

8

评论 (1)

OωO
  • 爱你
  • 爱心
  • 傲慢
  • 白眼
  • 棒棒糖
  • 爆筋
  • 抱拳
  • 鄙视
  • 闭嘴
  • 擦汗
  • 菜刀
  • 吃
  • 呲牙
  • 大兵
  • 大哭
  • 蛋
  • 得意
  • doge
  • 发呆
  • 发怒
  • 奋斗
  • 尴尬
  • 勾引
  • 鼓掌
  • 害羞
  • 憨笑
  • 好棒
  • 哈欠
  • 喝彩
  • 河蟹
  • 坏笑
  • 饥饿
  • 惊恐
  • 惊喜
  • 惊讶
  • 菊花
  • 可爱
  • 可怜
  • 抠鼻
  • 酷
  • 快哭了
  • 骷髅
  • 困
  • 篮球
  • 泪奔
  • 冷汗
  • 流汗
  • 流泪
  • 难过
  • OK
  • 喷血
  • 撇嘴
  • 啤酒
  • 强
  • 敲打
  • 亲亲
  • 糗大了
  • 拳头
  • 骚扰
  • 色
  • 胜利
  • 手枪
  • 衰
  • 睡
  • 调皮
  • 偷笑
  • 吐
  • 托腮
  • 委屈
  • 微笑
  • 握手
  • 我最美
  • 无奈
  • 吓
  • 小纠结
  • 笑哭
  • 小样儿
  • 斜眼笑
  • 西瓜
  • 嘘
  • 羊驼
  • 阴险
  • 疑问
  • 右哼哼
  • 幽灵
  • 晕
  • 再见
  • 眨眼睛
  • 折磨
  • 咒骂
  • 抓狂
  • 左哼哼
经典
颜文字
取消
  1. 头像
    phper
    iPhone · UC Browser

    受教了……表情

    回复