Cron Tasks
Cron Task Interface
<?php
namespace Plugins\MyPlugin\CronTask;
use App\Core\Cron\PluginCronTaskInterface;
use Psr\Log\LoggerInterface;
class MyCleanupTask implements PluginCronTaskInterface
{
public function __construct(
private readonly MyService $myService,
private readonly LoggerInterface $logger,
) {}
public function execute(): void
{
$this->logger->info('Running cleanup task');
try {
$deletedCount = $this->myService->cleanupOldData();
$this->logger->info('Cleanup completed', [
'deleted_count' => $deletedCount
]);
} catch (\Exception $e) {
$this->logger->error('Cleanup failed', [
'error' => $e->getMessage()
]);
throw $e;
}
}
public function getSchedule(): string
{
// Cron expression
return '0 3 * * *'; // Daily at 3 AM
}
public function getName(): string
{
return 'my-plugin:cleanup';
}
public function getDescription(): string
{
return 'Clean up old data from my-plugin tables';
}
public function isEnabled(): bool
{
// Control whether task should run
return true;
}
}Cron Expression Syntax
Expression
Meaning
When it runs
Cron Expression Format
Advanced Cron Task
Service Registration
Task Examples
Daily Database Cleanup
Hourly Data Sync
Weekly Report Generation
Server Health Monitoring
Expiration Reminder Notifications
Automatic Backup Cleanup
Analytics Data Aggregation
Error Handling
Conditional Execution
Best Practices
Monitoring Cron Tasks
Related Guides
Last updated