Installation
composer require dekalee/nightly-task-bundle
For Symfony <4.x, register the bundle in AppKernel.php:
new Dekalee\NightlyTaskBundle\DekaleeNightlyTaskBundle(),
First Use Case
Tag an existing console command as a nightly task by adding the dekalee.nightly_task tag in its definition (e.g., src/Command/MyNightlyCommand.php):
# config/services.yaml
services:
App\Command\MyNightlyCommand:
tags: ['console.command', 'dekalee.nightly_task']
Verify Registration List registered nightly tasks:
./bin/console dekalee:nightly:list
Tagging Commands
Use the dekalee.nightly_task tag to include any Symfony console command in the nightly execution pipeline. Example:
# config/services.yaml
services:
App\Command\BackupDatabaseCommand:
tags: ['console.command', 'dekalee.nightly_task']
App\Command\CleanupLogsCommand:
tags: ['console.command', 'dekalee.nightly_task']
Ordering Tasks
Define execution order via the dekalee.nightly_task.order argument (default: 0):
services:
App\Command\HighPriorityTask:
tags: ['console.command', 'dekalee.nightly_task']
arguments:
$order: 10 # Runs before tasks with lower order
Conditional Execution Skip tasks based on environment variables or logic:
// In your command class
protected function execute(InputInterface $input, OutputInterface $output): int
{
if (!$this->getContainer()->getParameter('run_nightly_tasks')) {
$output->writeln('Skipped: Nightly tasks disabled.');
return Command::SUCCESS;
}
// ... rest of the logic
}
Scheduling with Cron
Add a cron job to trigger nightly tasks (e.g., via crontab or a hosting provider):
0 3 * * * cd /path/to/project && php bin/console dekalee:nightly:tasks >> /var/log/nightly_tasks.log 2>&1
Logging and Monitoring Redirect output to a log file for debugging:
./bin/console dekalee:nightly:tasks --no-debug >> storage/logs/nightly_tasks.log 2>&1
Command Dependencies
return Command::FAILURE to halt execution.if ($this->checkCriticalCondition()) {
$this->output->writeln('Critical failure!');
return Command::FAILURE;
}
Order Collisions
order value may execute in an undefined sequence. Avoid ambiguity by using distinct values.Environment-Specific Tasks
dev vs. prod). Use Symfony’s environment-aware services:
# config/services.yaml
App\Command\DevOnlyTask:
tags: ['console.command', 'dekalee.nightly_task']
calls:
- [setEnvironment, ['%kernel.environment%']]
Performance Overhead
Testing Nightly Tasks
dekalee:nightly:tasks command in PHPUnit tests:
$this->executeCommand('dekalee:nightly:tasks', [
'--env' => 'test',
]);
Dry Run Mode Simulate execution without running commands:
./bin/console dekalee:nightly:list --verbose
Logging Enable debug mode for verbose output:
./bin/console dekalee:nightly:tasks --no-interaction --debug
Isolation Test individual tasks separately before bundling them:
./bin/console app:backup-database --no-interaction
Custom Task Groups
Extend the bundle to support dynamic task grouping (e.g., by priority or category). Override the NightlyTaskManager service:
# config/services.yaml
Dekalee\NightlyTaskBundle\Manager\NightlyTaskManager:
class: App\Service\CustomNightlyTaskManager
Pre/Post-Hooks
Add lifecycle hooks (e.g., send notifications before/after execution) by subscribing to the dekalee.nightly_task.run event:
// src/EventListener/NightlyTaskListener.php
public static function getSubscribedEvents(): array
{
return [
'dekalee.nightly_task.run' => 'onNightlyTaskRun',
];
}
Dynamic Task Registration
Register tasks programmatically (e.g., from a database) by implementing a custom TaskLoader:
// src/Service/DynamicTaskLoader.php
class DynamicTaskLoader implements TaskLoaderInterface
{
public function load(): array
{
return $this->fetchTasksFromDatabase();
}
}
Then override the service:
Dekalee\NightlyTaskBundle\Loader\TaskLoader:
class: App\Service\DynamicTaskLoader
How can I help you explore Laravel packages today?