October 21, 2024
Chicago 12, Melborne City, USA
PHP

Maximum Execution Time Question “Maximum execution time of 60 seconds exceeded”


I’m interested in getting more insight into how the Maximum Execution time on PHP/Laravel works. I tried to google it myself, but wasn’t sucessful.
Here is the problem.
I wrote a piece of code in Laravel that gets data from a API, extract wanted data from the response and then saves it to the database. (Essentially a product importer)
At first I had the whole code in a single function and everything seemed to be working fine. The whole import ran for about 45 minutes (due to the API request limitations). I never once got a "Maximum execution time of 60 seconds exceeded" fatal error.
Then once I got everything working I decided to start refactoring my code and make it into little separate functions. The importer logic stayed the same and nothing was added just moved to separate functions. Once I had done that and ran the importer again voila I got the good old "Maximum execution time of 60 seconds exceeded" error. Which is quite baffeling to me because the nature of the importer stayed the same it was just split up into smaller understandable functions.
Furthermore, I could not understand what was causing the issue, so I thought I’d try my luck here. My aim isn’t to fix the issue, because I can do that just by set_time_limit(n); at the start of each loop.
I am more interested in what could have caused the issue and how exactly does the maximum execution time work and behave. I would be very thankful to any documentation or explenation.
Thank you in advance.
P.S I would put both of the code snippets here, but I wasn’t using version control at that time (silly me)

public function importItems(): void
    {
        $totalRequests = 1;
        $totalItems = 0;
        $start = 0;
        $errorCount = 0;

        Log::info('IMPORTER: Started');
        $apiConnector = new APIConnector();

        for ($i = 1; $i <= $totalRequests; $i++) {
            $requestStartTime = microtime(true);
            $response = $apiConnector->getData('1', $start, 100, $this->Id, 'price', 'asc');

            if (!$this->responseValidation($response)) {
                if ($errorCount >= 3) {
                    continue;
                }
                $errorCount++;
                $i--;
                continue;
            } else {
                $errorCount = 0;
            }

            $response = $response->json();
            $start = $response['start'] + $response['pagesize'];

            if ($i == 1) {
                $totalItems = $response['total_count'];
                $totalRequests = $this->calculateTotalRequests($totalItems);
            }

            $this->saveItemsToDatabase($this->extractItemsData($response['results']));
            Log::info('IMPORTER: ' . $start . '/' . $totalItems . ' imported');
            //Needs to sleep due to request limit
            usleep(12000000 - ((microtime(true) - $requestStartTime) * 1000000) + 1000000);

            //usleep($this->calculateSleepTime($this->calculateRequestTime($requestStartTime, microtime(true))));
        }
        Log::info('IMPORTER: Complete');
    }

I tryed using a Log to determin when and where in the code the error happens. It happens during the sleep time between each request which is about 12 seconds and it happens on the 5th loop, which makes sense as one loop takes just a little over 12 seconds.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video