What is the proper way to monitor PHP execution in the frontend?
I will use an example to demonstrate this.
Assuming I have a MySQL DB where I place paths to files to be uploaded to S3, and a status column where each file is attributed either a pending
or uploaded
string.
I have a PHP script, upload.php
, which I can run with php upload.php
and receive the output logged to my terminal as the script progresses. I would like to set up a cron job that runs the script at certain intervals, say every 30 minutes, where each time the DB is queried and the files which hold a pending
status are processed for upload.
Now, I want to be able to track the progress of the script, regardless of its current status in the frontend (if currently no pending items are in the DB).
While I would appreciate any specific suggestion on how to do this, my question is also regarding best practice – meaning, what is the proper way to do this?
Here’s an example of a script of such (it’s using the Joshcam MysqliDb)
// Get items with a pending status function get_items_queue() { global $db; $cols = Array ("id", "filename"); $db->where('status = "pending"'); return $db->get('files', null, $cols); } // Upload items to S3 function UploadToS3($filename) { if (empty($filename)) { return false; } include_once('/s3/aws-autoloader.php'); $s3 = new S3Client($somearray); // Some S3 credentials here // Print status echo $filename . ' is uploading'; $uploaded = $s3->putObject($somearray); // Uploading to S3 if ($s3->doesObjectExist($s3_bucket, $filename)) { // Print status echo $filename . ' was uploaded'; } else { // Print status echo 'There has been an issue while uploading ' . $filename; } } // Run the script $queue_items = get_items_queue(); foreach ($queue_items as $key => $item) { $upload = UploadToS3($item['filename']); // Some function here that changes the status column for the uploaded item to 'uploaded' if ($upload) { set_item_queue_status($item['id']); } }
I ended up setting an installation of Cronicle from jhuckaby.
Essentially a cron manager, but what’s most important for my case is the live log-viewer. This enables me to run the script using a cron job at the intervals I defined, and watch as it executes via the log-viewer, while being able to leave and come back at any point to view the currently running task (or any of the previous tasks that ran while I was away).