[LON-CAPA-cvs] cvs: loncom /metadata_database parse_activity_log.pl
matthew
lon-capa-cvs@mail.lon-capa.org
Mon, 19 Sep 2005 18:32:00 -0000
matthew Mon Sep 19 14:32:00 2005 EDT
Modified files:
/loncom/metadata_database parse_activity_log.pl
Log:
Now hold exclusive lock on activity.log.lock until script exits, and if unable
to lock throw a warning and exit.
Added &clean_up_and_exit to attempt to make sure we die gracefully.
Fixed bug with $newfilename not being set properly (scoping issue).
get_id now uses $dbh->{'mysql_insertid'} to grab the last updated auto-increment column value.
&load_backup_xml_tables now uses $dbh->quote to quote values properly
&xml_store_id_table also uses $dbh->quote as well.
Index: loncom/metadata_database/parse_activity_log.pl
diff -u loncom/metadata_database/parse_activity_log.pl:1.15 loncom/metadata_database/parse_activity_log.pl:1.16
--- loncom/metadata_database/parse_activity_log.pl:1.15 Mon Jul 11 01:16:19 2005
+++ loncom/metadata_database/parse_activity_log.pl Mon Sep 19 14:31:57 2005
@@ -2,7 +2,7 @@
#
# The LearningOnline Network
#
-# $Id: parse_activity_log.pl,v 1.15 2005/07/11 05:16:19 matthew Exp $
+# $Id: parse_activity_log.pl,v 1.16 2005/09/19 18:31:57 matthew Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -131,7 +131,7 @@
print STDERR "$0: logging to $logfile".$/;
if (! open(LOGFILE,">$logfile")) {
warn("Unable to open $logfile for writing. Run aborted.");
- exit 5;
+ &cleanup_and_exit(5);
} else {
$logthis = \&log_to_file;
}
@@ -175,37 +175,24 @@
#
# Wait for a lock on the lockfile to avoid collisions
my $lockfilename = $sourcefilename.'.lock';
+$newfilename = $sourcefilename.'.processing';
if (! defined($xmlfile)) {
-open(LOCKFILE,'>'.$lockfilename);
-if (!flock(LOCKFILE,LOCK_EX)) {
- warn("Unable to lock $lockfilename. Aborting".$/);
- exit 6;
-}
+ open(LOCKFILE,'>'.$lockfilename);
+ if (!flock(LOCKFILE,LOCK_EX|LOCK_NB)) {
+ warn("Unable to lock $lockfilename. Aborting".$/);
+ &clean_up_and_exit(6);
+ }
-##
-## 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, so I cannot work on it.');
- exit 2;
-}
-
-if (-e $sourcefilename) {
- $logthis->('renaming '.$sourcefilename.' to '.$newfilename);
- rename($sourcefilename,$newfilename);
- Copy($newfilename,$newfilename.'.'.time) if ($backup);
- $logthis->("renamed $sourcefilename to $newfilename");
-} else {
- my $command = 'touch '.$newfilename;
- $logthis->($command);
- system($command);
- $logthis->('touch was completed');
+ if (! -e $newfilename && -e $sourcefilename) {
+ $logthis->('renaming '.$sourcefilename.' to '.$newfilename);
+ rename($sourcefilename,$newfilename);
+ Copy($newfilename,$newfilename.'.'.time) if ($backup);
+ $logthis->("renamed $sourcefilename to $newfilename");
+ } elsif (! -e $newfilename) {
+ utime(undef,undef,$newfilename);
+ }
}
-close(LOCKFILE);
-}
##
## Table definitions
##
@@ -312,7 +299,7 @@
if (!&Apache::lonmysql::verify_sql_connection()) {
warn "Unable to connect to MySQL database.";
$logthis->("Unable to connect to MySQL database.");
- exit 3;
+ &clean_up_and_exit(3);
}
$logthis->('SQL connection is up');
@@ -359,7 +346,7 @@
}
if (defined($xmlfile)) {
- exit(0);
+ &clean_up_and_exit(0);
}
##
@@ -369,7 +356,7 @@
if (! &create_tables()) {
warn "Unable to create tables";
$logthis->('Unable to create tables');
- exit 4;
+ &clean_up_and_exit(4);
}
##
@@ -390,7 +377,7 @@
if (! defined($result)) {
# Something went wrong along the way...
$logthis->('process_courselog returned undef');
- exit 5;
+ &clean_up_and_exit(5);
} elsif ($result > 0) {
$time_this->();
$logthis->('process_courselog returned '.$result.'.'.$/.
@@ -418,17 +405,25 @@
$logthis->(&outputtimes());
}
-if ($log) {
- close LOGFILE;
-}
+&clean_up_and_exit(0);
-foreach my $file ($lockfilename, $error_filename,$logfile) {
- if (defined($file) && -z $file) {
- unlink($file);
+########################################################
+########################################################
+
+sub clean_up_and_exit {
+ my ($exit_code) = @_;
+ # Close files
+ close(LOCKFILE);
+ close(LOGFILE);
+ # Remove zero length files
+ foreach my $file ($lockfilename, $error_filename,$logfile) {
+ if (defined($file) && -z $file) {
+ unlink($file);
+ }
}
-}
-exit 0; # Everything is okay, so end here before it gets worse.
+ exit $exit_code;
+}
########################################################
########################################################
@@ -718,21 +713,20 @@
sub get_id {
my ($table,$fieldname,$value) = @_;
- if (exists($IDs{$table}->{$value})) {
+ if (exists($IDs{$table}->{$value}) && $IDs{$table}->{$value} =~ /^\d+$/) {
return $IDs{$table}->{$value};
} else {
# insert into the table - if the item already exists, that is
# okay.
my $result = &Apache::lonmysql::store_row($table,[undef,$value]);
if (! defined($result)) {
- warn("Got error on id insert for $value\n".&Apache::lonmysql::get_error());
+ warn("Got error on id insert for $value\n".
+ &Apache::lonmysql::get_error());
}
# get the id
- my @Data =
- &Apache::lonmysql::get_rows($table,qq{$fieldname='$value'});
- if (@Data) {
- $IDs{$table}->{$value}=$Data[0]->[0];
- return $IDs{$table}->{$value};
+ my $id = &Apache::lonmysql::get_dbh()->{'mysql_insertid'};
+ if (defined($id)) {
+ $IDs{$table}->{$value}=$id;
} else {
$logthis->("Unable to retrieve id for $table $fieldname $value");
return undef;
@@ -812,6 +806,7 @@
my %ids = ();
sub load_backup_xml_tables {
my ($filename,$tables) = @_;
+ my $dbh = &Apache::lonmysql::get_dbh();
my $xmlfh;
open($xmlfh,"cat $filename | gzip -d - |");
if (! defined($xmlfh)) {
@@ -839,7 +834,7 @@
my $resource_id = &xml_get_id('resource',$resource);
my $student_id = &xml_get_id('student',$student);
my $machine_id = &xml_get_id('machine',$machine);
- &xml_store_activity_row(map { defined($_)?qq{'$_'}:''
+ &xml_store_activity_row(map { defined($_)?$dbh->quote($_):''
} ($resource_id,
$time,
$student_id,
@@ -891,9 +886,10 @@
sub xml_store_id_table {
my ($table,$tabledata) =@_;
+ my $dbh = &Apache::lonmysql::get_dbh();
if (! &Apache::lonmysql::bulk_store_rows
($tables{$table},2,
- [map{[$tabledata->{$_},qq{"$_"}]} keys(%$tabledata)])) {
+ [map{[$tabledata->{$_},$dbh->quote($_)]} keys(%$tabledata)])) {
$logthis->("Error:".&Apache::lonmysql::get_error());
warn "Error:".&Apache::lonmysql::get_error().$/;
}