casperengl/cron-ms

Runs cron jobs based on milliseconds, instead of minutes, hours, days, weeks and years

dev-master 2020-06-03 05:08 UTC

This package is auto-updated.

Last update: 2024-03-29 21:55:45 UTC


README

https://scrutinizer-ci.com/g/CasperEngl/cron-ms?b=master https://scrutinizer-ci.com/g/CasperEngl/cron-ms?b=master

This library makes it easy to run a piece of code, every X milliseconds or seconds.

// Initialization and running

// From milliseconds
$cron = CronMs::fromMs(5000, function ($i /* each iteration count is returned here, start at 0 */) {
    // ... your code
});

// From seconds
$cron = CronMs::fromSeconds(5, function ($i /* each iteration count is returned here, start at 0 */) {
    // ... your code
});


// Initialization only

// You can also start the process yourself later.
// Just pass false as the third (3rd) parameter
$cron = CronMs::fromMs(5000, function ($i /* each iteration count is returned here, start at 0 */) {
  // ... your code
}, false /* disables automatic run */);

// Some other code

// Some time later
$cron->run();


// Properties

// You can access `$ms`, `$start_timestamp` and `$startTime` from the CronMs instance
$cron = CronMs::fromMs(5000, function ($i /* each iteration count is returned here, start at 0 */) {
  // ... your code
});

$cron->ms // 5000
$cron->start_timestamp // unix timestamp
$cron->start // Carbon\Carbon instance

Testing

Run the ./vendor/bin/phpunit (optionally with the --testdox parameter, for pretty output). Be aware, tests are longrunning. This is because each test runs the u_sleep() method internally.