tp5 taskphp.php,TP5 定时任务

TP5+windows 定时任务

1.先写代码:

根据个人需求在某个模块下建立command文件夹,与controller文件夹同级。新建php文件,命名随意。

d99b239c86d2

TIM截图20190404130956.png

Task.php:

/**

* Created by PhpStorm.

* User: Administrator

* Date: 2019/2/22

* Time: 14:57

*/

namespace app\index\command;

use think\console\Command;

use think\console\Input;

use think\console\Output;

class Task extends Command{

protected function configure(){

$this->setName('Task')->setDescription("每天统计数据");//这里的setName和php文件名一致,setDescription随意

}

/*

* 报表-全局统计

*/

protected function execute(Input $input, Output $output)

{

//这里写业务逻辑

}

}

然后找到command.php,它在这个位置

d99b239c86d2

1.png.png

command.php:

// +----------------------------------------------------------------------

// | ThinkPHP [ WE CAN DO IT JUST THINK ]

// +----------------------------------------------------------------------

// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.

// +----------------------------------------------------------------------

// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )

// +----------------------------------------------------------------------

// | Author: yunwuxin <448901948@qq.com>

// +----------------------------------------------------------------------

return [

'app\index\command\Task' //这里把上面的任务php文件填上

];

2.windows做计划任务

搞个.bat脚本

D://进入D盘

cd \phpEnv\www\web\jingang //cd 命令后面是项目文件根目录

D:\phpEnv\php\php-5.6.36-nts\php.exe think Task// 用php.exe打开think Task任务

以win10为例,做计划任务

d99b239c86d2

image.png

d99b239c86d2

image.png

d99b239c86d2

image.png

新建触发器,就是定时,指定任务何时触发

d99b239c86d2

image.png

