Install the Bundle
composer require edgar/cron-bundle
Register the bundle in config/bundles.php:
return [
// ...
Edgar\CronBundle\EdgarCronBundle::class => ['all' => true],
];
Define a Cron Job
Create a command extending AbstractCron (located in src/Command/):
namespace App\Command;
use Edgar\CronBundle\Command\AbstractCron;
class MyCronJob extends AbstractCron
{
protected static $defaultName = 'app:my_cron_job';
protected static $cron = '* * * * *'; // Default cron expression
protected static $priority = 10; // Lower = higher priority
protected function execute($input, $output)
{
// Your logic here
$output->writeln('Running MyCronJob!');
}
}
Tag the Command as a Service
In config/services.yaml:
services:
App\Command\MyCronJob:
tags: ['edgar.cron']
Set Up the Cron Job
Add this to your server’s crontab (replace <project_root>):
* * * * * cd <project_root> && php bin/console edgar:crons:run
Test Locally Run manually to verify:
php bin/console edgar:crons:run
Command Organization
Group cron jobs by domain (e.g., App\Command\Billing\, App\Command\Reports\) for clarity.
Example:
namespace App\Command\Reports;
class GenerateMonthlyReport extends AbstractCron { ... }
Dynamic Cron Expressions
Override $cron per environment (e.g., dev vs. prod) using dependency injection:
class MyCronJob extends AbstractCron
{
protected static $cron;
public function __construct(string $cronExpression)
{
self::$cron = $cronExpression;
}
}
Configure in config/services.yaml:
services:
App\Command\MyCronJob:
arguments: ['%env(CRON_EXPRESSION)%']
tags: ['edgar.cron']
Priority Management
Use priorities to control execution order (e.g., 0 for critical jobs, 100 for low-priority):
protected static $priority = 5; // Mid-priority
Logging and Monitoring
Extend execute() to log output or integrate with Laravel’s logging:
protected function execute($input, $output)
{
\Log::info('MyCronJob started');
try {
// Job logic
} catch (\Exception $e) {
\Log::error('MyCronJob failed: ' . $e->getMessage());
}
}
Environment-Specific Jobs
Disable jobs in certain environments by checking $this->getEnvironment():
protected function execute($input, $output)
{
if ($this->getEnvironment() !== 'production') {
return;
}
// Production logic
}
Cron Expression Syntax
* * * * * *) will silently fail. Validate with tools like crontab.guru.protected static $cron = '0 0 * * 0'; // Weekly on Sunday at midnight
Service Tagging
edgar.cron will make it invisible to the scheduler.php bin/console debug:container edgar.cron
Priority Collisions
1, 2, 3) for critical sequences.Time Zone Issues
APP_TIMEZONE in .env:
APP_TIMEZONE=UTC
Debugging Execution
execute() to trace issues:
$output->writeln('Debug: ' . print_r($this->getArguments(), true));
tail -f storage/logs/laravel.log
Dry Runs
Use --dry-run to list jobs without executing:
php bin/console edgar:crons:run --dry-run
Custom Scheduling Logic
Override shouldRun() to add conditions:
protected function shouldRun(): bool
{
return parent::shouldRun() && $this->isWeekend();
}
private function isWeekend(): bool
{
return date('N') >= 6; // Saturday (6) or Sunday (7)
}
Dependency Injection Inject services (e.g., repositories) into cron jobs:
class MyCronJob extends AbstractCron
{
private $repository;
public function __construct(MyRepository $repository)
{
$this->repository = $repository;
}
}
Testing Cron Jobs
Mock the edgar:crons:run command in PHPUnit:
$this->artisan('edgar:crons:run')
->expectsQuestion('Run all crons?', 'yes')
->assertExitCode(0);
Performance Considerations
php.ini or via set_time_limit():
set_time_limit(300); // 5 minutes
How can I help you explore Laravel packages today?