Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

gianmarx's avatar

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

0 likes
5 replies
gerardnll's avatar

You'll have to send the job to the queue. HTTP Requests have a maximum execution time and it's good there is.

gianmarx's avatar

@gerardnll putting everything in a queue shouldn't the execution times remain unchanged even if managed asynchronously?

then what would you make the queue handle just the query, the generation of the file or the whole process?

Sinnbeck's avatar

@gianmarx queue uses cli, which has a seperate php.ini file, with different values.

1 like
gianmarx's avatar

@Sinnbeck so if I did generate the CSV file through a queue I could avoid the "Fatal error: Maximum execution time of 30 seconds exceeded"?

gerardnll's avatar
Level 1

@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.

2 likes

Please or to participate in this conversation.