--- loncom/metadata_database/parse_activity_log.pl 2004/09/15 21:11:06 1.5 +++ loncom/metadata_database/parse_activity_log.pl 2004/12/16 19:29:20 1.7 @@ -2,7 +2,7 @@ # # The LearningOnline Network # -# $Id: parse_activity_log.pl,v 1.5 2004/09/15 21:11:06 matthew Exp $ +# $Id: parse_activity_log.pl,v 1.7 2004/12/16 19:29:20 matthew Exp $ # # Copyright Michigan State University Board of Trustees # @@ -45,6 +45,8 @@ use Time::HiRes(); use Getopt::Long(); use IO::File; use File::Copy; +use Fcntl qw(:flock); + # # Determine parameters my ($help,$course,$domain,$drop,$file,$time_run,$nocleanup,$log,$backup); @@ -117,26 +119,34 @@ if ($log) { ## my $sourcefilename; # activity log data my $newfilename; # $sourcefilename will be renamed to this -my $sql_filename; # the mysql backup data file name. +my $gz_sql_filename; # the gzipped mysql backup data file name. my $error_filename; # Errors in parsing the activity log will be written here if ($file) { $sourcefilename = $file; } else { $sourcefilename = &get_filename($course,$domain); } -$sql_filename = $sourcefilename; +my $sql_filename = $sourcefilename; $sql_filename =~ s|[^/]*$|activity.log.sql|; +$gz_sql_filename = $sql_filename.'.gz'; $error_filename = $sourcefilename; $error_filename =~ s|[^/]*$|activity.log.errors|; $logthis->('Beginning logging '.time); + +# +# Wait for a lock on the lockfile to avoid collisions +my $lockfilename = $sourcefilename.'.lock'; +open(LOCKFILE,'>'.$lockfilename); +flock(LOCKFILE,LOCK_EX) || die("Unable to lock $lockfilename. Aborting".$/); + ## ## There will only be a $newfilename file if a copy of this program is already ## running. my $newfilename = $sourcefilename.'.processing'; if (-e $newfilename) { warn "$newfilename exists"; - $logthis->($newfilename.' exists'); + $logthis->($newfilename.' exists, so I cannot work on it.'); exit 2; } @@ -152,6 +162,8 @@ if (-e $sourcefilename) { $logthis->('touch was completed'); } +close(LOCKFILE); + ## ## Table definitions ## @@ -274,7 +286,7 @@ $logthis->('SQL connection is up'); if ($drop) { &drop_tables(); $logthis->('dropped tables'); } -if (-s $sql_filename) { +if (-s $gz_sql_filename) { # if ANY one of the tables does not exist, load the tables from the # backup. my @Current_Tables = &Apache::lonmysql::tables_in_db(); @@ -289,7 +301,7 @@ if (-s $sql_filename) { foreach my $table (@Activity_Table,@ID_Tables) { if (! $Found{$table->{'id'}}) { $time_this->(); - &load_backup_tables($sql_filename); + &load_backup_tables($gz_sql_filename); $time_this->('load backup tables'); last; } @@ -327,8 +339,8 @@ if (-s $newfilename) { exit 5; } elsif ($result > 0) { $time_this->(); - $logthis->('process_courselog returned '.$result.' backup up tables'); - &backup_tables($sql_filename); + $logthis->('process_courselog returned '.$result.' backing up tables'); + &backup_tables($gz_sql_filename); $time_this->('write backup tables'); } } @@ -579,14 +591,14 @@ sub outputtimes { ## Use mysqldump to store backups of the tables ## sub backup_tables { - my ($sql_filename) = @_; + my ($gz_sql_filename) = @_; my $command = qq{mysqldump --opt loncapa }; foreach my $table (@ID_Tables,@Activity_Table) { my $tablename = $table->{'id'}; $command .= $tablename.' '; } - $command .= '>'.$sql_filename; + $command .= '| gzip >'.$gz_sql_filename; $logthis->($command); system($command); } @@ -595,13 +607,15 @@ sub backup_tables { ## Load in mysqldumped files ## sub load_backup_tables { - my ($sql_filename) = @_; - return undef if (! -e $sql_filename); - # Check for .my.cnf - my $command = 'mysql -e "SOURCE '.$sql_filename.'" loncapa'; - $logthis->('loading previously saved sql table'.$/.$command); - system($command); - $logthis->('finished loading old data'); + my ($gz_sql_filename) = @_; + if (-s $gz_sql_filename) { + &logthis('loading data from gzipped sql file'); + my $command='gzip -dc activity.log.sql.gz | mysql --database=loncapa'; + system($command); + $logthis->('finished loading gzipped data');; + } else { + return undef; + } } ##