use a queue for sending multiple emails
How does the middleware::terminate() method work?
I thought that if I create a middleware with a terminate method, its content will be executed after the response is already sent back to the client. The following experiment disproved it:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Symfony\Component\HttpFoundation\Response;
class QueueEmailsAfterResponseIsSent
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
return $next($request);
}
/**
* Handle tasks after the response has been sent to the browser.
*/
public function terminate(Request $request, Response $response): void
{
sleep(15);
Log::info('Works after response is sent');
}
}
So I have to wait 15 seconds before the web browser is refreshed (and logs the message). But I thought that the appropriate behavior would be that the browser refreshes immediately, and then I would have to wait 15 seconds to see the newly created message in the log file.
I have read the Symfony HttpKerlen component's documentation, from which Laravel implements its terminate() functionality (so I am not really interested in that part). ...or maybe I have not paid enough attention?
Inside the Laravel's doc, it sas:
If you define a terminate method on your middleware and your web server is using FastCGI...
If I use the built-in server using the php artisan serve command, then the phpinfo() shows these lines:
CGI / FastCGI : Rasmus Lerdorf, Stig Bakken, Shane Caraveo, Dmitry Stogov FastCGI Process Manager : Andrei Nigmatulin, dreamcat4, Antony Dovgal, Jerome Loyet
... it is enough evidence for me, that FastCGI is used by the default dev server, isn't it?
So do I misunderstand the terminate() method, or does it work but not with the sleep(15) function? As you can see, I would use it to unload the current request-response lifecycle for sending multiple emails. Will it do its job?
Please or to participate in this conversation.