Laravel Tutorials

Leveraging Laravel's Conditional Ping Methods for Task Monitoring

Published
Leveraging Laravel's Conditional Ping Methods for Task Monitoring image

Laravel introduces powerful conditional ping methods for the scheduler, enabling selective monitoring of your application's scheduled tasks. These methods provide flexible notification options based on your specific conditions.

The basic implementation shows how to conditionally notify monitoring services:

Schedule::command('backup:run')->hourly()
->pingBeforeIf($condition, $url . '/starting')
->pingOnSuccessIf($condition, $url . '/finished')
->pingOnFailureIf($condition, $url . '/failed');

Here's how to implement environment-specific monitoring for a complex application:

class ApplicationScheduler
{
public function configure()
{
// Environment detection
$isProd = app()->environment('production');
$isDev = app()->environment('development', 'local');
$monitoringUrl = config('services.monitoring.url');
 
// Customer data synchronization
Schedule::job(new SyncCustomerData)
->dailyAt('03:30')
->pingBeforeIf($isProd, "{$monitoringUrl}/sync/customers/start")
->pingOnSuccessIf($isProd, "{$monitoringUrl}/sync/customers/success")
->pingOnFailureIf($isProd || $isDev, "{$monitoringUrl}/sync/customers/error");
 
// Analytics processing
Schedule::command('analytics:process')
->hourlyAt(15)
->pingBeforeIf($isProd && now()->isWeekday(), "{$monitoringUrl}/analytics/start")
->pingOnSuccessIf($isProd && now()->isWeekday(), "{$monitoringUrl}/analytics/complete")
->pingOnFailureIf(true, "{$monitoringUrl}/analytics/failed");
 
// System health check
Schedule::command('system:health-check')
->everyFiveMinutes()
->pingOnFailureIf(true, "{$monitoringUrl}/system/health-check-failed");
}
}

These conditional notification methods provide granular control over task monitoring, allowing you to selectively notify your monitoring systems based on environment, time, or application state.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

masteringlaravel logo
Laravel Code Review

Get expert guidance in a few days with a Laravel code review

Visit Laravel Code Review

The latest

View all →
NativePHP v4: Build Native iOS and Android UI in Blade image

NativePHP v4: Build Native iOS and Android UI in Blade

Read article
Laravel Lock: Distributed Locks for Models and Routes image

Laravel Lock: Distributed Locks for Models and Routes

Read article
Laravel Image Responses: Serve Resized Images From Routes image

Laravel Image Responses: Serve Resized Images From Routes

Read article
Pause All Laravel Queues During a Deploy image

Pause All Laravel Queues During a Deploy

Read article
Laravel Terminal UI for the artisan dev Command image

Laravel Terminal UI for the artisan dev Command

Read article
Pause All Queues and a New artisan dev UI in Laravel 13.25 image

Pause All Queues and a New artisan dev UI in Laravel 13.25

Read article