#!PERL_EXE # @(#)cancel 1.13 09/09/04 use strict; use File::Path; use FindBin; use lib "$FindBin::Bin/../lib"; use ShakeConfig; use lib "$shake_perl_lib"; use Shake::Opt; use Shake::Die; use Shake::Version; ######################################################################### # Global variables ######################################################################### my $arglist = "@ARGV"; # save the arguments for entry # into the database my @cprogs; # Programs to run with -cancel my $bin = "$shake_home/bin"; my $GENEX = "genex"; my $TRANSFER = "transfer"; ######################################################################### # End global variables ######################################################################### ######################################################################### # Stuff to handle command line options and program documentation: ######################################################################### my $desc = 'Remove any references to an event from the processing system and archives'; my $flgs = [{ FLAG => 'event', ARG => 'event_id', TYPE => 's', REQ => 'y', DESC => 'Specifies the id of the event to process'}, { FLAG => 'verbose', DESC => 'Print informational messages to stderr.'}, { FLAG => 'help', DESC => 'Print program documentation and quit.'} ]; my $options = Opt::setOptions($flgs) or Die "Error in setOptions"; if (defined $options->{'help'}) { Opt::printDoc($desc); exit 0; } defined $options->{'event'} or Die "Must specify an event with -event flag"; my $evid = $options->{'event'}; my $verbose = defined $options->{'verbose'} ? 1 : 0; Die "Unknown argument(s): @ARGV" if (@ARGV); ######################################################################### # End of command line option stuff ######################################################################### exit main(); 0; sub main { my ($count, $prog, $flags, $command, $current_evid, $ev_dir, @subdirs); #---------------------------------------------------------------------- # Load the configuration -- we want the 'cancel' items from shake.conf #---------------------------------------------------------------------- unshift @$config_dirs, "$shake_home/data/$evid/config"; my $cfg = new LoadConfig('shake.conf', $config_dirs, \&Print) or Die "ERROR: can't create config object"; ($cfg->parse( { 'cancel' => \&cancelsub, 'DEFAULT' => sub { undef; } },) == 0) or Die "ERROR: can't parse shake.conf"; #---------------------------------------------------------------------- # $loc_db_conf and $loc_db_dirs are defined in the ShakeConfig module #---------------------------------------------------------------------- my $sv = new Version($evid, $loc_db_conf, $loc_db_dirs) or Die "Could not create Version object"; #----------------------------------------------------------------------- # Make the connection to the local ShakeMap databases; $loc_db_conf and # $loc_db_dirs are defined in ShakeConfig.pm #----------------------------------------------------------------------- my $dbc = new DbConnect($loc_db_conf, $loc_db_dirs) or Die "Error: couldn't config database with $loc_db_conf"; my $dbh = $dbc->connect or Die "Error: couldn't establish database connection"; #----------------------------------------------------------------------- # check for any entries in the shake_runs database # - quit now if there are no programs listed in the database #----------------------------------------------------------------------- my $sth = $dbh->prepare( qq{ select count(*) from shake_runs where evid=? }) or Die "Error in prepare (count)"; $sth->execute($evid) or Die "Error in execute (count)"; ($count) = $sth->fetchrow_array; exit(0) if ($count == 0); #----------------------------------------------------------------------- # Create a new (cancelled) version of the event -- ShakeCast requires # a new version and scfeed may be called in the next bit of code... #----------------------------------------------------------------------- $sv->setVersion("Event cancelled.") or Die "Error: couldn't set version"; #----------------------------------------------------------------------- # Cancel the programs listed in shake.conf if they were run #----------------------------------------------------------------------- for $prog ( @cprogs, $TRANSFER ) { $flags = $sv->getFlags($prog); if(not defined $flags) { Print "Program $prog was not run for this event" if $verbose; next; } $flags .= " -forget" if ($prog =~ /transfer/); $command = "$bin/$prog -event $evid -cancel $flags"; Print "running '$command'" if $verbose; system($command); } undef $sv; #----------------------------------------------------------------------- # remove event from web database #----------------------------------------------------------------------- $dbh->do("delete from earthquake where evid='$evid'") or Print "Error deleting old event id"; #----------------------------------------------------------------------- # determine most recent event, where we will store new home and # database pages #----------------------------------------------------------------------- $sth = $dbh->prepare( qq{ select evid from earthquake where mainshock != 'scenario' and mainshock != 'invisible' order by tabsol desc }) or Die "Error preparing earthquake select"; $sth->execute() or Die "Error executing earthquake select"; $sth->bind_columns(undef,\$current_evid); #----------------------------------------------------------------------- # get the first row, which is most recent event #----------------------------------------------------------------------- $sth->fetch; $sth->finish; if($current_evid) { $sv = new Version($current_evid, $loc_db_conf, $loc_db_dirs) or Die "Could not create Version object for previous event"; #----------------------------------------------------------------------- # rebuild shake home and database pages #----------------------------------------------------------------------- $flags = $sv->getFlags($GENEX); if(not defined $flags) { Print "Program genex was not run for previous event" if $verbose; $flags = ''; } $command = "$bin/$GENEX -event $current_evid $flags"; Print "running '$command'" if $verbose; system($command); #----------------------------------------------------------------------- # push new pages to web, don't remember that we did this #----------------------------------------------------------------------- $command = "$bin/$TRANSFER -event $current_evid -www -forget"; Print "running '$command'" if $verbose; system($command); } #----------------------------------------------------------------------- # Disconnect from the local ShakeMap databases #----------------------------------------------------------------------- $dbc->disconnect; #----------------------------------------------------------------------- # remove all processing results, but keep input #----------------------------------------------------------------------- $ev_dir = "$shake_home/data/$evid"; @subdirs = grep { $_ !~ /input$/ } <$ev_dir/*>; rmtree(\@subdirs,0,0); return 0; } sub cancelsub { my $cancel = shift; defined $cancel or return "cancel: undefined program\n"; push @cprogs, $cancel; return undef; }