【内部资料】ThinkPHP6.0使用EasyTask常驻内存多进程任务管理
作者:野牛程序员:2023-11-24 15:00:56php阅读 2848
ThinkPHP6.0使用EasyTask常驻内存多进程任务管理
composer require easy-task/easy-task
功能Test代码
<?php
declare (strict_types = 1);
namespace app\\command;
use app\\admin\\controller\\Video;
use think\\console\\Command;
use think\\console\\Input;
use think\\console\\input\\Argument;
use think\\console\\input\\Option;
use think\\console\\Output;
use think\\facade\\Log;
/**
class Task extends Command
{
// protected function configure()
// {
// // 指令配置
// $this->setName('schedulePublish')->setDescription('定时发布视频');
// }
// 定义命令名称
protected $commandName = 'schedulePublish';
protected function configure()
{
// 指令配置
//这里 我们 定义了一个叫 schedulePublish 的命令,到时候我们运行该schedulePublish 命令 就会执行该文件下面的execute()方法
$this->setName($this->commandName)->setDescription('定时发布视频');
}
protected function execute(Input $input, Output $output)
{
// 指令输出
$output->writeln('app\\command\\task');
}
}
**/
class Task extends Command
{
protected function configure()
{
// $this->setName('schedulePublish')
// ->addArgument('action', Argument::OPTIONAL, "action", '')
// ->addArgument('force', Argument::OPTIONAL, "force", '');
$this->setName('schedulePublish')
->addArgument('argv', Argument::OPTIONAL, "your argv")
->addOption('force', NULL, Option::VALUE_REQUIRED, 'your force')
->addOption('taskcommand', NULL, Option::VALUE_REQUIRED, 'your taskcommand')
->setDescription('测试专用');
}
protected function execute(Input $input, Output $output)
{
/**
$action = trim($input->getArgument('action'));
$force = trim($input->getArgument('force'));
// 添加日志记录
Log::write('Task started. Action: ' . $action . ', Force: ' . $force);
$task = new \\EasyTask\\Task();
$task->setRunTimePath('./runtime/');
//// 配置任务,每隔20秒访问2次
$task->addFunc(function () {
// $url = 'https://www.xxx.cn/?id=327';
// file_get_contents($url);
Log::write('addFuncTask started successfully.');
}, 'request', 20, 2);
// if ($action == 'start') {
// $task->start();
// // 添加日志记录
// Log::write('Task started successfully.');
// } elseif ($action == 'status') {
// $task->status();
// // 添加日志记录
// Log::write('Task status checked.');
// } elseif ($action == 'stop') {
// $force = ($force == 'force');
// $task->stop($force);
// // 添加日志记录
// Log::write('Task stopped. Force: ' . ($force ? 'Yes' : 'No'));
// } else {
// // 添加日志记录
// Log::write('Command is not exist');
// exit('Command is not exist');
// }
**/
$argv = trim($input->getArgument('argv'));
$force = $input->hasOption('force') ?: false;
$taskcommand = $input->hasOption('taskcommand') ? $input->getOption('taskcommand') : "empty";
Log::write('Task started. argv: ' . $argv . ', Force: ' . $force);
$task = new \\EasyTask\\Task();
// 设置常驻内存
$task->setDaemon(true);
// 设置项目名称
$task->setPrefix('ThinkTask');
// 设置系统时区
$task->setTimeZone('Asia/Shanghai') ;
// 设置子进程挂掉自动重启
$task->setAutoRecover(true) ;
$task->setRunTimePath('./runtime/');
switch ($taskcommand) {
case 'timego':
$task->addFunc(function () {
$now = date("Y-m-d H:i:s");
$logs= "[{$now}] It's timego now" . PHP_EOL;
Log::write('1、addFuncTask started successfully.'.$logs);
}, $taskcommand, 10, 1); // 1.添加闭包函数类型定时任务(开启2个进程,每隔10秒执行1次)
break;
default:
$task->addFunc(function () {
$now = date("Y-m-d H:i:s");
$logs= "[{$now}] it is empty taskcommand now " . PHP_EOL;
Log::write('2、addFuncTask started successfully.'.$logs);
}, $taskcommand, 30, 1); // 1.添加闭包函数类型定时任务(开启1个进程,每隔30秒执行1次)
}
switch ($argv) {
case 'start':
Log::write('3.1、EasyTask start ');
//初始化
$task->start();
// Log::write('3.2、EasyTask start ');
// // 设置非常驻内存
// $task->setDaemon(true);
// // 设置项目名称
// $task->setPrefix('EasyTask');
// 设置记录运行时目录(日志或缓存目录)
// $task->setRunTimePath(root_path() . 'runtime' . DIRECTORY_SEPARATOR);
//$output->writeln("EasyTask start {$taskcommand}");
Log::write('3.3、EasyTask start ');
break;
case 'status':
$task->status();
//$output->writeln("EasyTask status {$taskcommand}");
Log::write('4、EasyTask status');
break;
case 'stop':
$task->stop($force);
// $output->writeln("EasyTask stop {$force} {$taskcommand}");
Log::write('5、EasyTask stop ');
break;
default:
// exit('argv is not exist' . PHP_EOL);
Log::write('6、argv is not exist');
}
return true;
}
}在项目 config/console.php 中的’commands’中注册命令
<?php // +---------------------------------------------------------------------- // | 控制台配置 // +---------------------------------------------------------------------- return [ // 指令定义 'commands' => [ 'schedulePublish' => 'app\\command\\Task', ], ]; 开启调度 php think schedulePublish start 执行timego: php think schedulePublish start --taskcommand timego 参数: pid:任务进程 id name:任务别名 started:任务启动时间 time:任务执行时间 status:任务状态 ppid:守护进程id 启动任务: php console.php start 查询任务: php console.php status 普通关闭: php console.php stop 强制关闭: php console.php stop force 守护执行: $task->setDaemon(true)
执行任务timego:
php think schedulePublish start --taskcommand timego
停止任务
php think schedulePublish stop --taskcommand timego
野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892

- 上一篇:【内部资料】thinkphp6 创建定时任务
- 下一篇:C语言树状结构遍历
