Installation:
composer require axiolab/crontask-bundle
Add the bundle to config/bundles.php (Symfony) or AppKernel.php (legacy):
Axiolab\CrontaskBundle\AxiolabCrontaskBundle::class => ['all' => true],
Configure:
Edit config/packages/axiolab_crontask.yaml (Symfony 4.4+) or app/config/config.yml (legacy):
axiolab_crontask:
commands:
- 'app:my-cron-job'
- 'app:another-job'
schedule: '*/5 * * * *' # Default: every 5 minutes
First Use Case:
Create a Symfony command (e.g., src/Command/MyCronJobCommand.php):
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class MyCronJobCommand extends Command
{
protected static $defaultName = 'app:my-cron-job';
protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln('Running cron job!');
// Your logic here
return Command::SUCCESS;
}
}
Register the command in config/services.yaml:
services:
App\Command\MyCronJobCommand:
tags: ['console.command']
Task Registration:
axiolab_crontask.yaml under commands.app:cron-*) to group related tasks.Dynamic Scheduling:
axiolab_crontask:
commands:
app:high-priority-job: '@hourly'
app:low-priority-job: '0 3 * * *' # Daily at 3 AM
Logging and Output:
> /path/to/log.txt 2>&1 to the command in the config.LoggerInterface in commands for structured logging.Environment-Specific Configs:
%kernel.environment% in YAML to switch schedules:
schedule: '%env(CRON_SCHEDULE)%'
.env (e.g., CRON_SCHEDULE="*/10 * * * *").Integration with Symfony Events:
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class MyCronJobCommand extends Command {
public function __construct(private EventDispatcherInterface $dispatcher) {}
protected function execute(InputInterface $input, OutputInterface $output): int {
$this->dispatcher->dispatch(new PreCronEvent());
// ...
}
}
Dependency Injection:
use App\Service\MyService;
class MyCronJobCommand extends Command {
public function __construct(private MyService $service) {}
}
Outdated Package:
Console\Command changes in Symfony 6+).Cron Syntax Errors:
*/9 * * * *) will silently fail. Validate with crontab.guru.cron daemon is configured to run the Symfony command:
* * * * * /path/to/php /path/to/bin/console axiolab:crontask:run
Logging Overhead:
# config/packages/monolog.yaml
handlers:
cron:
type: stream
path: var/log/cron.log
level: debug
Command Not Found:
console.command in services.yaml.php bin/console cache:clear
Race Conditions:
flock or symfony/lock) in commands:
use Symfony\Component\Lock\LockFactory;
$lock = $lockFactory->createLock('my_cron_job_lock', 300);
if (!$lock->acquire()) {
throw new \RuntimeException('Lock acquired by another process');
}
Testing:
CrontaskManager in tests:
$manager = $this->createMock(CrontaskManager::class);
$manager->method('run')->willReturn(true);
$this->container->set('axiolab_crontask.manager', $manager);
Extending the Bundle:
CrontaskManager service to add custom logic:
# config/services.yaml
services:
App\Service\CustomCrontaskManager:
decorates: 'axiolab_crontask.manager'
arguments: ['@.inner']
Performance:
Process component to run commands asynchronously:
use Symfony\Component\Process\Process;
$process = new Process(['php', 'bin/console', 'app:long-job']);
$process->start();
Debugging:
php bin/console axiolab:crontask:run --verbose
last_execution field in the database (if using the last_execution_time feature).Alternatives:
spiral/robo for task scheduling.CronExpression for parsing schedules.spatie/schedule (if migrating to Laravel).How can I help you explore Laravel packages today?