Diff for /loncom/metadata_database/cleanup_database.pl between versions 1.7 and 1.8

version 1.7, 2006/11/20 17:07:57 version 1.8, 2016/08/15 18:01:14
Line 57  before it will be dropped. Line 57  before it will be dropped.
   
 =back  =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:  If you desire the immediate cleanup of temporary tables, use the following:
   
Line 74  Depending on permissions, you may have t Line 75  Depending on permissions, you may have t
 use strict;  use strict;
 use lib '/home/httpd/lib/perl/';  use lib '/home/httpd/lib/perl/';
 use LONCAPA::Configuration;  use LONCAPA::Configuration;
   use Apache::lonnet;
 use Getopt::Long;  use Getopt::Long;
 use Time::Local;  use Time::Local;
 use DBI;  use DBI;
   use Digest::MD5();
   
 my $help = 0;  my $help = 0;
 my $killtime = 86400*2;  my $killtime;
 GetOptions( "killtime=s" => \$killtime,   GetOptions( "killtime=s" => \$killtime, 
             "help"       => \$help );              "help"       => \$help );
 if ($help) {  if ($help) {
Line 88  cleanup_database.pl     Cleans up the LO Line 91  cleanup_database.pl     Cleans up the LO
                         temporary tables.                          temporary tables.
 Command line arguements  Command line arguements
    --killtime  <number>     The number of seconds a temporary table is allowed     --killtime  <number>     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.     --help                   Print out this help message.
   
 Examples:  Examples:
Line 121  my $sth = $dbh->prepare("SHOW TABLE STAT Line 128  my $sth = $dbh->prepare("SHOW TABLE STAT
 $sth->execute();  $sth->execute();
 my $results = $sth->fetchall_hashref('Name');  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)) {  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};      my $tabletime = $results->{$name}{Update_time};
     # Times are like: 2002-07-25 10:17:08      # Times are like: 2002-07-25 10:17:08
     my ($year,$month,$day,$hour,$min,$sec)=       my ($year,$month,$day,$hour,$min,$sec)= 
         ($tabletime =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/);          ($tabletime =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/);
     my $epoch_seconds = timelocal($sec,$min,$hour,$day,$month-1,$year-1900);      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);          $dbh->do('DROP TABLE '.$name);
     }      }
 }  }
Line 137  $sth->finish(); Line 183  $sth->finish();
 # --------------------------------------------------- Close database connection  # --------------------------------------------------- Close database connection
 $dbh->disconnect;  $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;
   }
   

Removed from v.1.7  
changed lines
  Added in v.1.8


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>