shapecode/cron-bundle
Laravel-friendly cron scheduler bundle that lets you define, manage, and run scheduled jobs from your app. Provides an easy API for registering tasks, configuring frequency and conditions, and executing commands reliably with clear organization and control.
Installation
composer require shapecode/cron-bundle
Add to config/app.php under providers:
ShapeCode\CronBundle\CronBundle::class,
Publish the config (optional):
php artisan vendor:publish --provider="ShapeCode\CronBundle\CronBundle" --tag="config"
Basic Registration
Define a command (e.g., app/Console/Commands/ExampleCommand.php):
namespace App\Console\Commands;
use Illuminate\Console\Command;
class ExampleCommand extends Command
{
protected $signature = 'example:task';
protected $description = 'An example scheduled task';
}
Register it in config/cron.php:
'commands' => [
'App\Console\Commands\ExampleCommand' => '*/5 * * * *', // Every 5 minutes
],
First Run
php artisan cron:run
Or use a system cron job:
* * * * * cd /path-to-project && php artisan cron:run >> /dev/null 2>&1
Dynamic Registration: Register commands programmatically in a service provider:
public function boot()
{
$this->app->make('ShapeCode\CronBundle\CronManager')->addCommand(
\App\Console\Commands\ExampleCommand::class,
'*/10 * * * *' // Every 10 minutes
);
}
Environment-Specific Scheduling: Use config overrides:
// config/cron.php (production)
'commands' => [
'App\Console\Commands\HeavyTask' => '0 3 * * *' // Nightly
],
php artisan cron:run >> storage/logs/cron.log 2>&1
CronManager to log missed jobs:
$manager->setLogger(new \Monolog\Logger('cron'));
class ExampleCommand extends Command
{
public function handle()
{
dispatch(new \App\Jobs\ProcessData);
}
}
config/cron.php:
'timezone' => 'America/New_York',
CronManager in tests:
$manager = $this->app->make('ShapeCode\CronBundle\CronManager');
$manager->run(); // Simulate cron execution
php artisan cron:run) will prevent tasks from running.* * * * *) may run concurrently if not idempotent.timezone in config to avoid surprises.storage/logs/cron.log for errors.php artisan cron:run --dry-run (if supported).php artisan cron:list to verify registered commands.ShapeCode\CronBundle\Schedule\Schedule to add custom syntax (e.g., "every 2 hours").CronManager to add logic before/after command execution:
$manager->beforeRun(function () {
Log::info('Cron jobs starting...');
});
lazy option in config to defer command loading:
'commands' => [
'App\Console\Commands\LazyCommand' => [
'schedule' => '* * * * *',
'lazy' => true,
],
],
php artisan cron:run is only executable by trusted users.if (app()->environment('local')) {
$this->info('Skipping in local environment.');
return;
}
How can I help you explore Laravel packages today?