本教程使用的定时任务基于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,记得用星星支持我们
开启错误提示代码:ini_set("display_errors", "On"); error_reporting(E_ALL | E_STRICT);关闭错误提示代码:error_reporting(E_ALL ^&n...
项目中使用服务来执行webservice,由于对方系统api不稳定,经常导致服务崩溃,只能重启,一个月差不多要重启一次。初期的解决办法是捕获异常,然后continue掉。<?php try { $url = 'http...
当我想在一个进程中监听kill 和 kill -9命令报了这个错误。//监听kill pcntl_signal(SIGTERM, function () { posix_kill(0, SIGTERM); });...
为什么使用队列?因为pop取队列具有原子性。假如我们需要秒杀一个商品id,我们先将商品的库存保存到一个队列。例如:<?php $redis = new Redis(); $redis->connect('127.0.0.1', 6...
场景:模拟验证码发送。仅做代码演示。(1).创建一个验证码发送接口sendCaptcha/** * 发送验证码 */ public function sendCaptcha() { //外部参数(获...
在编写thinkphp常驻内存的命令行应用中我们需要保证数据库连接不会断开,保证断开还能重新连接,因此大部分人的方案是直接修改tp的数据库配置文件database.php// 开启断线重连 'break_reconnect' => true,通常...