Tanzeem
Tanzeem

Reputation: 109

How to avoid timeout when running time consuming jobs in Laravel

In my laravel application i do a form submit to execute a time consuming task by calling shell_exec(my_time_conusming_Script.sh) . The process executes successfully on the background. But the connection with the frontent is found to be lost. I find that the form submitting the data stops after processing for sometime. I had a return redirect()->back()->with('success',$message); after the shell_exec. But the redirect is not found to be working. if i comment out the time consuming process the redirect works. is any timeout happening. How should i get the redirect working after the time consuming process completes.Should i use laravel queues and jobs and broadcasting.

Upvotes: 0

Views: 441

Answers (1)

Nicola Lazzaro
Nicola Lazzaro

Reputation: 46

Although increasing the timeout may be a solution, I do not recommend it as the client should receive a response in the shortest time possible for an optimal user experience. It is not clear to me whether the outcome of the script execution affects the response to be returned to the client, i.e. with a success message rather than an error message. If this isn't the case, and you want to avoid dealing with queues that can be more complex, I would suggest using terminable middleware, and then return a success response to your client and the middleware will take care of executing right after the script runs. Please note that this will only work if your web server uses FastCGI.

Upvotes: 0

Related Questions