Metainformationen zur Seite
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| start [2026/02/17 19:59] – angelegt ahummel | start [2026/02/17 21:05] (aktuell) – gelöscht ahummel | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| - | Write | ||
| - | Notifications | ||
| - | ⚙️ Demystifying Laravel Jobs & Queues: Best Practices for Scalable Task Processing | ||
| - | Sandeeppant | ||
| - | Sandeeppant | ||
| - | 3 min read | ||
| - | · | ||
| - | May 28, 2025 | ||
| - | |||
| - | While building your web application, | ||
| - | #Creating Jobs | ||
| - | |||
| - | By default, all of the queueable jobs for your application are stored in the app/Jobs directory. If the app/Jobs directory doesn' | ||
| - | |||
| - | php artisan make:job SendEmail | ||
| - | |||
| - | This creates a job class in app/ | ||
| - | |||
| - | use App\Mail\WelcomeMail; | ||
| - | use Illuminate\Support\Facades\Mail; | ||
| - | |||
| - | public function handle() | ||
| - | { | ||
| - | Mail:: | ||
| - | } | ||
| - | |||
| - | # | ||
| - | |||
| - | To queue a job, you dispatch it like so: | ||
| - | |||
| - | SendEmail:: | ||
| - | |||
| - | You can delay it too: | ||
| - | |||
| - | SendEmail:: | ||
| - | |||
| - | ⚙️ Queue Configuration | ||
| - | |||
| - | Laravel supports different queue drivers out of the box, like: | ||
| - | Press enter or click to view image in full size | ||
| - | |||
| - | Set your preferred driver in .env: | ||
| - | |||
| - | For high-throughput apps, Redis is often the go-to choice. | ||
| - | |||
| - | QUEUE_CONNECTION=redis | ||
| - | |||
| - | Configure Redis queues in config/ | ||
| - | |||
| - | Then create the jobs table: | ||
| - | |||
| - | php artisan queue:table | ||
| - | php artisan migrate | ||
| - | |||
| - | 🧾 Running the Queue Worker | ||
| - | |||
| - | php artisan queue:work | ||
| - | |||
| - | You can also run it as a daemon with: | ||
| - | |||
| - | php artisan queue:work --daemon | ||
| - | |||
| - | #Job Chaining: | ||
| - | |||
| - | Laravel supports job chaining — a powerful way to run multiple jobs in sequence. | ||
| - | |||
| - | Bus:: | ||
| - | new ProcessPodcast($podcast), | ||
| - | new OptimizePodcast($podcast), | ||
| - | new ReleasePodcast($podcast), | ||
| - | ])-> | ||
| - | |||
| - | If any job in the chain fails, the remaining jobs won’t be executed. | ||
| - | Combine with catch() to handle failure: | ||
| - | |||
| - | Bus:: | ||
| - | -> | ||
| - | // Handle chain failure | ||
| - | })-> | ||
| - | |||
| - | #Job Batching: | ||
| - | |||
| - | Batching allows you to dispatch a group of jobs together and track their collective progress. | ||
| - | |||
| - | Bus:: | ||
| - | new ResizeImage($image), | ||
| - | new ApplyWatermark($image), | ||
| - | new UploadToCDN($image), | ||
| - | ])-> | ||
| - | // All jobs completed | ||
| - | })-> | ||
| - | // Handle failed job | ||
| - | })-> | ||
| - | // After all jobs (success or failure) | ||
| - | })-> | ||
| - | |||
| - | To use batching, ensure Queue:: | ||
| - | |||
| - | php artisan queue: | ||
| - | php artisan migrate | ||
| - | |||
| - | #Job Middleware | ||
| - | |||
| - | Middleware can apply behaviors before or after the job executes: | ||
| - | |||
| - | Rate limiting | ||
| - | Preventing duplicates | ||
| - | Locking | ||
| - | |||
| - | Example — prevent overlapping: | ||
| - | |||
| - | use Illuminate\Queue\Middleware\WithoutOverlapping; | ||
| - | |||
| - | public function middleware() | ||
| - | { | ||
| - | return [new WithoutOverlapping($this-> | ||
| - | } | ||
| - | |||
| - | You can also use: | ||
| - | |||
| - | ThrottlesExceptions | ||
| - | RateLimited | ||
| - | Custom middleware for retry/ | ||
| - | |||
| - | #Retry Strategies and Backoff | ||
| - | |||
| - | Each job can define how long to wait before retrying: | ||
| - | |||
| - | public function backoff() | ||
| - | { | ||
| - | return [10, 30, 60]; // retry after 10s, then 30s, then 60s | ||
| - | } | ||
| - | |||
| - | You can also control maximum retries: | ||
| - | |||
| - | public $tries = 5; | ||
| - | public $maxExceptions = 3; | ||
| - | |||
| - | To globally set retry strategy, use config/ | ||
| - | #Job Failures, Timeouts, and Alerts | ||
| - | |||
| - | Jobs may fail due to: | ||
| - | |||
| - | External API issues | ||
| - | Timeouts | ||
| - | Exceptions | ||
| - | |||
| - | You can handle job failure gracefully with failed(): | ||
| - | |||
| - | public function failed(Throwable $exception) | ||
| - | { | ||
| - | Log:: | ||
| - | } | ||
| - | |||
| - | Timeouts: | ||
| - | |||
| - | public $timeout = 120; // seconds | ||
| - | |||
| - | To alert via Slack or email, use Notification classes in failed(). | ||
| - | #Scheduling Queued Jobs | ||
| - | |||
| - | Queued jobs can also be scheduled via Laravel’s task scheduler: | ||
| - | |||
| - | $schedule-> | ||
| - | |||
| - | Press enter or click to view image in full size | ||
| - | Conclusion | ||
| - | |||
| - | Laravel’s job and queue system is not just about sending emails — it’s a gateway to building resilient, scalable, event-driven architectures. With advanced techniques like batching, chaining, middleware, and Horizon monitoring, you can turn your Laravel app into a finely-tuned asynchronous machine. | ||
| - | |||
| - | Laravel | ||
| - | Queue | ||
| - | Programming | ||
| - | PHP | ||
| - | Software Development | ||
| - | |||
| - | Sandeeppant | ||
| - | Written by Sandeeppant | ||
| - | 88 followers | ||
| - | · | ||
| - | 5 following | ||
| - | |||
| - | Laravel Vue Js Developer https:// | ||
| - | No responses yet | ||
| - | Anton Hummel | ||
| - | Anton Hummel | ||
| - | |||
| - | | ||
| - | More from Sandeeppant | ||
| - | Master the Art of API Design with Laravel: Best Practices for 2026 | ||
| - | Sandeeppant | ||
| - | |||
| - | Sandeeppant | ||
| - | Master the Art of API Design with Laravel: Best Practices for 2026 | ||
| - | Building a robust API is more than just returning JSON from a controller. It’s about creating a predictable, | ||
| - | Jan 9 | ||
| - | 4 | ||
| - | IDEMPOTENCY IN LARAVEL CONSISTENT OPERATIONS | ||
| - | Sandeeppant | ||
| - | |||
| - | Sandeeppant | ||
| - | IDEMPOTENCY IN LARAVEL CONSISTENT OPERATIONS | ||
| - | In modern web applications, | ||
| - | Feb 3 | ||
| - | 1 | ||
| - | Mastering API Development in Laravel: A Step-by-Step Guide with Best Practices | ||
| - | Sandeeppant | ||
| - | |||
| - | Sandeeppant | ||
| - | Mastering API Development in Laravel: A Step-by-Step Guide with Best Practices | ||
| - | APIs (Application Programming Interfaces) are the backbone of modern web and mobile applications, | ||
| - | Apr 15, 2025 | ||
| - | 33 | ||
| - | 3 | ||
| - | Middleware in Laravel 12: A Step-by-Step Guide | ||
| - | Sandeeppant | ||
| - | |||
| - | Sandeeppant | ||
| - | Middleware in Laravel 12: A Step-by-Step Guide | ||
| - | Starting in Laravel 11, the configuration of the middleware changed from using the HTTP Kernel to the application’s bootstrap file. Though… | ||
| - | Mar 29, 2025 | ||
| - | 15 | ||
| - | 2 | ||
| - | See all from Sandeeppant | ||
| - | Recommended from Medium | ||
| - | Laravel Best Practices: SOLID, Clean Architecture & Design Patterns | ||
| - | Paulo Martins | ||
| - | |||
| - | Paulo Martins | ||
| - | Laravel Best Practices: SOLID, Clean Architecture & Design Patterns | ||
| - | Laravel Best Practices: SOLID, Clean Architecture & Design Patterns | ||
| - | Feb 9 | ||
| - | 25 | ||
| - | 1 | ||
| - | Building Smooth CSV Imports with Laravel Job Batches and Real-time Progress Tracking | ||
| - | Developer Awam | ||
| - | |||
| - | Developer Awam | ||
| - | Building Smooth CSV Imports with Laravel Job Batches and Real-time Progress Tracking | ||
| - | Learn how to upload CSV in Laravel using Queue & Batch Job with a realtime Livewire progress bar for smooth data import. | ||
| - | Sep 2, 2025 | ||
| - | 71 | ||
| - | 1 | ||
| - | Mastering the Strategy Pattern in Laravel: From Chaos to Clean Code | ||
| - | MasteryOfLaravel | ||
| - | |||
| - | MasteryOfLaravel | ||
| - | Mastering the Strategy Pattern in Laravel: From Chaos to Clean Code | ||
| - | Strategy Pattern in Laravel: Transform Payment Processing from If-Else Hell to Clean, Testable Code with Real-World Examples | ||
| - | Jan 4 | ||
| - | 5 | ||
| - | Why Your Laravel App Slows Down in Production (And How to Fix It) | ||
| - | Gaál Gergely | ||
| - | |||
| - | Gaál Gergely | ||
| - | Why Your Laravel App Slows Down in Production (And How to Fix It) | ||
| - | Laravel in Production: 5 Critical Pitfalls You Must Avoid | ||
| - | Feb 4 | ||
| - | Laravel DTO Mapper: Type-Safe Request Handling with PHP 8 Attributes | ||
| - | Jakub Skowron (skowron.dev) | ||
| - | |||
| - | Jakub Skowron (skowron.dev) | ||
| - | Laravel DTO Mapper: Type-Safe Request Handling with PHP 8 Attributes | ||
| - | The Problem We All Face | ||
| - | Oct 17, 2025 | ||
| - | 115 | ||
| - | 4 | ||
| - | What Happens When You Misunderstand == vs === in PHP? | ||
| - | Ann R. | ||
| - | |||
| - | Ann R. | ||
| - | What Happens When You Misunderstand == vs === in PHP? | ||
| - | Discover why PHP’s loose comparison (==) causes dangerous production bugs. Learn the mechanics of type juggling, see real-world failure… | ||
| - | Feb 9 | ||
| - | 163 | ||
| - | See more recommendations | ||
| - | |||
| - | Help | ||
| - | |||
| - | Status | ||
| - | |||
| - | About | ||
| - | |||
| - | Careers | ||
| - | |||
| - | Press | ||
| - | |||
| - | Blog | ||
| - | |||
| - | Privacy | ||
| - | |||
| - | Rules | ||
| - | |||
| - | Terms | ||
| - | |||
| - | Text to speech | ||