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 →
Slow Job Alerts and Replica Commit IDs in Laravel 13.40 image

Slow Job Alerts and Replica Commit IDs in Laravel 13.40

Read article
Flock: A Free, Native Herd Alternative for Laravel image

Flock: A Free, Native Herd Alternative for Laravel

Read article
Live Props for Real-Time Updates in Inertia.js v3.11 image

Live Props for Real-Time Updates in Inertia.js v3.11

Read article
Cooldown Dependency Policy now in Composer 2.11 image

Cooldown Dependency Policy now in Composer 2.11

Read article
Email Sending and Testing for Laravel in One Account image

Email Sending and Testing for Laravel in One Account

Read article
Decide with Jev: Tie Every AI Check to the Exact Draft image

Decide with Jev: Tie Every AI Check to the Exact Draft

Read article