beyondbluesky/cron-manager-bundle
Installation
composer require beyondbluesky/cron-manager-bundle
Add to config/bundles.php:
return [
// ...
BeyondBluesky\CronManagerBundle\BeyondBlueskyCronManagerBundle::class => ['all' => true],
];
Basic Configuration
Define cron jobs in config/packages/beyondbluesky_cron_manager.yaml:
cron_manager:
jobs:
my_job:
command: 'app:my-command'
schedule: '*/5 * * * *' # Every 5 minutes
user: 'www-data'
enabled: true
First Use Case
Create a Symfony command (e.g., app:my-command) to execute your task. The bundle will handle scheduling via cron.
Job Definition
command: Symfony command to execute.schedule: Cron expression (e.g., 0 0 * * * for daily).user: User to run the command (optional).enabled: Toggle job activation (default: true).cron_manager:
jobs:
cleanup_db:
command: 'app:cleanup-db'
schedule: '0 3 * * *' # Daily at 3 AM
user: 'root'
Dynamic Job Management
CronManager service to enable/disable jobs programmatically:
use BeyondBluesky\CronManagerBundle\Service\CronManager;
public function __construct(private CronManager $cronManager) {}
public function toggleJob(string $jobName, bool $enabled) {
$this->cronManager->setJobEnabled($jobName, $enabled);
}
Logging and Monitoring
use Psr\Log\LoggerInterface;
public function execute(InputInterface $input, OutputInterface $output, LoggerInterface $logger) {
$logger->info('Job executed at ' . new \DateTime());
// Job logic here
}
Environment-Specific Schedules
# config/packages/dev/beyondbluesky_cron_manager.yaml
cron_manager:
jobs:
my_job:
schedule: '*/1 * * * *' # More frequent in dev
Command trait.CronManager into controllers/services to manage jobs dynamically.CronManager in tests to verify job toggling logic.Cron Syntax Errors
*/9 * * * *) will fail silently. Validate expressions using tools like crontab.guru.echo commands before deploying.User Permissions
user field is misconfigured, cron jobs may fail due to permission issues.Bundle Not Auto-Registering
config/bundles.php and the YAML file is correctly named/located.php bin/console cache:clear
Command Not Found
command field references a non-existent Symfony command, the job will fail.app:my-command vs. my:command)./var/log/syslog or journalctl -u cron) for execution errors.echo to verify scheduling:
cron_manager:
jobs:
test_job:
command: 'echo "Testing cron at $(date)"'
schedule: '* * * * *'
Custom Job Storage
BeyondBluesky\CronManagerBundle\Storage\JobStorageInterface and configuring it in services.yaml:
services:
BeyondBluesky\CronManagerBundle\Service\CronManager:
arguments:
$jobStorage: '@app.custom_job_storage'
Event Listeners
BeyondBluesky\CronManagerBundle\Event\CronJobEvent and tagging the service:
services:
App\EventListener\CronJobListener:
tags:
- { name: 'beyondbluesky.cron_manager.event_listener' }
Dynamic Schedules
JobLoader service:
use BeyondBluesky\CronManagerBundle\Loader\JobLoaderInterface;
class DatabaseJobLoader implements JobLoaderInterface {
public function load(): array {
return $this->fetchJobsFromDatabase();
}
}
%kernel.environment% in YAML to conditionally define jobs:
cron_manager:
jobs:
prod_only_job:
command: 'app:prod-task'
schedule: '0 0 * * *'
enabled: '%kernel.environment% === "prod"'
nice or ionice to reduce system impact:
cron_manager:
jobs:
backup_db:
command: 'nice -n 19 app:backup-db'
schedule: '0 2 * * *'
How can I help you explore Laravel packages today?