cgal/Packages/Maintenance/package_handling/collect_submissions

206 lines
5.1 KiB
Perl
Executable File

#! /sw/bin/perl -T
use strict;
#--------------------------------------------------------------#
# 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
my $Scripts_directory='/projects/CGAL/admin_scripts';
my $Data_directory='/projects/CGAL/submissions/autohandle/data';
my $LOCKFILE="$Data_directory/collect_submission.lock";
my $Mail_directory='/users/geert/PUBLIC/CGAL-submit';
my $MAIL_IN="$Mail_directory/incoming-mail";
#my $MAIL_OUT="$Mail_directory/handled-mail";
my $MAIL_TMP="$Mail_directory/temp-mail";
my $SCRIPT="$Scripts_directory/collect_submissions";
my $MAIL_PROG="$Scripts_directory/send_cgal_mail";
my $treat_one_mail_program="$Scripts_directory/treat_submit_mail";
# program locations
my $FORMAIL="/sw/bin/formail";
my $LOCKCMD="/sw/bin/lockfile";
# the log file
my $LOGFILE="$Data_directory/collect_submissions.log";
#
my $seconds_between_invocations=300;
my $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.
my $please_die=0;
my $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;
}
my $hostname = $ENV{HOST};
if ($hostname =~ /^\s*([-\@\w.]+)\s*$/) {
$hostname = $1;
} else {
print STDERR <<"TOTHIER";
The script $SCRIPT did not start because
the HOST environment value has a strange value ($hostname).
TOTHIER
exit 1;
}
sub termination_signal_handler {
$please_die=1;
}
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) {
open NOTICE, "|$MAIL_PROG \"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;
exit 1;
}
#
# 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 !~ /stopped/) {
unlink $LOCKFILE;
open NOTICE, "|$MAIL_PROG \"collect_submissions notice\" $invoker";
print NOTICE <<"TOTHIER";
The script $SCRIPT did not proceed because
there seems to be another process collecting the mail messages.
The file $LOGFILE says:
$logline
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;
unlink $LOCKFILE;
}
sub new_untreated_mail_file()
{
my $filename;
my $n=1;
for ($n=1; $n<1000; ++$n) {
$filename="$Mail_directory/untreated_mail_$n";
last if (! -f $filename);
}
return $filename;
}
sub treat_current_mail()
{
if ( -f $MAIL_IN ) {
if ( system("$LOCKCMD", "-r", '10', "$MAIL_IN.lock") == 0) {
rename($MAIL_IN, $MAIL_TMP);
unlink "$MAIL_IN.lock";
if (system("$FORMAIL -ns $treat_one_mail_program < $MAIL_TMP")== 0 )
{
unlink $MAIL_TMP;
} else {
my $Save_file = new_untreated_mail_file();
rename($MAIL_TMP, $Save_file) or die;
open NOTICE,
"|$MAIL_PROG \"collect_submissions notice\" $invoker";
print NOTICE <<"TOTHIER";
The script $SCRIPT could not treat incoming mail.
This mail was saved in $Save_file.
TOTHIER
}
}
}
}
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_PROG \"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_PROG \"collect_submissions notice\" $invoker";
print NOTICE <<"TOTHIER";
The script $SCRIPT has been stopped,
because the temporary mail box $MAIL_TMP was not emptied.
TOTHIER
close NOTICE;
unlink $LOCKFILE;
$please_die = 1;
} else {
treat_current_mail;
}
unlink $LOCKFILE;
}
sleep $seconds_between_invocations unless $please_die;
}
}
sub finalisation()
{
open LOGFILE, ">>$LOGFILE";
print LOGFILE "$hostname $$: stopped executing at ", `date`;
close LOGFILE;
}
initialisation();
main_loop();
finalisation();
exit 0;