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 →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article