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

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
PayZephyr: One Payment API for Stripe, Paystack, and PayPal image

PayZephyr: One Payment API for Stripe, Paystack, and PayPal

Read article
Bifrost Turns One With AI Builds and New Workflows image

Bifrost Turns One With AI Builds and New Workflows

Read article
Preview Blade Templates in macOS Finder with Quick Blade image

Preview Blade Templates in macOS Finder with Quick Blade

Read article
Artisan Debugging Commands in Laravel Telescope 5.24.0 image

Artisan Debugging Commands in Laravel Telescope 5.24.0

Read article
Queue totalSize() and JobInterrupted Event in Laravel 13.31 image

Queue totalSize() and JobInterrupted Event in Laravel 13.31

Read article
Find Unexpected Test Inputs with Fuzz for Pest image

Find Unexpected Test Inputs with Fuzz for Pest

Read article