workerman/crontab

Description

workerman/crontab is a scheduling task program based on Workerman, similar to Linux's crontab. workerman/crontab supports second-level scheduling.

To use workerman/crontab, you need to set the PHP time zone first; otherwise, the execution results may differ from expectations.

Time Explanation

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)[can be omitted; if the 0 position is absent, the minimum time granularity is in minutes]

Installation

composer require workerman/crontab

Example

<?php
use Workerman\Worker;
require __DIR__ . '/vendor/autoload.php';

use Workerman\Crontab\Crontab;
$worker = new Worker();

// Set time zone to avoid inconsistencies with expected results
date_default_timezone_set('PRC');

$worker->onWorkerStart = function () {
    // Execute at the 1st second of every minute.
    new Crontab('1 * * * * *', function(){
        echo date('Y-m-d H:i:s')."\n";
    });
    // Execute at 7:50 AM every day; note that the seconds field is omitted here.
    new Crontab('50 7 * * *', function(){
        echo date('Y-m-d H:i:s')."\n";
    });
};

Worker::runAll();

Note
Scheduled tasks will not execute immediately; all scheduled tasks start counting down to execution in the next minute.
Like Workerman's timer, all tasks are executed in the current process.
If the Swoole/Swow coroutine is not enabled, if a task is not completed by the next execution period, it will wait for the current task to finish, resulting in a delay for the new task.
If Swoole/Swow coroutines are enabled, if the previous task is not completed, the next task may execute immediately without waiting in line.

API

Crontab::destroy()

Destroy the timer

$crontab = new Crontab('1 * * * * *', function(){
    echo date('Y-m-d H:i:s')."\n";
});
$crontab->destroy();