Perl watchdog code

From miscellus

Perl watchdog script

This is really just an aide-memoir for myself and maybe of use to others.

Always liking to use ready made solutions, when I looked for a simple piece of code to prevent some beta perl code of mine from melting the core of this server, all I could find was Proc::Watchdog, which did not suite my purpose as this lib monitors for hung processes. I just needed something I could simply insert into a sequence of possibly never-ending loops to prevent them running forever. This is the code and it does the job perfectly.

# die if the loop is called more than 150 times
sub watchdog {
       my $self = shift;
       my $dog_name_or_number = shift || 'Un-named watchdog';
       my $fail_count = 150;
       $h_watchdog->{$dog_name_or_number} ++;
       die "Watchdog $dog_name_or_number exceeded $fail_count cycles" if $h_watchdog->{$dog_name_or_number} > $fail_count;

# example problematic code

while ( 'true') {
       print "Cooking the core";
       $self->watchdog('melter number one');
       for (1 .. 10000) {
               $self->watchdog('melter number two');