Tutorials for queues

  • · 1 h 31 min

    Course: Queues in Laravel

    In this 1.5-hour course, you will learn what you need to run Laravel Jobs via Queues in the backgrou...

  • June 14, 2021 · 8:42

    Laravel Queues 101: Example with Sending Emails

  • March 20, 2021 · 14:39

    Laravel Code Review: Multi-Tenancy, Events and Queues

  • August 06, 2021 · 9:39

    Laravel Excel Export/Import Large Files: Bus Batch and Livewire

  • August 10, 2022 · 5:13

    Laravel: Optimize Queue Jobs To Be "Dumb" With Fewer Parameters

  • · 3 mins, 484 words

    Tutorial: How cron and Task Scheduler work in Laravel

  • 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.

    1<?php
    2 
    3namespace App\Jobs;
    4 
    5use App\Models\User;
    6use Illuminate\Bus\Queueable;
    7use Illuminate\Contracts\Queue\ShouldQueue;
    8use Illuminate\Foundation\Bus\Dispatchable;
    9use Illuminate\Queue\InteractsWithQueue;
    10use Illuminate\Queue\SerializesModels;
    11use Illuminate\Support\Facades\Cache;
    12use Illuminate\Support\Facades\Log;
    13 
    14class MyLongJob implements ShouldQueue
    15{
    16 use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    17 
    18 public $timeout = 3600;
    19 
    20 private const CACHE_KEY = 'user.last-process-id';
    21 
    22 public function handle()
    23 {
    24 $processedUserId = Cache::get(self::CACHE_KEY, 0); // Get last processed item id
    25 $maxId = Users::max('id');
    26 
    27 if ($processedUserId >= $maxId) {
    28 Log::info("All users have already been processed!");
    29 return;
    30 }
    31 
    32 while ($user = User::where('id', '>', $processedUserId)->first()) {
    33 Log::info("Processing User ID: {$user->id}");
    34 
    35 // Your long work here to process user
    36 // Ex. Calling Billing API, Preparing Invoice for User etc.
    37 
    38 $processedUserId = $user->id;
    39 Cache::put(self::CACHE_KEY, $processedUserId, now()->addSeconds(3600)); // Updating last processed item id
    40 
    41 if (app('queue.worker')->shouldQuit) {
    42 $this->job->release(60); // Requeue the job with delay of 60 seconds
    43 break;
    44 }
    45 }
    46 
    47 Log::info("All users have processed successfully!");
    48 }
    49}

    Tip given by @a-h-abid

    Read all 343 tips on GitHub

  • Code example: akaunting/akaunting

    Files in the example

    • app/Jobs/Common/CreateItem.php
    • app/Jobs/Common/CreateItemTaxes.php
    • app/Http/Controllers/Api/Common/Items.php
  • Code example: tighten/novapackages

    Files in the example

    • app/Jobs/CheckPackageUrlsForAvailability.php
    • app/Console/Commands/CheckPackageUrls.php
  • Code example: spatie/freek.dev

    Files in the example

    • app/Jobs/TweetPostJob.php
    • app/Actions/PublishPostAction.php
  • Package: spatie/laravel-failed-job-monitor

    Get notified when a queued job fails

  • Package: laravel/horizon

    Horizon provides a beautiful dashboard and code-driven configuration for your Laravel powered Redis queues. Horizon allows you to easily monitor key metrics of your queue system such as job throughput, runtime, and job failures.

  • Code example: familytree365/backend

    Files in the example

    • app/Jobs/DnaMatching.php
    • app/Http/Controllers/DnaController.php
  • Code example: christophrumpel/larastreamers

    Files in the example

    • app/Jobs/ImportYoutubeChannelStreamsJob.php
    • app/Console/Commands/ImportChannelStreamsCommand.php
  • Code example: bytefury/crater

    Files in the example

    • app/Jobs/GenerateInvoicePdfJob.php
    • app/Http/Controllers/V1/Invoice/InvoicesController.php
  • Code example: devhub-az/devhub

    Files in the example

    • app/Jobs/BanUser.php
    • app/Http/Controllers/Admin/UsersController.php
  • Code example: pixelfed/pixelfed

    Files in the example

    • app/Jobs/ContactPipeline/ContactPipeline.php
    • app/Http/Controllers/ContactController.php
  • Code example: Senither/ShareX-Media-Server

    Files in the example

    • app/Jobs/CalculateUsedDiskSpace.php
    • app/Observers/UrlObserver.php
  • Code example: OpenLitterMap/openlittermap-web

    Files in the example

    • app/Http/Controllers/ApiPhotosController.php
    • app/Jobs/UploadData.php