I am currently working on a PHP/MySQl application. A 500 error is thrown if there is an error where by the application cannot recover and cannot continue executing:
- Could not connect to the database.
- Running out of disk space
- etc.
I would like to be emailed/smsed if such an error occurs. The problem is that I do not wish to be emailed everytime a 500 error is triggered. If the site is very busy, then in a short span of time, one can easily receive hundreds of emails or SMSes.
I thought about sending an email when the error first occurs, then setting a flag which prevents an email from being sent within the next 20 minutes. However this leads to another issue. If the flag is stored in the database, or in a file on the filesystem, then not being able to connect to the database or running out of disk space would make the flag useless.
There’s also the possibility that the SMTP server for sending emails might go down, but in that case, I don’t think anything can really be done, and an external monitoring service would be required.
Are there any better ways of doing this?
Nope, your solution is probably the best [simple] option. But do it with a pre-existing file. Create it somewhere on the filesystem so it exists even if space runs out. Use
touch()to update the modification time every time a 500 error occurs. If thefilemtime()was in the last [x] minutes, don’t send out the email.