0

I have a long running script which gets a (long) array of folders (with subarray of files in that folder) where I have to do several actions on each file.

What is the best way to make sure I make all actions successful? And how to handle unsuccessful actions?

Lets say what will happen if my mysql server is unavailable or like the Amazon S3 API is not working correctly.

pseudocode of my script:

  1. starting script with folders / files array
  2. looping through each folder
  3. looping through each file in that folder
  4. open file (from external server) and try converting it to custom object (only continue if file is a valid "object")
    1. extract some parts of file and save them to Amazon S3 bucket
    2. extract some other parts of file and save them to another Amazon S3 bucket
    3. extract metadata / text of file and insert into elasticsearch
    4. update mysql record
Cœur
  • 37,241
  • 25
  • 195
  • 267
Floris
  • 299
  • 3
  • 17
  • your answer is Exceptions :) Look at PHP docs about them ;) – Svetoslav Apr 02 '14 at 13:41
  • just so you do not have to google it: http://www.php.net/manual/en/language.exceptions.php – Andresch Serj Apr 02 '14 at 14:32
  • @Floris consider accepting my answer to prevent this question from getting more unneeded attention, or if my answer didn't solve your problem, use the comments section to ask for further detail. – Andresch Serj Apr 28 '14 at 09:53

1 Answers1

0

As mentioned, what you could do is throw and catch Exceptions.

So for instance, if you iterate over files in a folder using a foreach, doing something with those files, on an error, you can throw an Exception and it will stop code execution till it is catched.

So maybe you want to use a logger instead. Since it is 2014, you probably want to use a DIC to inject a logger service or otherwise, you can just use a singleton (only considering the great flaws that brings) that stores your errors.

So either way you have this service that stores every error. At the end you just check if it has any errors and then act accordingly.

Community
  • 1
  • 1
Andresch Serj
  • 35,217
  • 15
  • 59
  • 101