Answer the question
In order to leave comments, you need to log in
Laravel queues. How to perform tasks in the background?
In the controller I use the following code:
(practically an example from the documentation )
$job = (new DownloadItem($data))->onQueue('save');
dispatch($job);
return redirect('/cabinet/itemlist');
Answer the question
In order to leave comments, you need to log in
We read the documentation carefully: https://laravel.com/docs/queues
In short, by default your env uses QUEUE_DRIVER=sync
, which essentially means "no queues, do everything at once". If you want queues to run in the background, you need to change the driver to one of the appropriate options. For example, if you change to database
and create the necessary tables in the database, then all tasks will be placed in these tables. To execute them, you need to either start php artisan queue:work
(one-time execution of all tasks), or php artisan queue:work --daemon
(a daemon that will hang and monitor the addition of jobs to the queue), or, best of all, use supervisor , which will itself make sure that the daemon hangs in memory.
All this is easily googled, keywords in the previous paragraph. It makes sense to use Supervisor only in production, but if you don’t want to bother with installing and configuring it, it will be enough to add only the start of the queue daemon to autorun. True, if for some reason the process crashes, you will need to manually restart it.
The only thing that comes to mind is to either pass the task to an external application, or try running the task on a separate thread, but with PHP, I'm not sure how well this idea will work, or if it will work at all, because PHP has a parameter "script time limit", "number of pools", in other words, initially PHP is not designed to work "in real time".
Usually, schedulers are used for tasks, for example, write a list of tasks to be done to the database, and the scheduler walks through them every N minutes, checks and executes them according to the list respectively. Or, as I wrote above, transfer control to an external application (if you really want to, it can be a PHP script running in CLI mode [i.e. without a web server], which has no runtime restrictions).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question