Installation
composer require bcc/cron-manager-bundle
Add to config/bundles.php:
return [
// ...
Bcc\CronManagerBundle\BccCronManagerBundle::class => ['all' => true],
];
Basic Setup
cron_job table:
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
php bin/console cache:clear
First Use Case: Define a Cron Job
Create a command (e.g., SendDailyNewsletterCommand) and register it in services.yaml:
services:
App\Command\SendDailyNewsletterCommand:
tags: ['console.command']
Then, use the bundle’s admin interface (accessible at /admin/cron) to schedule it:
app:send-daily-newsletter0 8 * * * (daily at 8 AM)Via Admin UI
/admin/cron) to:
Via Code (Programmatic Control)
Use the CronManager service to interact with jobs programmatically:
// Get the manager
$cronManager = $this->container->get('bcc_cron_manager.cron_manager');
// Create a job
$job = new CronJob();
$job->setCommand('app:send-daily-newsletter');
$job->setSchedule('0 8 * * *');
$job->setActive(true);
$cronManager->save($job);
// List all jobs
$jobs = $cronManager->findAll();
Integration with Symfony Events
Listen for job-related events (e.g., bcc_cron_manager.job.run or bcc_cron_manager.job.failed) to extend functionality:
# config/services.yaml
services:
App\EventListener\CronJobListener:
tags:
- { name: kernel.event_listener, event: bcc_cron_manager.job.run, method: onJobRun }
Logging and Monitoring
config/packages/bcc_cron_manager.yaml:
bcc_cron_manager:
logging: true
monolog channels.Environment-Specific Scheduling
Use parameters to define schedules per environment (e.g., dev vs. prod):
# config/packages/dev/bcc_cron_manager.yaml
bcc_cron_manager:
schedules:
'app:send-daily-newsletter': '*/5 * * * *' # Every 5 minutes in dev
Permissions Issues
www-data) has permissions to execute commands.sudo -u www-data php bin/console app:your-command
Cron Expression Syntax
* * * * * ? for Quartz-style).Database Locking
$entityManager = $this->getDoctrine()->getManager();
$entityManager->beginTransaction();
try {
// Job logic
$entityManager->commit();
} catch (\Exception $e) {
$entityManager->rollBack();
throw $e;
}
Admin UI Access
/admin/cron route is not secured by default. Protect it with Symfony’s security:
# config/packages/security.yaml
access_control:
- { path: ^/admin/cron, roles: ROLE_ADMIN }
Command Output Handling
bcc_cron_manager:
log_file: '%kernel.logs_dir%/cron_jobs.log'
Custom Job Metadata
Extend the CronJob entity to store additional fields (e.g., priority, last_run_at):
// src/Entity/CustomCronJob.php
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
class CustomCronJob extends CronJob {
#[ORM\Column(type: 'integer')]
private $priority = 1;
}
Dynamic Schedules Use Twig or PHP to generate schedules dynamically (e.g., based on user input):
{# templates/admin/cron/new.html.twig #}
<input type="text" name="schedule" value="{{ cronExpression(user.timezone) }}">
Testing Cron Jobs
Mock the CronManager in tests:
$cronManager = $this->createMock(CronManager::class);
$cronManager->method('findAll')->willReturn([$mockJob]);
$this->container->set('bcc_cron_manager.cron_manager', $cronManager);
Performance Optimization
bcc_cron_manager:
logging: '%kernel.debug%'
Backup and Migration
cron_job table, as it’s critical for job persistence.php bin/console make:migration
How can I help you explore Laravel packages today?