buzzingpixel / craft-scheduler
Schedule tasks to run in Craft
Installs: 209
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
Type:craft-plugin
Requires
- php: ^8.0
- psr/container: ^1.1
- psr/log: ^1.0
Requires (Dev)
- codedungeon/phpunit-result-printer: ^0.31.0
- craftcms/cms: ^3.7
- doctrine/coding-standard: ^9
- friendsofphp/php-cs-fixer: ^2.18
- php-di/php-di: ^6.3
- phpstan/phpstan: ^0.12.69
- phpstan/phpstan-deprecation-rules: ^0.12.6
- phpstan/phpstan-strict-rules: ^0.12.9
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.5
- symfony/var-dumper: ^5.3
- vimeo/psalm: ^4.4
This package is auto-updated.
Last update: 2024-11-13 23:13:56 UTC
README
If you would like to programmatically schedule tasks to run in Craft CMS, this is the module for you! Just set up a cron to call the command every minute, then anything you schedule to run by hooking into the event will get run on the schedule you define.
Here's how to use it:
- In your craft project, run
composer require buzzingpixel/craft-scheduler
- Then run
php craft plugin/install craft-scheduler
- Now hook into the
RetrieveSchedule
event to add items to the schedule
The RetrieveSchedule
event
The RetrieveSchedule
event is how you add runners to the schedule. Here's a demo:
use BuzzingPixel\CraftScheduler\ScheduleRetrieval\RetrieveSchedule; use BuzzingPixel\CraftScheduler\ScheduleRetrieval\RetrieveScheduleEvent; use BuzzingPixel\CraftScheduler\ScheduleRetrieval\ScheduleConfigItem; use BuzzingPixel\CraftScheduler\Frequency; use yii\base\Event; Event::on( RetrieveSchedule::class, RetrieveSchedule::EVENT_RETRIEVE_SCHEDULE, static function (RetrieveScheduleEvent $e): void { $e->scheduleConfigItems()->addItem(item: new ScheduleConfigItem( className: SomeClass::class, // The class to run runEvery: Frequency::ALWAYS, // How often to run it method: 'myOptionalMethod', // Specify method on the class to call, defaults to __invoke resolveWith: SomeContainer::class, // Optionally provide your own ContainerInterface implementation. Defaults to the Yii container (or whatever default container you specify, see below) )); $e->scheduleConfigItems()->addItem(item: new ScheduleConfigItem( className: SomeOtherClass::class, runEvery: Frequency::HOUR, )); } );
The SetDefaultContainer
event
The scheduler allows you to provide your own ContainerInterface implementation as the default. If no default is specified, Craft Scheduler's Yii Container implementation (a wrapper around the Yii container that implements the PSR ContainerInterface) will be used.
use BuzzingPixel\CraftScheduler\CraftSchedulerPlugin; use BuzzingPixel\CraftScheduler\ScheduleRetrieval\SetDefaultContainerEvent; use DI\ContainerBuilder; use Psr\Http\Message\ResponseFactoryInterface; use Psr\Http\Message\ResponseInterface; use Slim\App; use Slim\Psr7\Factory\ResponseFactory; use yii\base\Event; use function DI\autowire; Event::on( CraftSchedulerPlugin::class, CraftSchedulerPlugin::EVEN_SET_DEFAULT_CONTAINER, static function (SetDefaultContainerEvent $e) { $containerBuilder = (new ContainerBuilder()) ->useAnnotations(true) ->useAutowiring(true) ->ignorePhpDocErrors(true) ->addDefinitions([ ResponseFactoryInterface::class => autowire(ResponseFactory::class), ]); $container = $containerBuilder->build(); $e->setDefaultContainer($container); } );