1

I am running a php script on a website that creates thumbnails from a big image. The script uses imagick to generates 10 thumbnails per image.
It runs on first page load, when the thumbnails don't exist yet and can potentially be asked to generate hundreds of thumbnails on first page load. This takes time (about 2.5 seconds per batch of 10 thumbnails) and therefore the server returns a 504 Gateway Timeout error.

This error means:

The server, while acting as a gateway or proxy, did not receive a timely response from the upstream server specified by the URI... [ source ]

Which I can understand as the php script can be running for 60 seconds or more.

My question is : does this mean that the script has crashed the server and that it has stoppped during it's execution?
Or does this just mean that my browser is to impatient to wait for the server response?

My main concern is that the script craches during the thumbnail generation and can potentialy generate corrupted files.

1

A Gateway Timeout error does not usually mean that your server crashed. Neither do browsers generate these errors. It means that an intermediate server got tired of waiting for your primary server.

When you get these errors, it usually means that there is a proxy server involved. You might have a reverse proxy from one port to another. You might be using a CDN or a load balancer.

You should be worried about corrupt files. Generating files on first load is not usually a good plan. Most requests to have a time limit before the web server kills off the process. For example if you are kicking this process off through PHP, you would need to set a long enough http://php.net/manual/en/function.set-time-limit.php

There is also a race condition that you have to consider. What happens when a second request comes in while the first is still processing? Does it also generate images? Two processes trying to write images to the same locations can cause corruption.

Jobs that take more than a few seconds should be handled differently. You have a few options. You could run them asynchronously and have web requests check the status. You could resize the images after they are uploaded, or as part of the website deployment. You could have a cron job that periodically checks to see if images need to be resized.

  • Thanks for your answer. I think I will have to consider changing my approach for the thumbnail generation. I will investigate the options you listed. – web-tiki Mar 15 '18 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.