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

serpapi logo
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi

The latest

View all →
Generate Laravel Validation Rules From a JSON Schema image

Generate Laravel Validation Rules From a JSON Schema

Read article
Laravel Suspicious Login: Detect and Block Risky Logins image

Laravel Suspicious Login: Detect and Block Risky Logins

Read article
Decide with Jev: Make Failed AI Requests Safe in Laravel image

Decide with Jev: Make Failed AI Requests Safe in Laravel

Read article
Black Friday: Learn the habits behind AI workflows that work image

Black Friday: Learn the habits behind AI workflows that work

Read article
Laravel Black Friday and Cyber Monday Deals 2026 image

Laravel Black Friday and Cyber Monday Deals 2026

Read article
pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux image

pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux

Read article