Install the Bundle
composer require caeligo/scheduler-bundle
Register the bundle in config/bundles.php:
return [
// ...
Caeligo\SchedulerBundle\CaeligoSchedulerBundle::class => ['all' => true],
];
Publish Configuration & Assets
php bin/console caeligo:scheduler:install
This generates:
config/packages/caeligo_scheduler.yaml (default config)var/log/scheduler/ (log directory)public/scheduler/ (dashboard assets)Define Your First Task
Use the #[AsSchedulableCommand] attribute on a Symfony command:
use Caeligo\SchedulerBundle\Attribute\AsSchedulableCommand;
#[AsSchedulableCommand(
name: 'app:example-task',
schedule: '@hourly', // or '0 * * * *' for cron
description: 'Runs hourly'
)]
class ExampleTask extends Command
{
protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln('Task executed!');
return Command::SUCCESS;
}
}
Install the Crontab Entry
php bin/console caeligo:scheduler:install:cron
Or via the dashboard at /scheduler.
php bin/console caeligo:scheduler:run app:example-task
php bin/console caeligo:scheduler:logs app:example-task
php bin/console caeligo:scheduler:list
Attribute-Based Definition
Use #[AsSchedulableCommand] on existing Symfony commands. No need to duplicate logic or create separate entities.
#[AsSchedulableCommand(
schedule: '0 3 * * *', // Runs daily at 3 AM
overlap: 'skip', // Skip if previous run is still active
timeout: 300 // Max execution time (seconds)
)]
Configuration Overrides
Override default settings in config/packages/caeligo_scheduler.yaml:
caeligo_scheduler:
log_directory: '%kernel.project_dir%/var/log/custom_scheduler'
default_timezone: 'Europe/Berlin'
roles:
scheduler_dashboard: ROLE_ADMIN
Dynamic Scheduling Use PHP logic to conditionally enable/disable tasks:
#[AsSchedulableCommand(
schedule: '@daily',
enabled: false // Disable by default
)]
class ConditionalTask extends Command
{
public function __construct(private bool $isEnabled) {}
protected function configure(): void
{
$this->setEnabled($this->isEnabled);
}
}
Shared Hosting Compatibility Use the HTTP trigger fallback for environments without cron:
caeligo_scheduler:
http_trigger:
enabled: true
endpoint: '/scheduler/webhook'
Then call https://your-site.com/scheduler/webhook via a third-party service (e.g., Pinger, Cron-job.org).
Logging and Monitoring Integrate with Monolog handlers for centralized logging:
monolog:
handlers:
scheduler:
type: stream
path: '%kernel.logs_dir%/scheduler.log'
level: debug
Testing Mock the scheduler in PHPUnit:
use Caeligo\SchedulerBundle\Scheduler\SchedulerInterface;
$scheduler = $this->createMock(SchedulerInterface::class);
$scheduler->method('runTask')->willReturn(true);
$this->container->set(SchedulerInterface::class, $scheduler);
Crontab Permission Issues
www-data or root) has write access to:
var/log/scheduler/cache/ and var/ directories.chown -R www-data:www-data var/
chmod -R 775 var/
Timezone Mismatches
caeligo_scheduler:
default_timezone: 'America/New_York'
php bin/console debug:config caeligo_scheduler
Overlap Handling
'skip') may hide failures. Use 'fail' to throw exceptions:
#[AsSchedulableCommand(overlap: 'fail')]
php bin/console caeligo:scheduler:status app:example-task
Dashboard Caching
php bin/console cache:clear
Enable Verbose Logging
Add to config/packages/monolog.yaml:
handlers:
scheduler:
type: stream
path: '%kernel.logs_dir%/scheduler.log'
level: debug
channels: ['!event']
Check Crontab Installation Verify the cron entry exists:
crontab -l | grep caeligo
Reinstall if missing:
php bin/console caeligo:scheduler:install:cron
Task Execution Debugging Use Symfony’s debug toolbar to inspect:
Custom Task Storage
Override the file-based storage by implementing Caeligo\SchedulerBundle\Storage\TaskStorageInterface:
use Caeligo\SchedulerBundle\Storage\TaskStorageInterface;
class DatabaseTaskStorage implements TaskStorageInterface
{
public function saveTask(Task $task): void { /* ... */ }
public function findTask(string $name): ?Task { /* ... */ }
// ...
}
Register as a service:
services:
Caeligo\SchedulerBundle\Storage\TaskStorageInterface: '@database_task_storage'
Event Listeners
Subscribe to scheduler events (e.g., TaskRunningEvent):
use Caeligo\SchedulerBundle\Event\TaskRunningEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class SchedulerSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
TaskRunningEvent::NAME => 'onTaskRunning',
];
}
public function onTaskRunning(TaskRunningEvent $event): void
{
// Log or notify before task execution
}
}
Custom Dashboard Templates
Override Twig templates in templates/bundles/caeligo_scheduler/:
mkdir -p templates/bundles/caeligo_scheduler/
cp vendor/caeligo/scheduler-bundle/Resources/views/* templates/bundles/caeligo_scheduler/
Extend the base template (index.html.twig) to add custom fields.
How can I help you explore Laravel packages today?