Latest Premium Tutorial
-
· 11 mins, 2097 words · premium
Laravel Reverb Demo: Real-Time Notification on Completed Task
Some Laravel tasks are running in the background. What if you want to "listen" for those events to finish? Let's implement exactly this real-time feedback with the Reverb server.
Newest content
-
· 2 mins, 215 words
This Week: 40% Discount for Yearly and Lifetime Plans!
-
· 1 min, 107 words
Laravel 11: New Artisan "make:trait" Command
-
Random Quick Laravel Tip:
Pause a long running job when queue worker shuts downWhen 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.
<?phpnamespace 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 idif (app('queue.worker')->shouldQuit) {$this->job->release(60); // Requeue the job with delay of 60 secondsbreak;}}Log::info("All users have processed successfully!");}}Tip given by @a-h-abid
-
· 1 min, 110 words
Laravel 11: New Artisan "make:interface" Command
-
· 1 min, 159 words
Laravel 11: New Artisan "make:enum" Command
-
· 1 min, 194 words
Laravel 11: New Artisan "make:class" Command
-
Premium Course: How to Structure Laravel 11 Projects
-
· 7 mins, 1314 words
Laravel 11: Main New Features and Changes
-
· 1 min, 171 words
Laravel 11: How to Change Default SQLite to MySQL
-
· Updated Mar 2024 · 3 mins, 477 words
Caching in Laravel with Redis: Simple Example
-
· 5 mins, 890 words
Virtual DB Columns in Laravel Migrations and MySQL
-
· 2 mins, 291 words
CarbonImmutable Class: Why You Would Need It