Latest Premium Tutorial
-
· 11 mins, 2098 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
-
· 4 mins, 672 words
Filament Dependent Dropdowns in Edit Form: Set Select Values
-
· 1 min, 140 words
Filament: Remove Label Text in Resource Form Page
-
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
-
· 6 mins, 1068 words
Filament Repeater: Set Values Manually or From Other Fields
-
· 4 mins, 716 words
Filament: Money Columns and Storing Value in Cents
-
· 1 min, 193 words
Laravel Jetstream: Redirect After Login
-
Premium Course: Laravel 11 Multi-Tenancy: All You Need To Know
-
· 1 min, 186 words
Filament Select with Relationship: Custom Label for titleAttribute Options
-
· 3 mins, 480 words
Filament Select with Relationship: ModifyQueryUsing for Edit Form
-
· 3 mins, 405 words
Filament Register: Automatically Assign Role from Spatie Permission
-
· 2 mins, 345 words
Filament: Show/Hide Field Based on Select Value with Relationship Column
-
· 2 mins, 249 words
Filament: Show/Hide Fields based on Roles and Permissions