rkr/service-dispatcher

0.3.9 2024-11-15 19:02 UTC

README

Build Status Scrutinizer Code Quality Latest Stable Version License

Common usage using SQLite

A simple service dispatcher for shell scripts. The intent is to run a php-cli script every minute and let the script decide, what to run at what time...

use Kir\Services\Cmd\Dispatcher\Dispatcher;

require __DIR__ . '/vendor/autoload.php';

use Kir\Services\Cmd\Dispatcher\ServiceDispatcherBuilder;

$dispatcher = ServiceDispatcherBuilder::withSQLite(__DIR__.'/services.db')->build();

$dispatcher->register('service1', Dispatcher::ONE_HOUR, function () {
	// Do something
	throw new Exception();
});

$dispatcher->register('service2', Dispatcher::ONE_HOUR * 3, function () {
	// Do something
});

$dispatcher->run();

The example above show the simplest usage of the service dispatcher. Two services get registered. "Service1" and "Service2". If one service throws an exception, the whole execution stops. Next time, the failed service starts at the end of the queue. If a service was successfully executed, the timeout schedules the service in this case to 1 hour (3600 seconds) in the future.

MySQL-Specific settings:

use Kir\Services\Cmd\Dispatcher\ServiceDispatcherBuilder;

require __DIR__ . '/vendor/autoload.php';

/* ...  */

$dispatcher = ServiceDispatcherBuilder::withMySQL($pdo)
->useLocking(true)
->setLockPrefix('my-app:')
->build();

$dispatcher->register(/*...*/);

/* ...  */