CronJobs.pm


NAME

Communiware::CronJobs - Common functions for communiware daemons executing by cron.


SYNOPSIS

   daemon_init($name)
   check_self()
   err_print(message)


DESCRIPTION

Use this module in each communiware daemon which is used to be executing by cron. At very begining of daemon execution daemon_init should be called.


VARIABLES

$daemon_name

Stores the deamon name used to create lock-file's name and in log messages. Gets it's value in daemon_init subroutine.

$lock_file

Full name of lock-file of this process. Gets it's value in daemon_init subroutine.


FUNCTIONS

Here is detailed description of exported functions.

daemon_init ( [ $daemon_name ] )

Checks does the same daemon already running. If it does, dies with error message. If there is no the same daemon running, creates lock-file, which contains it's PID and returns. This lock-file will be removed on successful daemon termonation. Killed daemon will not remove lock-file, but being started again it will determine that there is no running process which PID is in lock-file.

Optional parameter $daemon_name is the name to use in log messages and to create a name of lock-file. If not specified, daemon name will be created by truncating all leading directory names from executing file name ($0 global variable).

You could have to specify daemon_name explicitly if you want to allow your daemon to use different names running with different modes.

check_self( $daemon_name, $lock_file )

errprint ( LIST )

Prints to STDERR an error message (which is passed as LIST) starting it with text:

        [ <time> ] <daemon_name> ERROR:

where

 <daemon_name> is a name of daemon, stored in $Communiware::CronJobs::daemon_name,
 <time> is a time of printing

starterror ( $daemon_name, LIST )

Simply sets daemon name and call errprint. Useful to print error messages before daemon_init have been called.

It is safe to call it after daemon_init - it will skip the first parameter and use already specified daemon name.

starterror adds word 'on startup' after 'ERROR' in message so such errors can be easily distinguished from other.

logprint ( LIST )

Prints to STDOUT a message (passed as LIST) starting it with text:

        [ <time> ] <daemon_name>:

where <daemon_name> and <time> are the same as in errprint.

debugprint ( LIST )

Same as logprint, except printing to STDERR, not STDOUT.

for future reference

sub chk_stop {
my $do_stop = shift;
my $sleep_time = shift;
        if ($do_stop) {
                die "$do_stop";
        }
        if ($sleep_time) {
                my ($avg) = Sys::CpuLoad::load();
                my ($a)   = shift;
                return $avg if (defined($a) && ($a == -1));
                if ($avg > $opt_s) {
                        do {
                                print "Load average = ", $avg, ". Go sleeping for $std_sleep_time seconds...\n";
                                sleep(
                                        $std_sleep_time);
                        } while (($avg = &chk_stop(-1)) > $opt_s);
                }
        }
        return 0;
}
16 октябрь 2007 13:44