Crontab Scheduler Component

Description

workerman/crontab is similar to the Linux crontab, but it supports scheduling down to the second.

Time format:

0   1   2   3   4   5
|   |   |   |   |   |
|   |   |   |   |   +------ day of week (0 - 6) (Sunday=0)
|   |   |   |   +------ month (1 - 12)
|   |   |   +-------- day of month (1 - 31)
|   |   +---------- hour (0 - 23)
|   +------------ min (0 - 59)
+-------------- sec (0-59)[optional; if omitted, the minimum time granularity is minute]

Project Address

https://github.com/walkor/crontab

Installation

composer require workerman/crontab

Usage

Step 1: Create a process file app/process/Task.php

<?php
namespace app\process;

use Workerman\Crontab\Crontab;

class Task
{
    public function onWorkerStart()
    {

        // Execute once every second
        new Crontab('*/1 * * * * *', function(){
            echo date('Y-m-d H:i:s')."\n";
        });

        // Execute once every 5 seconds
        new Crontab('*/5 * * * * *', function(){
            echo date('Y-m-d H:i:s')."\n";
        });

        // Execute once every minute
        new Crontab('0 */1 * * * *', function(){
            echo date('Y-m-d H:i:s')."\n";
        });

        // Execute once every 5 minutes
        new Crontab('0 */5 * * * *', function(){
            echo date('Y-m-d H:i:s')."\n";
        });

        // Execute at the first second of each minute
        new Crontab('1 * * * * *', function(){
            echo date('Y-m-d H:i:s')."\n";
        });

        // Execute at 7:50 AM daily, note that the seconds field is omitted here
        new Crontab('50 7 * * *', function(){
            echo date('Y-m-d H:i:s')."\n";
        });

    }
}

Step 2: Configure process file to start with Webman

Open the configuration file config/process.php and add the following configuration

return [
    ....other configurations, omitted here....

    'task'  => [
        'handler'  => app\process\Task::class
    ],
];

Step 3: Restart Webman

Note: The scheduled tasks will not execute immediately; they will begin timing execution starting from the next minute.

Description

Crontab is not asynchronous. For example, if a task process has two timers A and B that both execute once per second, but task A takes 10 seconds, then task B must wait for task A to complete before it can be executed, resulting in a delay for task B.
If the business is sensitive to time intervals, sensitive scheduled tasks should be placed in separate processes to prevent them from being affected by other scheduled tasks. For example, the configuration in config/process.php should look like this:

return [
    ....other configurations, omitted here....

    'task1'  => [
        'handler'  => process\Task1::class
    ],
    'task2'  => [
        'handler'  => process\Task2::class
    ],
];

Place time-sensitive scheduled tasks in process/Task1.php and other scheduled tasks in process/Task2.php.

For more details on the configuration of config/process.php, please refer to Custom Processes