thinkphp6定时任务 EasyTak
一. 安装 composer require easy-task/easy-task二. 创建类 php think make:command Tasktask<?phpdeclare (strict_types=1);namespace app\command;use think\console\Command;use think\console\Input;use think\conso
一. 安装 composer require easy-task/easy-task
二. 创建类 php think make:command Task task
<?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';
$content = file_get_contents($url);
echo $content;
}, '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');
}
}
}
三. 配置tp\config\console.php文件
<?php
// +----------------------------------------------------------------------
// | 控制台配置
// +----------------------------------------------------------------------
return [
// 指令定义
'commands' => [
'task' => 'app\command\Task',
],
];
五. 执行指令 windows上
执行命令(windows请使用cmd):
php think task start 启动命令
php think task status 查询命令
php think task stop 关闭命令
php think task stop force 强制关闭命令
六. 错误处理
执行 php think task start 启动命令 会报错 Failed to create COM object Wpc.Core’: 无效的语法。
(3).wpc扩展安装方法,下载wpc扩展一键安装包,根据PHP是32/64位执行一键安装包即可,切记此处说明的是PHP的位数,不是系统的位数,下载地址:https://www.gaojiufeng.cn/static/exe/1.0/Wpc_install.zip 扩展包属于com扩展,所以请不要手工安装dll到php中,windows中此工具仅作为开发环境支持,建议生产环境使用linux
解压缩包选择exe, 点击安装,选择php目录,安装成功

执行成功结果
参考 : https://www.gaojiufeng.cn/?id=328
https://www.kancloud.cn/a392223903/easytask/1666907
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)