TeaCupApp
TeaCupApp

Reputation: 11452

Handling php script errors, while executing it by cron

I have a MVC framework with a controller in it. A controller downloads images from server. I need to refresh my database with those images every 5 minutes. So, I planned to create php script which downloads the file and persists it to my database. In order to do this every 5 minutes. I will be setting up Cron job.

Now the question is,

  1. What is the best practise to handle errors inside php script?

    Because Cron will keep executing at every 5 minutes without knowing that the last queried image is already lost and not being saved.

  2. How do I notify myself that something unusual happend and I need to maintain the DB consistency by my self (Which I don't mind for few instances).

Upvotes: 1

Views: 1953

Answers (3)

Mike Brant
Mike Brant

Reputation: 71384

In addition to the other comments. I have often found it useful in cron scripts that could run into problems or take longer than the desired execution interval to where multiple execution instances could be running, to provide some text file that indicates last execution time, execution success, etc. that you can inspect to determine if the script should run as scheduled. It could be something as simple as writing a file at script start and deleting it on successful execution, and then checking for this file on next execution to decide whether to run or not.

Upvotes: 1

Robert
Robert

Reputation: 2471

Use try-catch along with database transactions (if possible). You can dump errors to error_log() and either set that up to generate email or add email to your error handler.

Upvotes: 1

zb'
zb'

Reputation: 8059

What is the best practise to handle errors inside php script? Because Cron will keep executing at every 5 minutes without knowing that the last queried image is already lost and not being saved.

use asserts as described here: http://php.net/manual/en/function.assert.php

How do I notify myself that something unusual happend and I need to maintain the DB consistency by my self (Which I don't mind for few instances).

use mail() in asserts

Upvotes: 1

Related Questions