Installation:
composer require colourstream/cron-bundle:dev-master
Register the bundle in AppKernel.php:
new ColourStream\Bundle\CronBundle\ColourStreamCronBundle(),
Database Schema: Run the schema update to create the required tables:
php app/console doctrine:schema:update --force
First Use Case: Define a command to schedule:
php app/console generate:bundle --namespace=Acme/DemoBundle --format=yml --dir=src --no-interaction
Create a command (e.g., AcmeDemoBundle/Command/DemoCommand.php):
namespace Acme\DemoBundle\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class DemoCommand extends Command
{
protected function configure()
{
$this->setName('demo:task');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('Running scheduled task!');
return 0;
}
}
Register the Command: Use the bundle’s CLI to register the command with a schedule:
php app/console cron:scan
Then define the schedule in app/config/config.yml:
colourstream_cron:
commands:
demo:task:
schedule: "0 0 * * *" # Runs daily at midnight
Trigger Execution: Run the cron jobs manually:
php app/console cron:run
Command Registration:
cron:scan to auto-discover commands annotated with @Cron (if supported) or manually define them in config.yml.colourstream_cron:
commands:
app:send_emails:
schedule: "*/5 * * * *" # Every 5 minutes
description: "Send daily emails"
Webhook Integration:
cron:run command via a web endpoint using Symfony’s Router or a controller:
// src/Acme/DemoBundle/Controller/CronController.php
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class CronController extends Controller
{
public function runAction()
{
$output = $this->get('kernel')->getContainer()->get('cron.runner')->run();
return new Response($output);
}
}
routing.yml:
cron_run:
path: /cron/run
defaults: { _controller: AcmeDemoBundle:Cron:run }
Logging and Monitoring:
cron_job_execution) for auditing.$executions = $this->getDoctrine()->getRepository('ColourStreamCronBundle:CronJobExecution')->findBy(['command' => 'demo:task']);
Dynamic Scheduling:
CronManager service:
// src/Acme/DemoBundle/DependencyInjection/Compiler/CronPass.php
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class CronPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$definition = $container->findDefinition('colourstream_cron.manager');
$definition->addMethodCall('setCustomSchedule', ['demo:task', '*/10 * * * *']); // Override schedule
}
}
Environment-Specific Configs:
%kernel.environment% in config.yml to define different schedules per environment (e.g., dev, prod):
colourstream_cron:
commands:
demo:task:
schedule: "%cron_demo_task_schedule%"
app/config/parameters.yml:
parameters:
cron_demo_task_schedule: "*/5 * * * *" # Dev: every 5 mins
Database Schema Mismatch:
CronJob or CronJobExecution), ensure you update the schema:
php app/console doctrine:schema:update --force
Command Not Found:
config.yml (e.g., app:send_emails vs. demo:task).php app/console list
Cron Jobs Running Too Frequently:
*/5 * * * * won’t run more than once every 5 minutes). However, if cron:run is called manually, it may execute pending jobs immediately.cron:run at the desired interval.Symfony 2.1 Dependency:
Missing Web Cron Endpoint:
cron:run via a controller).Locking Mechanism:
cron:run is triggered simultaneously.// Example pseudo-lock in a custom command
$lock = $this->get('database_connection')->fetchOne('SELECT GET_LOCK("cron_demo_task_lock", 5)');
if (!$lock) {
$this->getOutput()->writeln('Lock failed. Skipping.');
return 1;
}
// Execute job...
$this->get('database_connection')->exec('SELECT RELEASE_LOCK("cron_demo_task_lock")');
Dry Run:
cron:scan --dry-run to preview registered commands without modifying the database.Log Output:
cron:run output to a log file for debugging:
php app/console cron:run >> /var/log/cron.log 2>&1
Check Last Execution:
cron_job_execution table to verify jobs ran:
SELECT * FROM cron_job_execution WHERE command = 'demo:task' ORDER BY executed_at DESC LIMIT 1;
Symfony Debug Toolbar:
config_dev.yml to inspect cron-related services.Custom Storage:
ColourStream\Bundle\CronBundle\Model\CronJobManagerInterface:
// src/Acme/DemoBundle/Model/CustomCronJobManager.php
use ColourStream\Bundle\CronBundle\Model\CronJobManagerInterface;
class CustomCronJobManager implements CronJobManagerInterface
{
public function findAll() { /* Custom logic */ }
public function findByCommand($command) { /* Custom logic */ }
// ...
}
services.yml:
services:
acme.demo.cron_job_manager:
class: Acme\DemoBundle\Model\CustomCronJobManager
tags:
- { name: colourstream_cron.manager }
Event Listeners:
cron.job.before, cron.job.after) by implementing ColourStream\Bundle\CronBundle\Event\CronEvents:
// src/Acme/DemoBundle/EventListener/CronListener.php
use ColourStream\Bundle\CronBundle\Event\CronEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CronListener implements EventSubscriberInterface
{
public static function getSub
How can I help you explore Laravel packages today?