--- loncom/metadata_database/cleanup_database.pl 2006/11/20 17:07:57 1.7 +++ loncom/metadata_database/cleanup_database.pl 2016/08/15 18:01:14 1.8 @@ -2,7 +2,7 @@ # The LearningOnline Network # cleanup_database.pl Remove stale temporary search results. # -# $Id: cleanup_database.pl,v 1.7 2006/11/20 17:07:57 albertel Exp $ +# $Id: cleanup_database.pl,v 1.8 2016/08/15 18:01:14 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -57,9 +57,10 @@ before it will be dropped. =back -The following invocation is equivalent to the default: +The following invocation will drop all tables without updates in the past +two days (the default). - cleanup_database.pl --killtime 86400 + cleanup_database.pl --killtime 172800 If you desire the immediate cleanup of temporary tables, use the following: @@ -74,12 +75,14 @@ Depending on permissions, you may have t use strict; use lib '/home/httpd/lib/perl/'; use LONCAPA::Configuration; +use Apache::lonnet; use Getopt::Long; use Time::Local; use DBI; +use Digest::MD5(); my $help = 0; -my $killtime = 86400*2; +my $killtime; GetOptions( "killtime=s" => \$killtime, "help" => \$help ); if ($help) { @@ -88,7 +91,11 @@ cleanup_database.pl Cleans up the LO temporary tables. Command line arguements --killtime The number of seconds a temporary table is allowed - to live. Defaults to 86400 (1 day) + to live. On a library server, specifying this argument + also overrides any course-specific or domain-specific + lifetimes which apply to the various md5_hash_* tables + which contain student data for a course, for which the + current server is the homeserver. --help Print out this help message. Examples: @@ -121,14 +128,53 @@ my $sth = $dbh->prepare("SHOW TABLE STAT $sth->execute(); my $results = $sth->fetchall_hashref('Name'); +my ($nokilltime,$gotconf,%coursetypes,%md5hashes,%domcrsdefs,%gotcourseenv,%crssetting); +if ($killtime eq '') { + $killtime = 86400*2; + if ($perlvar{'lonRole'} eq 'library') { + $nokilltime = 1; + } +} + foreach my $name (keys(%$results)) { - next if ($results->{$name}{Comment} ne 'temporary'); + next if ($results->{$name}{Comment} ne 'temporary'); my $tabletime = $results->{$name}{Update_time}; # Times are like: 2002-07-25 10:17:08 my ($year,$month,$day,$hour,$min,$sec)= ($tabletime =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/); my $epoch_seconds = timelocal($sec,$min,$hour,$day,$month-1,$year-1900); - if ((time - $epoch_seconds) > $killtime) { + my $currkilltime = $killtime; + if ($nokilltime) { + if ($name =~ /^md5_(\w+)_(?:\w+)$/) { + my $hashid = $1; + unless ($gotconf) { + &get_config(\%coursetypes,\%md5hashes,\%domcrsdefs); + $gotconf = 1; + } + if (exists($md5hashes{$hashid})) { + my ($cdom,$cnum) = split(/_/,$md5hashes{$hashid}); + unless ($gotcourseenv{$md5hashes{$hashid}}) { + my %envhash = &Apache::lonnet::dump('environment',$cdom,$cnum); + $gotcourseenv{$md5hashes{$hashid}} = 1; + if ($coursetypes{$md5hashes{$hashid}} eq 'unofficial') { + if ($envhash{'internal.textbook'}) { + $coursetypes{$md5hashes{$hashid}} = 'textbook'; + } + } + $crssetting{$md5hashes{$hashid}} = $envhash{'internal.mysqltables'}; + } + if (($crssetting{$md5hashes{$hashid}} ne '') && ($crssetting{$md5hashes{$hashid}} !~ /^\D/)) { + $currkilltime = $crssetting{$md5hashes{$hashid}}; + } elsif (ref($domcrsdefs{$cdom}) eq 'HASH') { + if (($domcrsdefs{$cdom}{$coursetypes{$md5hashes{$hashid}}} ne '') && + ($domcrsdefs{$cdom}{$coursetypes{$md5hashes{$hashid}}} !~ /^\D/)) { + $currkilltime = $domcrsdefs{$cdom}{$coursetypes{$md5hashes{$hashid}}}; + } + } + } + } + } + if ((time - $epoch_seconds) > $currkilltime) { $dbh->do('DROP TABLE '.$name); } } @@ -137,3 +183,39 @@ $sth->finish(); # --------------------------------------------------- Close database connection $dbh->disconnect; +sub get_config { + my ($coursetypes,$md5hashref,$domsettings) = @_; + my @domains = sort(&Apache::lonnet::current_machine_domains()); + my @ids=&Apache::lonnet::current_machine_ids(); + foreach my $dom (@domains) { + my %domconfig = &Apache::lonnet::get_dom('configuration',['coursedefaults'],$dom); + if (ref($domconfig{'coursedefaults'}) eq 'HASH') { + if (ref($domconfig{'coursedefaults'}{'mysqltables'}) eq 'HASH') { + $domsettings->{$dom} = $domconfig{'coursedefaults'}{'mysqltables'}; + } + } + my %currhash = &Apache::lonnet::courseiddump($dom,'.',1,'.','.','.',1,\@ids,'.'); + if (keys(%currhash)) { + foreach my $key (keys(%currhash)) { + if ($key ne '') { + if (ref($currhash{$key}) eq 'HASH') { + my $digest = &Digest::MD5::md5_hex($key); + $md5hashref->{$digest} = $key; + my $crstype = $currhash{$key}{'type'}; + my $longcrstype = 'unofficial'; + if ($crstype eq 'Community') { + $longcrstype = 'community'; + } elsif ($crstype eq 'Placement') { + $longcrstype = 'placement'; + } elsif ($currhash{$key}{'inst_code'}) { + $longcrstype = 'official'; + } + $coursetypes->{$key} = $longcrstype; + } + } + } + } + } + return; +} +