geckoboom / scheduler
Cron scheduler for console commands
1.0.5
2022-10-20 09:42 UTC
Requires
- php: >=7.4
- dragonmantank/cron-expression: ^3.1
- psr/container: ^1.0||^2.0
- psr/simple-cache: >=1.0
- symfony/process: >3.3.0
Requires (Dev)
- dg/bypass-finals: dev-master
- phpunit/phpunit: 9.5.x-dev
- squizlabs/php_codesniffer: 4.0.x-dev
README
Table of Contents
Introduction
Command scheduler is a great approach to managing scheduled console commands on the server. The package allows you to control your task scheduling within the console application. When using a scheduler, only a single cron entry is required on your server.
Installation
This is installable via Composer as This is installable via Composer as geckoboom/scheduler:
composer require geckoboom/scheduler
Configuration
Several steps required to configure scheduler
- Provide implementation of
Geckoboom\Scheduler\CallerInterface
.
class ReflectionCaller implements \Geckoboom\Scheduler\CallerInterface { protected ReflectionContainer $container; public function call(\Closure $callback){ $args = []; $reflectionMethod = new \ReflectionMethod($callback, '__invoke'); $args = []; foreach ($reflectionMethod->getParameters() as $parameter) { if (!$this->container->has($parameter->getName()) && $parameter->isDefaultValueAvailable()) { $args[$parameter->getName()] = $parameter->getDefaultValue(); } } return $this->container->call($callback, $args); } }
- Provide two implementations of
EventMutexInterface
andScheduleMutexInterface
. The package contains default realizations of these interfaces based onPsr\SimpleCache\CacheInterface
dependency. You can do it in such ways
$container->add( \Psr\SimpleCache\CacheInterface::class, MyCacheImplementation::class ); $container->add( \Geckoboom\Scheduler\EventMutexInterface::class, \Geckoboom\Scheduler\EventMutex\CacheEventMutex::class ); $container->add( \Geckoboom\Scheduler\ScheduleMutexInterface::class, \Geckoboom\Scheduler\ScheduleMutex\CacheScheduleMutex::class );
or create your own realizations
$container->add( \Geckoboom\Scheduler\EventMutexInterface::class, MyEventMutexImplementation::class ); $container->add( \Geckoboom\Scheduler\ScheduleMutexInterface::class, MyScheduleMutexImplementation::class );
- Provide your
ScheduleRegistrarInterface
dependency.
class MyScheduleRegistrar implements \Geckoboom\Scheduler\ScheduleRegistrarInterface { public function schedule(\Geckoboom\Scheduler\Schedule $schedule) : void { $schedule->command('send:emails', ['--option1' => 'value', 'argument1']) ->daily() ->withoutOverlapping(); $schedule->command('orders:distribute') ->mondays() ->at('14:00') ->runInBackground(); } } ... $container->add( \Geckoboom\Scheduler\ScheduleRegistrarInterface::class, MyScheduleRegistrar::class );
- Provider
Schedule
singleton dependency
$container->addShared( \Geckoboom\Scheduler\Schedule::class, function ($di): \Geckoboom\Scheduler\Schedule { $schedule = new \Geckoboom\Scheduler\Schedule( $di->get(\Geckoboom\Scheduler\EventMutexInterface::class), $di->get(\Geckoboom\Scheduler\ScheduleMutexInterface::class), $di->get(\Geckoboom\Scheduler\CommandBuilder::class), '/path/to/project/root', new DateTimeZone('Europe/London') ); $registrar = $di->get(\Geckoboom\Scheduler\ScheduleRegistrarInterface::class); $registrar->schedule($schedule); return $schedule; } );
Usage
Now you can create executable script or console command based on the appropriate framework syntax.
$service = $container->get(\Geckoboom\Scheduler\ScheduleService::class); $service->run(new \DateTimeImmutable());
The last step is to add a single cron configuration entry to our server that runs your executable command every minute.
* * * * * cd /path-to-your-project && php /path-to-executable-script >> /dev/null 2>&1