senman/yii2-crontab-manager

更方便地管理您的定时任务,实时暂停、启动、修改定时任务的执行时间等等。使用本包,您可以更好地管理您多样的定时任务,只需定义一个执行入口,通过操作数据库数据实现定时、开启、关闭、设定启动时间等更灵活的管理。

dev-master 2018-08-03 01:31 UTC

This package is not auto-updated.

Last update: 2024-09-21 04:01:26 UTC


README

Yii-crontab-manager

使用本包,您可以更好地管理您多样的定时任务,只需定义一个执行入口,通过操作数据库数据实现定时、开启、关闭、设定启动时间等更灵活的管理。

描述:任务管理器实现了多进程的灵活性。任务开启后,每个任务会占用一个子进程,完成后自动释放。

准备:安装扩展

为了实现PHP的多进程,我们需要两个扩展pcntl和posix,可以在编译时添加:–enable-pcntl,不使用–disable-pcntl,posix是默认安装的。PHP多进程模块依赖于pcntl扩展。

开始使用

通过这个包,您可以更好地管理您繁杂的定时任务,只需定义一个执行入口即可,通过操作数据库数据实现定时、开启、关闭、设定启动时间等等更灵活的管理。

说明:该任务管理器实现是利用多进程的灵活性,在任务开启后,每个任务占用一个子进程,完成后自动释放。

准备:安装扩展

要实现PHP的多进程,我们需要两个扩展pcntl和posix,在编译时可以加入:–enable-pcntl,不要使用–disable-pcntl,posix是默认安装的。php多进程模块依赖pcntl扩展。

开始使用:

1、安装 Install

composer require senman/yii2-crontab-manager dev-master

2、执行数据库导入

将vender/yii2-crontab-manager/目录下的 cron-manager.sql导入到数据库或者执行 yii migrate 命令导入数据库

从vender/yii2-crontab-manager/目录导入cron-manager.sql到数据库

3、添加任务 Add tasks

name: 任务名称 Task name

remark:备注

status:任务状态,1:正常运行,0:终止运行 Task state, 1: normal operation, 0: terminate operation

start_time:开始运行时间 Start running time

interval_time:间隔时间,具体单位取决于type的类型 Interval time, the specific unit depends on the type of type

type:字段含义 0:间隔时间单位为秒,1:间隔时间单位为分钟,2:间隔时间单位为小时,3:间隔时间单位为天,4:间隔时间单位为周,5:间隔时间单位为月

0: 间隔时间单位为秒,1: 间隔时间单位为分钟,2: 间隔时间单位为小时,3: 间隔时间单位为天,4: 间隔时间单位为周,5: 间隔时间单位为月。

path:执行路径 Execution path

module:所属模块 Subordinate module

sort:任务执行的优先顺序,数字越大,越先执行 The priority of task execution, the greater the number, the more advanced.

create_time:创建时间 Creation time

将任务添加到数据库 Add the task to the database

   $cron_config_model=new CronConfig();
   
   $cron_config_model->name="短信批量发";
   
   $cron_config_model->remark="这是短信批量发";
   
   $cron_config_model->status=1;
   
   $cron_config_model->start_time="2018-05-07 00:15:00";
   
   $cron_config_model->interval_time="1";
   
   $cron_config_model->type="1";
   
   $cron_config_model->path="email/send";
   
   $cron_config_model->module="email";
   
   $cron_config_model->sort="100";
   
   $cron_config_model->create_time="2018-05-28 11:53:19";
   
   $cron_config_model->save();
   

3、编写脚本入口 Write the entry of the script

在项目根目录的console\controllers目录创建CrontabController.php文件,如下: Create the CrontabController.php file in the console\controllers directory of the project root directory, as follows

namespace console\controllers;

use Crontab\models\CronConfig;

use yii\console\Controller;

class CrontabController extends Controller{

    public function actionCrontab(){
    
        CronConfig::run();
        
        }
   } 
 

4、执行任务 Carry out the task

执行命令:php yii crontab/crontab 即可开始按照预设条件进行任务处理

执行命令:PHP Yii crontab/crontab

根据预设条件开始处理任务

5、建议将该命令加入到Linux的定时任务中,根据需要设定运行间隔时间,例如每1分钟运行一次

6、若中途想修改执行的时间则需要执行下列步骤:

1)修改该任务的cron_config表里的last_run_time;

7、如有任何疑问欢迎加入QQ群:338461207 进行交流 if you have any questions, welcome to join QQ group: 338461207