Latest Premium Tutorial

Latest Course

Newest content

  • This Week: 40% Discount for Yearly and Lifetime Plans!
    · 2 mins, 215 words

    This Week: 40% Discount for Yearly and Lifetime Plans!

  • Laravel 11: New Artisan "make:trait" Command
    · 1 min, 107 words

    Laravel 11: New Artisan "make:trait" Command

  • Random Quick Laravel Tip:
    Pause a long running job when queue worker shuts down

    When running a long job, if your queue worker gets shutdown by

    • Stopping the worker.
    • Sending signal SIGTERM (SIGINT for Horizon).
    • Pressing CTRL + C (Linux/Windows).

    Then the job process may get corrupted while it is doing something.

    By checking with app('queue.worker')->shouldQuit, we can determine if the worker is shutting down. This way, we can save the current process and requeue the job so that when the queue worker runs again, it can resume from where it left.

    This is very useful in the Containerized world (Kubernetes, Docker etc.) where the container gets destroyed and re-created anytime.

    <?php
     
    namespace App\Jobs;
     
    use App\Models\User;
    use Illuminate\Bus\Queueable;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Foundation\Bus\Dispatchable;
    use Illuminate\Queue\InteractsWithQueue;
    use Illuminate\Queue\SerializesModels;
    use Illuminate\Support\Facades\Cache;
    use Illuminate\Support\Facades\Log;
     
    class MyLongJob implements ShouldQueue
    {
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
     
    public $timeout = 3600;
     
    private const CACHE_KEY = 'user.last-process-id';
     
    public function handle()
    {
    $processedUserId = Cache::get(self::CACHE_KEY, 0); // Get last processed item id
    $maxId = Users::max('id');
     
    if ($processedUserId >= $maxId) {
    Log::info("All users have already been processed!");
    return;
    }
     
    while ($user = User::where('id', '>', $processedUserId)->first()) {
    Log::info("Processing User ID: {$user->id}");
     
    // Your long work here to process user
    // Ex. Calling Billing API, Preparing Invoice for User etc.
     
    $processedUserId = $user->id;
    Cache::put(self::CACHE_KEY, $processedUserId, now()->addSeconds(3600)); // Updating last processed item id
     
    if (app('queue.worker')->shouldQuit) {
    $this->job->release(60); // Requeue the job with delay of 60 seconds
    break;
    }
    }
     
    Log::info("All users have processed successfully!");
    }
    }

    Tip given by @a-h-abid

    Read all 376 tips on GitHub

  • Laravel 11: New Artisan "make:interface" Command
    · 1 min, 110 words

    Laravel 11: New Artisan "make:interface" Command

  • Laravel 11: New Artisan "make:enum" Command
    · 1 min, 159 words

    Laravel 11: New Artisan "make:enum" Command

  • Laravel 11: New Artisan "make:class" Command
    · 1 min, 194 words

    Laravel 11: New Artisan "make:class" Command

  • How to Structure Laravel 11 Projects

    · 11041 words

    Premium Course: How to Structure Laravel 11 Projects

  • Laravel 11: Main New Features and Changes
    · 7 mins, 1314 words

    Laravel 11: Main New Features and Changes

  • Laravel 11: How to Change Default SQLite to MySQL
    · 1 min, 171 words

    Laravel 11: How to Change Default SQLite to MySQL

  • Caching in Laravel with Redis: Simple Example
    · Updated Mar 2024 · 3 mins, 477 words

    Caching in Laravel with Redis: Simple Example

  • Virtual DB Columns in Laravel Migrations and MySQL
    · 5 mins, 890 words

    Virtual DB Columns in Laravel Migrations and MySQL

  • CarbonImmutable Class: Why You Would Need It
    · 2 mins, 291 words

    CarbonImmutable Class: Why You Would Need It