Bogdan Lashkov
Bogdan Lashkov

Reputation: 359

How can I make asynchronous request with Laravel?

In my Laravel 5.4 web app user can request report generation that takes a couple of minutes due to a big amount of data. Because of these he couldn't work with application no more, until report will be generated. To fix this problem I have read about queues in laravel and separated out my report generation code to the job class, but my app still holds until report will be generated. How can I fix that?

To be absolutely clear I will sum up my problem:

  1. User make request for report generation (my app absolutely holds at this moment)
  2. My app receives POST request in routes and calls a function from the controller class.
  3. Controller's function dispatches a job, that should generate report and put it into the client web folder.

Upvotes: 1

Views: 4750

Answers (2)

Jerodev
Jerodev

Reputation: 33186

By default, Laravel uses the sync queue driver. This driver executes the queued jobs in the same request as the one they are created in. So this won't make any difference.

You should take a look at other drivers and use the Laravel queue worker background process to execute jobs to make sure they don't hold the webrequest from completing.

Upvotes: 1

Lorna Mitchell
Lorna Mitchell

Reputation: 1986

It sounds like you have already pretty much solved the problem by introducing a queue. Put the job in the queue, but don't keep track of its progress - allow your code to continue and return to the user. It should be possible to "fire-and-forget", and then either ask the user to check if the report is ready in a couple of minutes, or offer the ability to email it to them when it is completed.

Upvotes: 3

Related Questions