Skip to content

Instantly share code, notes, and snippets.

@aaronbushnell
Last active February 1, 2024 20:48
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronbushnell/396f55acb9597c4e99397a9eace4f771 to your computer and use it in GitHub Desktop.
Save aaronbushnell/396f55acb9597c4e99397a9eace4f771 to your computer and use it in GitHub Desktop.
πŸ—“ Setting up omnilight/yii2-scheduling for flexible Craft task scheduling

πŸ“¦ Installation

  1. Run composer require omnilight/yii2-scheduling to install the scheduling package
  2. Setup a config/schedule.php file to house your scheduled processes
  3. Modify your config/app.php to include the schedule component details to run the Craft command (instead of yii)
  4. Run php craft schedule/run --scheduleFile=@config/schedule.php to process any scheduled operations.

πŸ’‘ Tip: You likely want to run php craft schedule/run --scheduleFile=@config/schedule.php on a cron job that fires every minute!

πŸ™ Credits

<?php
use craft\helpers\App;
return [
'*' => [
'id' => App::env('APP_ID') ?: 'CraftCMS',
'modules' => [],
'bootstrap' => [],
'components' => [
'schedule' => [
'class' => \omnilight\scheduling\Schedule::class,
'cliScriptName' => 'craft',
],
]
],
];
<?php
/** @var \omnilight\scheduling\Schedule $schedule */
$schedule->command('queue/run')
->description("Run Queue")
->everyMinute();
$schedule->command('seomatic/sitemap/generate')
->description("Generate sitemaps")
->weekdays()
->withoutOverlapping()
->onOneServer();
$schedule->call(function () {
Craft::$app->getQueue()->push(
new \modules\guide\jobs\TestJob()
);
})
->description("Run an arbitrary, complex test job")
->daily()
->skip(fn() => \USHolidays\Carbon::now()->isHoliday())
->onOneServer();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment