Installation:
composer require colourstream/cron-bundle:dev-master
Add to config/bundles.php:
ColourStream\Bundle\CronBundle\ColourStreamCronBundle::class => ['all' => true],
Database Setup:
php bin/console doctrine:schema:update --force
This creates the cron_job table to store scheduled tasks.
First Use Case: Register a command to run daily:
php bin/console cron:register "app/console my:command" "0 0 * * *" "My Daily Task"
cron_job table) or via:
php bin/console cron:list
Trigger Execution:
php bin/console cron:run
Or set up a system cron job to run cron:run periodically (e.g., every 5 minutes):
*/5 * * * * php /path/to/your/project/bin/console cron:run >> /dev/null 2>&1
Registering Jobs:
php bin/console cron:register "app/console my:command --option=value" "0 * * * *" "Task Description"
$this->get('cron.manager')->register(
'app/console my:command',
'0 * * * *', // Cron expression
'Task Description',
['option' => 'value'] // Command arguments
);
Running Jobs:
php bin/console cron:run
/cron/run) to call:
$this->get('cron.manager')->run();
Useful for services like EasyCron or Cron-job.org.Listing/Managing Jobs:
php bin/console cron:list
php bin/console cron:disable 1 # Disable job with ID 1
php bin/console cron:enable 1 # Enable it again
php bin/console cron:delete 1
Logging and Output:
monolog system.register():
$this->get('cron.manager')->register(
'app/console my:command >> /var/log/my_task.log 2>&1',
'0 * * * *',
'Task with Logging'
);
Dependency Injection:
Inject the CronManager service where needed:
use ColourStream\Bundle\CronBundle\Manager\CronManager;
class MyService {
public function __construct(private CronManager $cronManager) {}
public function scheduleTask() {
$this->cronManager->register('app/console my:command', '* * * * *', 'Scheduled via DI');
}
}
Environment-Specific Jobs:
Use Symfony’s %kernel.environment% to register jobs only in specific environments (e.g., prod):
if ('prod' === $this->getParameter('kernel.environment')) {
$this->get('cron.manager')->register('app/console my:prod-command', '0 3 * * *', 'Production-only Task');
}
Dynamic Cron Expressions: Generate cron expressions dynamically (e.g., based on user input or config):
$expression = sprintf('0 %d * * *', $hour); // Run daily at a specific hour
$this->get('cron.manager')->register('app/console my:command', $expression, 'Dynamic Hourly Task');
Event Listeners: Listen for job execution events (if the bundle supports them) to add pre/post hooks:
// Example (hypothetical; verify bundle docs)
$eventDispatcher->addListener(CronEvents::JOB_START, function ($event) {
// Log or modify job context before execution
});
Database Dependency:
php bin/console cron:export > jobs.sql
php bin/console cron:import < jobs.sql
Cron Expression Syntax:
* * * * *). Invalid expressions (e.g., 0 25 * * *) will fail silently or cause jobs to never run.spatie/cron-expression.Command Paths:
app/console my:command) and accessible from the CLI environment where cron:run executes.console my:command) will fail.bin/console in paths:
php bin/console cron:register "bin/console my:command" "0 * * * *" "Fixed Path"
Output Handling:
>> /file.log), ensure the user running cron:run has write permissions.chmod 777 /var/log/my_task.log
Symfony Version Compatibility:
Concurrent Executions:
cron:run instances execute simultaneously, jobs may run multiple times.php bin/console cron:register "bin/console my:command --lock-file=/tmp/my_task.lock" "* * * * *" "Safe Task"
Check Job Status:
php bin/console cron:list
cron_job table directly for missing/incorrect entries.Log Execution:
php bin/console cron:run --env=dev
use Psr\Log\LoggerInterface;
class MyCommand extends ContainerAwareCommand {
protected function execute(InputInterface $input, OutputInterface $output) {
$this->get('logger')->info('Job started', ['command' => $this->getName()]);
// ...
}
}
Test Cron Expressions:
at (Linux/macOS):
echo "bin/console my:command" | at -f /dev/stdin 2023-12-31 23:59
Permissions:
cron:run (e.g., www-data or a cron user) has:
Custom Job Storage:
CronJobRepository to use a custom storage backend (e.g., Redis, cache).ColourStream\Bundle\CronBundle\Repository\CronJobRepositoryInterface.Webhook Trigger:
How can I help you explore Laravel packages today?