【内部资料】thinkphp6 创建定时任务
作者:野牛程序员:2023-11-24 13:08:42php阅读 2406
1、.安装tp6
composer create-project topthink/think tp
2、安装定时任务composer包
composer require easy-task/easy-task
3、创建命令行处理类文件
php think make:command Task schedulePublish
会生成文件:tp\\app\\command\\Task.php
将Task.php文件内容修改如下:
<?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; } }
4、配置tp\\config\\console.php文件
<?php // +---------------------------------------------------------------------- // | 控制台配置 // +---------------------------------------------------------------------- return [ // 指令定义 'commands' => [ //'task' => 'app\\command\\Task', 'schedulePublish' => 'app\\command\\Task', ], ];
5、执行命令(windows请使用cmd):
php think schedulePublish start 启动命令 php think schedulePublish status 查询命令 php think schedulePublish stop 关闭命令 php think schedulePublish stop force 强制关闭命令
执行任务timego:
php think schedulePublish start --taskcommand timego
停止任务
php think schedulePublish stop --taskcommand timego
野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892