Skip to main content

On This Page

Don't Let Your Staging Server Die: Separate Task Scheduling in Laravel

2 min read
Share

These articles are AI-generated summaries. Please check the original sources for full details.

Don’t Let Your Staging Server Die: Separate Task Scheduling in Laravel

A staging server froze at 100% CPU and 3.2GB memory usage due to Laravel’s default task scheduler, which runs production-level jobs like price exports and supplier syncs. The fix: environment-specific scheduling.

Why This Matters

Staging environments often lack the resources of production, yet developers frequently copy identical task schedules. This creates a mismatch between ideal models (uninterrupted execution) and technical reality (limited CPU/memory). The cost? Server freezes, failed tests, and deployment delays. In one case, heavy scheduled commands caused a 100% CPU spike, rendering the server unresponsive.

Key Insights

  • “Staging server froze at 100% CPU, 2025”: CPU and memory metrics from htop in the context
  • “Environment-specific scheduling for Laravel”: Separating tasks by APP_ENV to avoid overloading staging
  • “Digital Ocean droplet with 2 vCPUs”: Typical low-resource staging setup that fails with heavy jobs

Working Example

// app/Console/Kernel.php
final class Kernel extends ConsoleKernel
{
    protected function schedule(Schedule $schedule): void
    {
        $this->scheduleCommon($schedule);
        if ($this->app->environment('production')) {
            $this->scheduleProduction($schedule);
        }
        if ($this->app->environment('staging')) {
            $this->scheduleStaging($schedule);
        }
    }

    private function scheduleCommon(Schedule $schedule): void
    {
        $schedule->command(PruneCommand::class, ['--model' => [CartItem::class]])->hourly();
        $schedule->command(PruneExpired::class)->dailyAt('03:00');
    }

    private function scheduleProduction(Schedule $schedule): void
    {
        $schedule->command(SearchIndexSyncDiff::class)->dailyAt('05:00');
        $schedule->command(PriceExportAutoDispatchCommand::class)->cron('15 6,8,10,12,14,16,18 * * *');
    }

    private function scheduleStaging(Schedule $schedule): void
    {
        $schedule->command(PriceExportAutoDispatchCommand::class)->cron('15 6 * * *');
    }
}

Practical Applications

  • Use Case: Laravel apps with staging environments needing reduced task frequency for testing
  • Pitfall: Copying production schedules to staging causes resource exhaustion and server instability

References:


Continue reading

Next article

Free AI Image Watermark Removal Tool Processes Data Locally

Related Content