简体   繁体   English

PHP Cron作业引擎

[英]PHP Cron Job Engine

I was wondering how one would go about building a PHP Cron Job "engine". 我想知道如何构建一个PHP Cron Job“引擎”。

Essentially I am building a web app with "modules" which go and retrieve data at set intervals. 本质上,我正在使用“模块”构建一个Web应用程序,该模块可以按设置的时间间隔检索数据。 I want these modules to be separate, but they all need to gather data at the same time (roughly) - some are once a day, others every 15 minutes. 我希望这些模块是分开的,但是它们都需要同时(大约)同时收集数据-有些模块每天一次,其他每15分钟一次。

With several popular CMS's, you set up a cron job to hit a file every 5 minutes or so - what you can then do is "register" the cron job in each module rather than having to edit the cron tab every time you install a module. 使用几种流行的CMS,您可以设置一个cron作业每隔5分钟左右命中一个文件-然后,您可以在每个模块中“注册” cron作业,而不必在每次安装模块时都编辑cron选项卡。

Eg 例如

*/5 * * * * php /path/to/cron.php

In the module you could then do something like 在模块中,您可以执行以下操作

function task() {
    // do some work 
}

$cron->register(task())->daily();

Or similar - i'm not quite sure how to achieve it, nor am I sure what to search for! 或类似的结果-我不太确定该如何实现,也不确定该寻找什么!

Edit: I'm not looking for something that will edit the crontab, I am also trying to avoid an SQL database. 编辑:我不是在寻找可以编辑crontab的东西,我也在试图避免使用SQL数据库。 I know it won't happen magically and I'm happy to edit a master "file" to "install" a module. 我知道这不会神奇地发生,我很高兴编辑主“文件”以“安装”模块。

Edit 2: I suppose essentially what i'm looking for is like laravel do, but without the database... possible? 编辑2:我想我基本上在寻找像laravel一样,但是没有数据库...可能吗?

Hope it makes sense... 希望有道理...

After some more thinking, I managed to solve this. 经过一番思考,我设法解决了这个问题。

Each module now has a "cron" folder. 每个模块现在都有一个“ cron”文件夹。 The files in there represent the frequency of which to run (eg halfHourly.php or daily.php ). 那里的文件代表了运行的频率(例如halfHourly.phpdaily.php )。

I then have a PHP class that gets fired at regular intervals, with the type passed as a parameter (eg once a day the file gets fired with a "daily" parameter). 然后,我有一个PHP类,该类会定期触发,并将类型作为参数传递(例如,每天使用“每日”参数触发文件)。

Every hour, the hourly, halfHourly and quaterHourly ones run (using a switch statement you can ripple down). 每小时运行一次小时,半小时和四分之一小时(使用switch语句,您可以降低运行速度)。 So on and so fourth. 依此类推,第四。 The disadvantage is there are several cron statements but no database is needed! 缺点是有多个cron语句,但是不需要数据库!

Example of the code that runs 运行的代码示例

// This is done as a switch, so that some tasks can ripple down
switch ($argv[1]) {

    case 'daily':
        $cron->daily();

    case 'hourly':
        $cron->hourly();

    case 'halfHourly':
        $cron->halfHourly();

    case 'quarterHourly':
        $cron->quarterHourly();
    break;

    default:
        $cron->execute($argv[1]);
}

And the actual cron class: 和实际的cron类:

/**
 * Cron
 */
class Cron {
    private $modules;

    function __construct() {
        exec('ls -d /path/to/modules/*/cron', $files, $return);
        $this->modules = $files;
    }

    function execute($time) {
        $client = $GLOBALS['client'];

        echo PHP_EOL . "\033[1;32mProcessing the " . $time . " crons" . PHP_EOL;

        foreach($this->modules as $module) {
            $path = $module . '/' . $time . '.php';
            if(file_exists($path)) {
                echo PHP_EOL . "\033[1;34mExecuting module cron: \033[0;36m" . $module . PHP_EOL . "\033[0m";
                include $path;
            }
        }
    }

    public function __call($name, $arguments) {
        $this->execute($name);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM