I add job on queue on page load controller and invoke laravel event and broadcast it via socket io to front end. Problem is since this is done on page load, the job is executed before the page fully loaded. As a result, I can see the response appended for a short time while the page loads and disappeared when its fully loaded. Why is that so?
-
I doubted if the connection is on sync instead of redis. Upon checking .env and config/queue.php uses redis as default.
-
DispatchNow is working fine but dispatch is not. Is it due to this the response sent immediately before the page settled down?
-
In front end, I added the code to connect with socket inside document ready to ensure it's done after the dom is loaded. But doesnt help, it behaves the same.
-
I tried other workaround where I fire an ajax call to the queue job once the specific DOM element is visible, and it works fine.
But I want it to be called on the page controller itself instead of a separate ajax.
In controller:
$sellings = curl(...some call to external url); SendOrder::dispatchNow($sellings, Auth::id()); return view('home');
In SendOrder job:
public function handle() { // Allow only 2 emails every 1 second Redis::throttle('any_key')->allow(2)->every(1)->then(function () { event(new DashboardEvent('job1', $this->order, $this->user)); Log::info('job 1done'); }, function () { // Could not obtain lock; this job will be re-queued return $this->release(2); }); }
.env:
BROADCAST_DRIVER=redis CACHE_DRIVER=redis QUEUE_CONNECTION=redis SESSION_DRIVER=redis
config/queue.php
'default' => env('QUEUE_CONNECTION', 'redis'), ........ ......... 'redis' => [ 'driver' => 'redis', 'connection' => 'default', 'queue' => env('REDIS_QUEUE', 'default'), 'retry_after' => 90, 'block_for' => null, ],
https://stackoverflow.com/questions/66863854/laravel-queue-event-executed-immediately-before-page-fully-loaded March 30, 2021 at 10:05AM
没有评论:
发表评论