PHP code example of wyrihaximus / react-cron

1. Go to this page and download the library: Download wyrihaximus/react-cron library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wyrihaximus / react-cron example snippets


use React\Promise\PromiseInterface;
use WyriHaximus\React\Cron;
use WyriHaximus\React\Cron\Action;

use function React\Promise\resolve;

Cron::create(
    new Action(
        'Hour', // Identifier used for mutex locking locking
        60, // TTL for the mutex lock, always set this way higher than the expected execution time, but low enough any failures during the run will cause issues
        '@hourly', // The cron expression used to schedule this action
        function (): PromiseInterface { // The callable ran when this action is due according to it's schedule
            echo 'Another hour has passed!', PHP_EOL;

            return resolve(true); // This callable MUST return a promise, which is used for releasing the mutex lock
        }
    ),
    new Action(
        'Minute',
        0.1,
        '* * * * *',
        function (): PromiseInterface {
            echo 'Another minute has passed!', PHP_EOL;

            return resolve(true);
        }
    )
);

// Stops scheduling new action runs
$cron->stop();

$cron = Cron::create(...$actions);
$cron->on('error', static function (Throwable $throwable): void {
    // Handle error
});