I did not find the answer to this question at all. Hope someone here can help.
I have a PHP script (specifically a WordPress template) that automatically imports and processes images when the user clicks on it. The problem is that image processing takes up a lot of memory, especially if several users access the template at the same time and start image processing. Because of this, my server crashed several times.
My solution was not to perform the image processing function if it was already running. Before running the function, I would check the database record named image_import_running to see if it is set to false. If so, the function would then be executed. The very first thing the function did was set as image_import_running to true. Then, after everything was finished, I returned it to false.
It worked great - theoretically. Since then, the site has not crashed, I can tell you this. But there are two main problems:
If the user closes the page while it is loading, the script never completes the image processing and therefore never sets the image_import_running image to false. The template will never process the images again until it manually sets the value to false.
If the script expires during image processing - and this is a strong possibility, if there are a lot of images in the queue - you have essentially the same problem as No. 1: the script never reaches the point at which image_import_running returns false.
To handle # 1 (the first of two issues I realized), I added ignore_user_abort(true) to the script. Did it work? I do not know, because No. 2 is still a problem. That I'm at a standstill.
If I could ask the server if the script is working or not, I could do something like this:
if($import_running && $script_not_running) { $import_running = false; }
But how to set this $script_not_running variable? It hits me.
I told you this whole story just in case you have another brilliant solution.
source share