You'll have to send the job to the queue. HTTP Requests have a maximum execution time and it's good there is.
API Create CSV: Maximum execution time
i have a created an API in laravel 8.x that creates a csv file. Unfortunately when I get to generate CSV of 60000 lines I am given the error "Fatal error: Maximum execution time of 30 seconds exceeded".
the query takes about 10 seconds to process the data after which I go to create the CSV file. In the repository where I make the query I leave the Builder I do not make get (), cursor () or lazy (). Once I get the Builder my name is a Service that was created to generate a CSV, I create the file with fopen (...) and then I write in the file as follows:
foreach ($data->cursor() as $rows) {
$rows->each(function ($row) use ($remapper){
fputcsv($this->handle, $remapper($row));
});
}
I wonder if there is a way to avoid the "Maximum execution time". Whether there are other strategies always on the API side. I would not want to increase the time, I would like to avoid having the server disintegrate
@gianmarx
Put the whole logic into a Job (https://laravel.com/docs/8.x/queues#creating-jobs) .
Dispatch the job to the queue on the controller (https://laravel.com/docs/8.x/queues#dispatching-jobs). Set up your queue, using database, redis or whatever driver (https://laravel.com/docs/8.x/queues#driver-prerequisites).
Set up the queue worker (https://laravel.com/docs/8.x/queues#running-the-queue-worker). The job is going to be executed by this process and because is running under the cli/php.ini, it usually has an unlimited execution time. FYI, the PHP processes that process HTTP requests are normally set up as FPM and so they run the fpm/php.ini.
You'll have to do something to check if the job has finished processing.
Please or to participate in this conversation.