In my app a user can upload several photographs at once.
Once these are received by the server the following happens:
- A Gearman worker job is scheduled which processes all the uploaded images, creating various different crop sizes and applying Lancosz filtering before finally writing them to disk. Depending on the number and resolution of photographs uploaded this process may take up to around 10 seconds.
- Within the main PHP thread the database is updated to reflect the uploaded images.
Although everything works fine, there is no guarantee that the Gearman worker has completed by the time the database has updated. Meaning that a user may potentially get broken image links if they access a page which queries the database for those images.
Therefore I need to do one of the following:
- Move my database updating code into the Gearman worker so that it completes once the image processing has finished.
- Temporarily, in the main PHP thread, place the full size images in ALL the locations, duplicating the full size image several times into the various differend directories ("/assets/images/cropped/image.jpg", "/assets/images/thumbnail/image.jpg" etc) waiting for them to be overwritten with the smaller cropped and filtered versions later when the Gearman worker completes.
For obvious reasons solution number 1 makes the most sense although of course that will have the drawback of potentially causing a delay between the user uploading the images and he/she actually being able to view them within the application.
Therefore this is really 2-3 questions in one:
- Is solution 1 or 2 the best way to go, if so why? And is there a much better way of managing this situation that I am not seeing?
- If I go with solution 1 is there anyway to manage the potential delay caused by updating the database within the worker?
- If I choose to go with solution 1 I have a problem as I am relatively new to object oriented programming in PHP. As explained below:
If I do indeed go with solution 1 I need to be able to access the public functions for my main Applications database class and memcached class as well as the class of the function which called the Gearman worker itself, all from within the Gearman worker.
For example, in my main app to perform a MySQL PDO database query I use $query = new Query($sql);
then bind the values and execute it etc. And at the top of that file I have use \App\Query
so that I have access to those public functions. In other instances I would be accessing a function from within another function but in the same class using self::functionToPerform();
The Gearman workers scripts are external to my actual App so how would I go about accessing these functions from within the worker?
I know this is a lot to ask from one question and I hope I have articulated it well enough. Any help really would be appreciated.