From 072896d66be675f6ec32b46267fb57b649b4eb03 Mon Sep 17 00:00:00 2001 From: Galen Charlton Date: Thu, 28 Oct 2010 09:05:21 -0400 Subject: [PATCH] bug 5219: tidy cleanup_database.pl Includes a bit of cleanup of the enhancement patch for bug 5074 - adding comments about old and new behavior isn't necessary for such small changes. Signed-off-by: Galen Charlton Signed-off-by: Chris Cormack --- misc/cronjobs/cleanup_database.pl | 93 ++++++++++++++++--------------- 1 file changed, 47 insertions(+), 46 deletions(-) diff --git a/misc/cronjobs/cleanup_database.pl b/misc/cronjobs/cleanup_database.pl index 7d5bcd1341..1333111789 100755 --- a/misc/cronjobs/cleanup_database.pl +++ b/misc/cronjobs/cleanup_database.pl @@ -30,6 +30,7 @@ BEGIN { use C4::Context; use C4::Dates; + #use C4::Debug; #use C4::Letters; #use File::Spec; @@ -43,7 +44,7 @@ Usage: $0 [-h|--help] [--sessions] [--sessdays DAYS] [-v|--verbose] [--zebraqueu other options --sessions purge the sessions table. If you use this while users are logged into Koha, they will have to reconnect. - --sessdays DAYS purge only sessions older than DAYS days (use together with sessions parameter). + --sessdays DAYS purge only sessions older than DAYS days (use together with sessions parameter). -v --verbose will cause the script to give you a bit more information about the run. --zebraqueue DAYS purge completed entries from the zebraqueue from @@ -53,14 +54,14 @@ USAGE exit $_[0]; } -my ($help, $sessions, $sess_days, $verbose, $zebraqueue_days, $mail); +my ( $help, $sessions, $sess_days, $verbose, $zebraqueue_days, $mail ); GetOptions( - 'h|help' => \$help, - 'sessions' => \$sessions, - 'sessdays:i' => \$sess_days, - 'v|verbose' => \$verbose, - 'm|mail' => \$mail, + 'h|help' => \$help, + 'sessions' => \$sessions, + 'sessdays:i' => \$sess_days, + 'v|verbose' => \$verbose, + 'm|mail' => \$mail, 'zebraqueue:i' => \$zebraqueue_days, ) || usage(1); @@ -68,7 +69,7 @@ if ($help) { usage(0); } -if (!($sessions || $zebraqueue_days || $mail)){ +if ( !( $sessions || $zebraqueue_days || $mail ) ) { print "You did not specify any cleanup work for the script to do.\n\n"; usage(1); } @@ -79,8 +80,8 @@ my $sth; my $sth2; my $count; -if ($sessions && !$sess_days) { #old behavior - if ($verbose){ +if ( $sessions && !$sess_days ) { + if ($verbose) { print "Session purge triggered.\n"; $sth = $dbh->prepare("SELECT COUNT(*) FROM sessions"); $sth->execute() or die $dbh->errstr; @@ -88,35 +89,36 @@ if ($sessions && !$sess_days) { #old behavior print "$count_arr[0] entries will be deleted.\n"; } $sth = $dbh->prepare("TRUNCATE sessions"); - $sth->execute() or die $dbh->errstr;; - if ($verbose){ + $sth->execute() or die $dbh->errstr; + if ($verbose) { print "Done with session purge.\n"; } -} -elsif($sessions && $sess_days>0) { #new behavior with number of days old - if ($verbose){ +} elsif ( $sessions && $sess_days > 0 ) { + if ($verbose) { print "Session purge triggered with days>$sess_days.\n"; } RemoveOldSessions(); - if ($verbose){ + if ($verbose) { print "Done with session purge with days>$sess_days.\n"; } } -if ($zebraqueue_days){ +if ($zebraqueue_days) { $count = 0; - if ($verbose){ + if ($verbose) { print "Zebraqueue purge triggered for $zebraqueue_days days.\n"; } - $sth = $dbh->prepare("SELECT id,biblio_auth_number,server,time FROM zebraqueue - WHERE done=1 and time < date_sub(curdate(), interval ? day)"); + $sth = $dbh->prepare( + "SELECT id,biblio_auth_number,server,time FROM zebraqueue + WHERE done=1 and time < date_sub(curdate(), interval ? day)" + ); $sth->execute($zebraqueue_days) or die $dbh->errstr; $sth2 = $dbh->prepare("DELETE FROM zebraqueue WHERE id=?"); - while (my $record = $sth->fetchrow_hashref){ - $sth2->execute($record->{id}) or die $dbh->errstr; + while ( my $record = $sth->fetchrow_hashref ) { + $sth2->execute( $record->{id} ) or die $dbh->errstr; $count++; } - if ($verbose){ + if ($verbose) { print "$count records were deleted.\nDone with zebraqueue purge.\n"; } } @@ -135,29 +137,28 @@ if ($mail) { exit(0); sub RemoveOldSessions { - my ($id, $a_session, $limit, $lasttime); - $limit= time() - 24*3600*$sess_days; - - $sth= $dbh->prepare("SELECT id, a_session FROM sessions"); - $sth->execute or die $dbh->errstr; - $sth->bind_columns(\$id, \$a_session); - $sth2 = $dbh->prepare("DELETE FROM sessions WHERE id=?"); - $count=0; - - while ($sth->fetch) { - $lasttime=0; - if($a_session =~ /lasttime:\s+(\d+)/) { - $lasttime= $1; - } - elsif($a_session =~ /(ATIME|CTIME):\s+(\d+)/ ) { - $lasttime= $2; + my ( $id, $a_session, $limit, $lasttime ); + $limit = time() - 24 * 3600 * $sess_days; + + $sth = $dbh->prepare("SELECT id, a_session FROM sessions"); + $sth->execute or die $dbh->errstr; + $sth->bind_columns( \$id, \$a_session ); + $sth2 = $dbh->prepare("DELETE FROM sessions WHERE id=?"); + $count = 0; + + while ( $sth->fetch ) { + $lasttime = 0; + if ( $a_session =~ /lasttime:\s+(\d+)/ ) { + $lasttime = $1; + } elsif ( $a_session =~ /(ATIME|CTIME):\s+(\d+)/ ) { + $lasttime = $2; + } + if ( $lasttime && $lasttime < $limit ) { + $sth2->execute($id) or die $dbh->errstr; + $count++; + } } - if($lasttime && $lasttime < $limit) { - $sth2->execute($id) or die $dbh->errstr; - $count++; + if ($verbose) { + print "$count sessions were deleted.\n"; } - } - if ($verbose){ - print "$count sessions were deleted.\n"; - } } -- 2.39.5