Enhanced Task Monitoring with Laravel's Conditional Ping Methods
Take control of your scheduled task monitoring with Laravel's latest scheduler additions. These new conditional ping methods make it easy to notify services about task status when needed.
Basic Usage
Configure conditional ping notifications:
Schedule::command('backup:run')->hourly()
->pingBeforeIf($condition, $url . '/starting')
->pingOnSuccessIf($condition, $url . '/finished')
->pingOnFailureIf($condition, $url . '/failed');
Real-World Example
Here's how to implement comprehensive task monitoring:
class TaskScheduler
{
public function registerTasks()
{
// Configuration
$monitoringEnabled = config('monitoring.enabled');
$baseUrl = config('monitoring.webhook_url');
// Database backup task
Schedule::command('backup:run')
->daily()
->at('01:00')
->pingBeforeIf($monitoringEnabled, "{$baseUrl}/backup/starting")
->pingOnSuccessIf($monitoringEnabled, "{$baseUrl}/backup/finished")
->pingOnFailureIf($monitoringEnabled, "{$baseUrl}/backup/failed");
// Cache cleanup task
Schedule::command('cache:prune-stale-tags')
->weekly()
->sundays()
->at('00:00')
->pingBeforeIf($monitoringEnabled, "{$baseUrl}/cache/starting")
->pingOnSuccessIf($monitoringEnabled, "{$baseUrl}/cache/finished")
->pingOnFailureIf($monitoringEnabled, "{$baseUrl}/cache/failed");
// Report generation
Schedule::job(new GenerateWeeklyReport)
->weekly()
->mondays()
->at('05:00')
->pingBeforeIf(
$monitoringEnabled && app()->environment('production'),
"{$baseUrl}/reports/starting"
)
->pingOnSuccessIf(
$monitoringEnabled && app()->environment('production'),
"{$baseUrl}/reports/finished"
)
->pingOnFailureIf(
$monitoringEnabled && app()->environment('production'),
"{$baseUrl}/reports/failed"
);
}
}
These conditional ping methods provide flexible task monitoring that can be easily enabled or disabled based on your environment or configuration.
If this guide was helpful to you, subscribe to my daily newsletter and give me a follow on X/Twitter and Bluesky. It helps a lot!