I’m trying to upload over 2,000 products with images from XML to my site. I wrote a script that does this. But this script takes a very long time and I can’t get the upload status of each product.
I don’t want advice like "make the execution time of the PHP script longer". I want to understand how to run very big scripts with PHP, divide tasks, and monitor each product’s loading status.
2
Answers
In such cases pagination is the answer.
So you set
limit
andoffset
on how many you process each iteration.So you have a CRON JOB, ajax or any other way visit url with page parameter.
then, in script.php would need to have:
your database query would need to have this limit and offest taken into account and then you do your processing of these products.
That is a "summary" of the approach.
You might want to have a controller script, that would read your XML file first, once a day, to find out how many entries there will be dealt with, then create a database that have a "queue" of work that needs to be done, pagination and statuses.. i.e. "not processed", "completed" and timestamps of when job was created and processed.
Then outside of this controller, have a cron job that looks up to database of table at certain intervals to process this products in chunks.
I would definitely use some sort of queueing system whether it’s rabbit, sqs, etc. This way you can parse records as they come into the system and offload tasks to individual queues. One queue would be responsible for handling database inserts, one for processing images, etc.
This would provide some easy ways to monitor each part of the process and provide context when and if things go wrong.
Here’s a really quick howto for PHP and rabbitMq: https://www.rabbitmq.com/tutorials/tutorial-one-php.html
Cheers!