Cleaned up the structure of the script.

It now has a initialisation, main loop and finalisation.
Now sending messages to the invoker instead of the maintainer.
This commit is contained in:
Geert-Jan Giezeman 1998-07-08 08:12:33 +00:00
parent 5f1e7c747a
commit 17536b7a45
1 changed files with 114 additions and 64 deletions

View File

@ -3,23 +3,20 @@
use strict;
#--------------------------------------------------------------#
# This script scans through the mail box $::MAILBOX, using procmail.
# If a message contains a line with the format
#
# package: <URL>
#
# this package will be automatically downloaded, and the sender of
# the message will be notified.
# This script scans through the mail box $::MAIL_IN, using formail.
# It handles requests automatically.
# This script deals with reading the mail box and dispatching the separate
# mails to the program that treats one mail.
# It takes care that there is only one program doing so, by means of locking.
#--------------------------------------------------------------#
# the mail boxes
$::LOCKFILE='/projects/CGAL/submissions/autohandle/data/collect_submission.lock';
$::MAIL_IN='/users/geert/PUBLIC/CGAL-submit/incoming-mail';
#$::MAIL_OUT='/users/geert/tmp/handled-mail';
#$::MAIL_OUT='/users/geert/PUBLIC/CGAL-submit/handled-mail';
$::MAIL_TMP='/users/geert/PUBLIC/CGAL-submit/temp-mail';
$::SCRIPT='/projects/CGAL/submissions/autohandle/scripts/collect_submissions';
$::MAINTAINER='cgal-submit@cs.uu.nl';
# program locations
$::FORMAIL='/projects/CGAL/submissions/autohandle/scripts/formail';
@ -36,47 +33,67 @@ $::max_tries_between_warning= int 21600/$::seconds_between_invocations;
$ENV{PATH}='/sbin:/usr/sbin:/usr/bsd:/bin:/usr/bin';
# variable that is set when there is an attempt to kill this process.
$::please_die=0;
$::invoker = $ENV{USER};
if ($::invoker =~ /^\s*([-\@\w]+)\s*$/) {
$::invoker = "$1\@cs.uu.nl";
} else {
print STDERR <<"TOTHIER";
The script $::SCRIPT did not start because
the USER environment value has a strange value.
TOTHIER
exit 1;
}
$::hostname = $ENV{HOST};
if ($::hostname =~ /^\s*([-\@\w]+)\s*$/) {
$::hostname = $1;
} else {
open NOTICE, "|Mail -s \"Collect_submissions not started!\" $::MAINTAINER";
print NOTICE <<"TOTHIER";
print STDERR <<"TOTHIER";
The script $::SCRIPT did not start because
the HOST environment value has a strange value.
TOTHIER
close NOTICE;
exit 1;
}
sub termination_signal_handler {
$::please_die=1;
}
$::tries = 1;
$::first_time = 1;
$SIG{INT} = \&termination_signal_handler;
$SIG{TERM} = \&termination_signal_handler;
while (! $::please_die) {
sub initialisation()
{
#
# Disable signals that kill this process.
#
$SIG{INT} = \&termination_signal_handler;
$SIG{TERM} = \&termination_signal_handler;
#
# Try to acquire the lock.
# Quit with a message if it fails.
#
if ( system("$::LOCKCMD", "-r", '10', "$::LOCKFILE") != 0) {
--$::tries;
if ($::tries < 0) {
open NOTICE, "|Mail -s \"Collect_submissions locked!\" $::MAINTAINER";
open NOTICE, "|Mail -s \"collect_submissions notice\" $::invoker";
print NOTICE <<"TOTHIER";
The script $::SCRIPT could not proceed because
it could not acquire the needed lock on file $::LOCKFILE.
TOTHIER
close NOTICE;
$::tries = $::max_tries_between_warning;
exit 1;
}
} else {
$::tries = $::max_tries_between_warning;
if ($::first_time) {
#
# Now that you have the lock, check to see if there is another process
# executing this program. If so, quit.
#
my $logline = `tail -1 $::LOGFILE`;
if ($logline =~ /started/) {
if ($logline !~ /stopped/) {
unlink $::LOCKFILE;
open NOTICE, "|Mail -s \"Collect_submissions busy!\" $::MAINTAINER";
open NOTICE, "|Mail -s \"collect_submissions notice\" $::invoker";
print NOTICE <<"TOTHIER";
The script $::SCRIPT did not proceed because
there seems to be another process collecting the mail messages.
@ -87,16 +104,41 @@ TOTHIER
close NOTICE;
exit 1;
}
#
# We can proceed. Record a message to the log file that this process
# takes control of dealing with the mail box.
#
open LOGFILE, ">>$::LOGFILE";
print LOGFILE "$::hostname $$: started executing on ", `date`;
close LOGFILE;
$::first_time = 0;
}
unlink $::LOCKFILE;
}
sub main_loop()
{
my $tries_to_go = $::max_tries_between_warning;
while (! $::please_die) {
if ( system("$::LOCKCMD", "-r", '10', "$::LOCKFILE") != 0) {
--$tries_to_go;
if ($tries_to_go < 0) {
open NOTICE,
"|Mail -s \"collect_submissions notice\" $::invoker";
print NOTICE <<"TOTHIER";
The script $::SCRIPT could not proceed because
it could not acquire the needed lock on file $::LOCKFILE.
TOTHIER
close NOTICE;
$tries_to_go = $::max_tries_between_warning;
}
} else {
$tries_to_go = $::max_tries_between_warning;
if ( -f $::MAIL_TMP ) {
open NOTICE, "|Mail -s \"Mail box $::MAIL_TMP was not emptied!\" $::MAINTAINER";
open NOTICE,
"|Mail -s \"collect_submissions notice\" $::invoker";
print NOTICE <<"TOTHIER";
The script $::SCRIPT has been stopped!
The script $::SCRIPT has been stopped,
because the temporary mail box $::MAIL_TMP was not emptied.
TOTHIER
close NOTICE;
unlink $::LOCKFILE;
@ -113,10 +155,18 @@ TOTHIER
unlink $::LOCKFILE;
}
sleep $::seconds_between_invocations unless $::please_die;
}
}
open LOGFILE, ">>$::LOGFILE";
print LOGFILE "$::hostname $$: stopped executing at ", `date`;
close LOGFILE;
exit 1;
sub finalisation()
{
open LOGFILE, ">>$::LOGFILE";
print LOGFILE "$::hostname $$: stopped executing at ", `date`;
close LOGFILE;
}
initialisation();
main_loop();
finalisation();
exit 0;