sumfen / easy-task
easy-task,简单定时器,定时任务
v2.4.5
2023-02-15 02:52 UTC
Requires
- php: >=5.4
- ext-curl: *
- ext-json: *
- ext-mbstring: *
Suggests
- ext-event: For better performance.
This package is auto-updated.
Last update: 2024-09-15 07:01:49 UTC
README
基于 easy-task/easy-task,去掉 thinkphp3.2.3.php
EasyTask 是一个易于使用的 PHP 常驻内存定时任务包
项目介绍
EasyTask 是一个 PHP 常驻内存计时器 Composer 包,与 Workerman 计时器效果相同,多个计时器可以在多个进程中同时运行,你可以用它来完成需要重复执行的任务(例如自动取消订单超时、异步发送短信邮件、队列/消费者/通道订阅者等),甚至可以处理 Crontab 定时任务(例如每天凌晨 1 点到 3 点同步 DB 数据、每月 1 日生成月度统一报表、每晚 10 点重启 nginx 服务器等);内置任务异常报告功能,你可以自定义异常错误的处理方式(例如自动短信通知异常错误);它还支持异常退出任务的自动重启,使你的任务运行更稳定,工具包支持 windows、linux 和 mac 环境的运行。
运行环境
Composer 安装
composer require easy-task/easy-task
【一】. 快速入门→创建任务
// init
$task = new Task();
// set up resident memory
$task->setDaemon(false);
// set project name
$task->setPrefix('EasyTask');
// set the logging runtime directory (log or cache directory)
$task->setRunTimePath('./Application/Runtime/');
// add closure function type timed task (open 2 processes, execute once every 10 seconds)
$task->addFunc(function () {
$url = 'https://www.gaojiufeng.cn/?id=243';
@file_get_contents($url);
}, 'request', 10, 2);
// add class method type timing task (also supports static methods) (start 1 process, execute once every 20 seconds)
$task->addClass(Sms::class, 'send', 'sendsms', 20, 1);
// add instruction-type timing tasks (start a process and execute it every 10 seconds)
$command = 'php /www/web/orderAutoCancel.php';
$task->addCommand($command,'orderCancel',10,1);
// add a closure function task, do not need a timer, execute immediately (open 1 process)
$task->addFunc(function () {
while(true)
{
//todo
}
}, 'request', 0, 1);
// start task
$task->start();
【二】. 快速入门→协同操作
$task = new Task();
// Set non-resident memory
$task->setDaemon(false)
// set project name
->setPrefix('ThinkTask')
// set system time zone
->setTimeZone('Asia/Shanghai')
// set the child process to hang up and restart automatically
->setAutoRecover(true)
// set the PHP running path, which is usually required for the Window system. You need to set it manually when the system cannot be found.
->setPhpPath('C:/phpEnv/php/php-7.0/php.exe')
/**
* set the logging runtime directory (log or cache directory)
*/
->setRunTimePath('./Application/Runtime/')
/**
* set to turn off standard output STD file recording
*/
->setCloseStdOutLog(true);
/**
* Close EasyTask's exception registration
* EasyTask will no longer listen to set_error_handler / set_exception_handler / register_shutdown_function events
*/
->setCloseErrorRegister(true)
/**
* set to receive errors or exceptions during operation (Mode 1)
* you can customize the handling of abnormal information, such as sending them to your emails, SMS, as an early warning
* (Not recommended, unless your code is robust)
*/
->setErrorRegisterNotify(function ($ex) {
//Get error information | error line | error file
$message = $ex->getMessage();
$file = $ex->getFile();
$line = $ex->getLine();
})
/**
* set the Http address to receive errors or exceptions in operation (Method 2)
* EasyTask will notify this URL and pass the following parameters:
* errStr:errStr
* errFile:errFile
* errLine:errLine
* your Url receives a POST request and can write code to send an email or SMS to notify you
* (Recommended wording)
*/
->setErrorRegisterNotify('https://www.gaojiufeng.cn/rev.php')
// add task to execute closure function regularly
->addFunc(function () {
echo 'Success3' . PHP_EOL;
}, 'fucn', 20, 1)
// add a method for task execution class
->addClass(Sms::class, 'send', 'sendsms1', 20, 1)
// add tasks to execute commands regularly
->addCommand('php /www/wwwroot/learn/curl.php','cmd',6,1)
// start task
->start();
【三】. 快速入门→命令集成
// get command
$force = empty($_SERVER['argv']['2']) ? '' : $_SERVER['argv']['2'];
$command = empty($_SERVER['argv']['1']) ? '' : $_SERVER['argv']['1'];
// configuration tasks
$task = new Task();
$task->setRunTimePath('./Application/Runtime/');
$task->addFunc(function () {
$url = 'https://www.gaojiufeng.cn/?id=271';
@file_get_contents($url);
}, 'request', 10, 2);;
// execute according to the order
if ($command == 'start')
{
$task->start();
}
elseif ($command == 'status')
{
$task->status();
}
elseif ($command == 'stop')
{
$force = ($force == 'force'); //whether to force stop
$task->stop($force);
}
else
{
exit('Command is not exist');
}
Start task: php console.php start
Query task: php console.php status
Stop Task: php console.php stop
Force close task: php console.php stop force
【四】. 快速入门→理解输出信息
┌─────┬──────────────┬─────────────────────┬───────┬────────┬──────┐
│ pid │ name │ started │ time │ status │ ppid │
├─────┼──────────────┼─────────────────────┼───────┼────────┼──────┤
│ 32 │ Task_request │ 2020-01-10 15:55:44 │ 10 │ active │ 31 │
│ 33 │ Task_request │ 2020-01-10 15:55:44 │ 10 │ active │ 31 │
└─────┴──────────────┴─────────────────────┴───────┴────────┴──────┘
参数:
pid:task process id
name:task alias
started:task start time
time:task execution time
status:task status
ppid:daemon id
【五】. 高级理解→推荐阅读
(1). It is recommended that you use the absolute path for development, which is the standard and the norm
(2). It is forbidden to use exit / die syntax in the task, otherwise it will cause the entire process to exit
(3). Please close anti-virus software when installing Wpc extension in Windows to avoid false alarms
(4). Windows recommends to open shell_exec method, it will automatically try to help you solve the problem of CMD output Chinese garbled, please try to use CMD administrator mode
(5). Windows command line does not support utf8 international standard encoding, you can switch git_bash to run, solve the garbled problem
(6). Windows prompts Failed to create COM object `Wpc.Core ': invalid syntax, please follow the documentation to install the Wpc extension
(7). Windows prompt com () has been disabled for security reasons, please delete disable_classes = com configuration item in php.ini
(8). The log file is in the Log directory of the runtime directory, and the input and output abnormal files are marked in the Std directory of the runtime directory
(9). Normally stop the task, the task will start to exit safely after the execution is successful, force the task to quit the task directly, and may quit when it is being executed
(10). The development follows the synchronous start test, normal operation without any errors, and then the asynchronous operation. If there is a problem, check the log file or the standard input and output abnormal file, or feedback on the QQ group.
【六】. 高级理解→框架集成教程
【七】. 高级理解→推荐操作
(1). It is recommended to use PHP version 7.1 or above, which supports asynchronous signals and does not depend on ticks
(2). It is recommended to install php_event to extend the millisecond timing support based on event polling
【八】. 高级理解→时间参数支持 crontab 命令
Since the 2.3.6 version to reduce maintenance work, Crontab support has been removed, please use PHP's own time functionDateTime class for processing.
For example, it only needs to be executed at 20 o'clock every night, and it is not necessary to execute Return at 20 o'clock.
$task->addFunc(function () {
$hour = date('H');
if ($hour != 20)
{
return;
}
//Write your code
},'request', 1, 1);
【九】. 特别感谢
(1) ThinkPHP (the official extension page shows EasyTask), official address: http://www.thinkphp.cn/
(2) ThinkPHP (command line output component based on Tp_Table component), official address: http://www.thinkphp.cn/
(3) Jetbrains (provide genuine authorization code, support genuine), official address: https://www.jetbrains.com/phpstorm/
【十】. 缺陷反馈
Please feedback to QQ group 777241713, thanks to the users who continue to feedback, your feedback makes EasyTask more and more stable!