Installation:
composer require cron/cron-bundle
Register the bundle in config/bundles.php (Symfony 4.3+):
return [
// ...
Cron\CronBundle\CronCronBundle::class => ['all' => true],
];
Database Setup:
php artisan make:migration CreateCronJobsTable --table=cron_jobs
Run migrations:
php artisan migrate
First Use Case:
Define a job in a service class (e.g., app/Services/MyCronJob.php):
namespace App\Services;
use Cron\CronBundle\Job\JobInterface;
class MyCronJob implements JobInterface
{
public function run()
{
// Your logic here (e.g., send emails, clean cache)
\Log::info('My cron job executed!');
}
}
Register the job in config/packages/cron.yaml:
cron:
jobs:
my_job:
class: App\Services\MyCronJob
schedule: "*/5 * * * *" # Every 5 minutes
enabled: true
Test Manually:
php artisan cron:list # List all jobs
php artisan cron:run # Run all enabled jobs once
php artisan cron:run my_job # Run a specific job
Job Definition:
JobInterface in a service class (recommended for reusability).
class CleanupJob implements JobInterface {
public function run() {
// Business logic
}
}
cron:
jobs:
simple_job:
on: # Alternative to schedule (runs on demand)
run: |
\Log::info('Simple job ran!');
Scheduling:
config/packages/cron.yaml:
schedule: "0 0 * * *" # Daily at midnight
on option for event-driven triggers:
on:
command: "app:event-trigger"
event: "app.some_event"
Integration with Laravel:
AppServiceProvider:
$this->app->bind('App\Jobs\MyJob', function () {
return new MyJob();
});
schedule:run in a cron entry (wrap the bundle’s command):
* * * * * cd /path-to-project && php artisan schedule:run >> /dev/null 2>&1
Then define a scheduled job in app/Console/Kernel.php:
protected function schedule(Schedule $schedule) {
$schedule->command('cron:run')->everyMinute();
}
Logging and Monitoring:
config/packages/cron.yaml:
logging: true
php artisan cron:log
Database Locking:
run().release() calls (though JobInterface auto-handles this).*/5 * * * * timeout 300 php artisan cron:run).Timezone Mismatches:
# config/packages/cron.yaml
timezone: "Europe/Paris"
php artisan cron:list --verbose
Dependency Injection:
class MyJob implements JobInterface {
private $logger;
public function __construct(\Psr\Log\LoggerInterface $logger) {
$this->logger = $logger;
}
public function run() {
$this->logger->info('Job running');
}
}
App facade directly. Use interfaces (e.g., LoggerInterface).Migration Conflicts:
cron_jobs table, run:
php artisan cron:migrate
to reset schema changes.Dry Runs:
php artisan cron:list --dry-run
Job Isolation:
php artisan cron:run my_job --env=local --verbose
Environment-Specific Jobs:
enabled flag to toggle jobs per environment:
cron:
jobs:
production_only_job:
schedule: "0 3 * * *"
enabled: "%kernel.environment% == 'prod'"
Custom Job Storage:
Cron\CronBundle\Storage\StorageInterface and configuring in cron.yaml:
storage:
class: App\Storage\CustomCronStorage
Event Listeners:
JobBeforeRunEvent):
// src/EventListener/CronListener.php
use Cron\CronBundle\Event\JobBeforeRunEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CronListener implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return [
JobBeforeRunEvent::NAME => 'onJobBeforeRun',
];
}
public function onJobBeforeRun(JobBeforeRunEvent $event) {
if ($event->getJob()->getName() === 'my_job') {
$event->stopPropagation(); // Skip execution
}
}
}
Parallel Execution:
Messenger component to run jobs asynchronously:
cron:
jobs:
async_job:
class: App\Services\AsyncJob
schedule: "*/10 * * * *"
messenger: true # Offloads to messenger queue
symfony/messenger and configuration in config/packages/messenger.yaml.How can I help you explore Laravel packages today?