Installation:
composer require amadeus-m/cron-bundle
Add to config/bundles.php (Symfony 4+):
return [
// ...
AmadeusM\CronBundle\AmadeusMCronBundle::class => ['all' => true],
];
Database Setup:
php bin/console make:migration
php bin/console doctrine:migrations:migrate
First Job Creation:
php bin/console cron:create
Follow prompts to define a job (e.g., send_daily_emails). Example YAML config (config/cron.yml):
cron:
jobs:
send_daily_emails:
command: 'app:send-emails'
schedule: '0 0 * * *'
enabled: true
Run Manually:
php bin/console cron:run send_daily_emails
Job Definition:
cron:create interactively or define jobs in config/cron.yml:
cron:
jobs:
cleanup_logs:
command: 'app:cleanup-logs'
schedule: '0 0 * * 0' # Weekly on Sunday
enabled: false
timeout: 3600
app:generate-reports).Scheduling:
* * * * *) or use AmadeusM\CronBundle\Job\JobInterface for custom logic.sync_data:
command: 'app:sync-data'
schedule: '*\/5 * * * *'
Integration with Symfony Services:
// src/Command/CustomJobCommand.php
namespace App\Command;
use AmadeusM\CronBundle\Job\JobInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CustomJobCommand extends Command implements JobInterface {
private $container;
public function __construct(ContainerInterface $container) {
$this->container = $container;
parent::__construct();
}
public function run() {
$this->container->get('app.service')->execute();
}
}
config/cron.yml:
custom_job:
class: App\Command\CustomJobCommand
schedule: '0 * * * *'
Logging and Monitoring:
config/packages/monolog.yaml:
handlers:
cron:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.cron.log"
level: debug
php bin/console cron:list
Background Execution:
php bin/console cron:start --blocking # Foreground (debugging)
php bin/console cron:start # Background (production)
php bin/console cron:stop
Database Schema Mismatch:
php bin/console make:migration and php bin/console doctrine:migrations:migrate to avoid Job table errors.Cron Syntax Errors:
* * * *) will silently fail. Validate with:
php bin/console cron:list
Permission Issues:
var/log/ (for logs).var/cache/ (for job locks).chmod -R 775 var/
Job Locking:
var/cron/locks/). Delete stale locks if jobs hang:
rm -f var/cron/locks/*
Timezone Misalignment:
config/packages/framework.yaml:
timezone: UTC
Dry Runs:
php bin/console cron:run job_name --force
Verbose Output:
php bin/console cron:run --env=dev job_name
Job History:
cron_job table directly for execution logs:
SELECT * FROM cron_job WHERE name = 'job_name' ORDER BY created_at DESC;
Environment-Specific Jobs:
# config/cron/prod.yml
cron:
jobs:
debug_job:
enabled: false
Custom Job Classes:
AmadeusM\CronBundle\Job\JobInterface for reusable logic:
class BaseJob implements JobInterface {
public function run() {
// Shared setup/teardown
}
}
Heroku/Platform-as-a-Service:
cron:start --blocking in a web dyno (not recommended for production; prefer add-ons like Heroku Scheduler).Testing:
$this->container->get('cron.manager')->runJob('test_job');
cron:run in tests with --env=test.Extending the Bundle:
JobManager service to add pre/post hooks:
# config/services.yaml
AmadeusM\CronBundle\Job\JobManager:
arguments:
$logger: '@monolog.logger.cron'
$preRunHooks: ['@app.cron.pre_hook']
How can I help you explore Laravel packages today?