新建操作-添加bat脚本文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
taskPHP taskPHP基于php开发的定时计划任务框架,利用多进程实现任务的分配和运行,利用内存共享实现进程间通信,支持多线程模式需要安装pthreads扩展(可选),支持linux和windows。有较好的伸缩性、扩展性、健壮稳定性而被多家公司使用,同时也希望开源爱好者一起贡献。   框架概况 框架目录结构: taskPHP 根目录 |-- core 框架系统目录 | |-- lib 框架核心文件目录 | | |-- .... 众多的框架核心类库文件 | |-- guide.php 框架引导文件 | |-- distribute_listen.php 任务派发进程入口 | |-- worker_listen.php 任务执行进程入口 |-- docs 开发文档存放目录 |-- logs 日志目录 |-- tasks 用户任务目录 | |-- demo demo任务 | | |-- Lib demo任务的扩展目录 | | |-- demoTask.php demo任务类文件 | | |-- config.php demo任务配置文件 | | ... 更多任务 | |-- config.php 全局配置文件 |-- main.php 框架入口文件 |-- windows_single.cmd windows快速启动文件 框架说明 linux下子进程执行任务,修改脚本无需重启后台服务立即生效,windows下修改任务脚本后需重启后台脚本 但往系统添加执行不受影响。 框架支持多线程模式,需要安装pthreads扩展(可选)。 使用内存共享实现进程通信,堵塞式消息队列,整个框架的运行无需第三方扩展。 任务派发及具体任务执行不在同个进程[distribute_listen.php]和[worker_listen.php],windows和linux下启用入口文件[main.php],windows下可运行[windows_single.cmd]快速启动。 执行时间语法跟crontab类似,且支持秒设置。 添加任务简单,只需继承Task基类,实现任务入口run方法。 环境要求 php版本>= 5.5 开启shmop 注意事项 由于任务存在派发时间,所以任务运行的时间可能会有1-2秒的误差。 windows下执行任务在循环里,编写任务有问题或调用exit将导致后台脚本停止,linux下无此问题。 建议生产部署在linux下运行多进程模式,因为运行在多线程模式运行一段时间后报错,pthreads has detected that the core\lib\Pthread could not be started, the system lacks the necessary resources or the system-imposed limit would be exceeded in xxx 文档列表 -->数据库类使用教程 支持(Mysql,Mongo,Oracle,Pgsql,Sqlsrv,Sqllite) -->windows下安装php多线程扩展pthreads教程 -->工具类Utils使用说明 -->http请求客户端类Client使用说明 使用说明 时间配置格式说明: * * * * * * * //格式 :秒 分 时 天 月 年 周 10 * * * * * * //表示每一分钟的第10秒运行 /10 * * * * * * //表示每10秒运行 /1 * 15,16 * * * * //表示 每天的15点,16点的每一秒运行 系统命令说明: main.php [start] 启动 可不带参数 main.php close 结束 main.php reload 重新加载任务 main.php delete demo 删除任务 main.php select 查看任务列表 main.php exec demo 运行任务 主要用于任务开发中调试单个任务 全局配置文件规范 标签:taskphp  计划任务
您好!在使用TP5框架实现定时任务时,可以借助Redis来实现定时任务的调度和执行。具体实现步骤如下: 1. 在TP5中配置Redis连接: 在`config/database.php`文件中,配置Redis数据库的连接参数,例如: ```php 'redis' => [ 'type' => 'redis', 'hostname' => '127.0.0.1', 'port' => 6379, 'password' => '', 'select' => 0, 'timeout' => 0, 'expire' => 0, ], ``` 2. 创建定时任务类: 在`app\command`目录下创建一个继承自`think\console\Command`的PHP类,用于定义具体的定时任务逻辑。例如,创建一个名为`TestTask`的任务类,可以在类中定义一个`handle()`方法来执行具体的定时任务逻辑,如: ```php namespace app\command; use think\console\Command; use think\console\Input; use think\console\Output; class TestTask extends Command { protected function configure() { $this->setName('test:task')->setDescription('This is a test task'); } protected function execute(Input $input, Output $output) { // 执行定时任务逻辑 // ... $output->writeln('TestTask executed.'); } } ``` 3. 使用Redis实现定时任务调度: 在需要进行定时任务调度的地方,可以使用Redis的有序集合(Sorted Set)来存储任务信息,并结合Redis的定时器特性来进行调度。 - 添加定时任务: 在需要添加定时任务的地方,将任务信息添加到Redis的有序集合中,以任务的执行时间作为分值(score),以任务标识作为成员(member),例如: ```php $score = time() + $delayTime; // 计算任务的执行时间 $member = 'task:1'; // 任务标识 \think\facade\Redis::zAdd('tasks', $score, $member); // 添加任务到有序集合 ``` - 定时任务执行器: 创建一个定时任务执行器来监听Redis中的任务,并根据任务的执行时间进行调度和执行。例如,创建一个名为`TaskExecutor`的PHP类: ```php namespace app\command; use think\console\Command; use think\console\Input; use think\console\Output; class TaskExecutor extends Command { protected function configure() { $this->setName('task:execute')->setDescription('Task executor'); } protected function execute(Input $input, Output $output) { // 循环监听并执行定时任务 while (true) { // 获取当前时间戳 $now = time(); // 获取需要执行的定时任务列表 $tasks = \think\facade\Redis::zRangeByScore('tasks', 0, $now, ['withscores' => true]); // 执行定时任务 foreach ($tasks as $member => $score) { // 处理定时任务逻辑 // ... $output->writeln("Task $member executed."); // 从有序集合中删除已执行的任务 \think\facade\Redis::zRem('tasks', $member); } // 休眠1秒 sleep(1); } } } ``` 4. 配置定时任务命令: 在`app\command.php`文件中,注册定时任务相关的命令,例如: ```php return [ 'app\command\TestTask', 'app\command\TaskExecutor', ]; ``` 5. 启动定时任务执行器: 在命令行中运行以下命令启动定时任务执行器: ``` php think task:execute ``` 以上就是使用TP5和Redis实现定时任务的基本步骤,希望对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值