| 1 | package OpenInteract2::Manage::Website::CleanExpiredSessions; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | # $Id: CleanExpiredSessions.pm,v 1.5 2005/03/18 04:09:50 lachoy Exp $ | ||||||||||||||
| 4 | |||||||||||||||
| 5 | 87 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 87 | ▶ | use base qw( OpenInteract2::Manage::Website ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 87 | ▶ | use MIME::Base64; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 87 | ▶ | use OpenInteract2::Context qw( CTX ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 87 | ▶ | use OpenInteract2::Exception qw( oi_error ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 87 | ▶ | use Storable qw( thaw ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | 87 | my $TEMP_TRACK_FILE = 'tmp_session_id'; | |||||||||||||
| 13 | 87 | my $DECODE = 'base64'; | |||||||||||||
| 14 | |||||||||||||||
| 15 | 87 | $OpenInteract2::Manage::Website::CleanExpiredSessions::VERSION = sprintf("%d.%02d", q$Revision: 1.5 $ =~ /(\d+)\.(\d+)/); | |||||||||||||
| 16 | |||||||||||||||
| 17 | # METADATA | ||||||||||||||
| 18 | |||||||||||||||
| 19 | 87 | ▶ | sub get_name { | ||||||||||||
|
Subroutine
get_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 20 | 87 | return 'clean_sessions'; | |||||||||||||
| 21 | } | ||||||||||||||
| 22 | |||||||||||||||
| 23 | 0 | ▶ | sub get_brief_description { | ||||||||||||
|
Subroutine
get_brief_description: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 24 | 0 | return "Remove sessions older than a given number of days."; | |||||||||||||
| 25 | } | ||||||||||||||
| 26 | |||||||||||||||
| 27 | 1 | ▶ | sub get_parameters { | ||||||||||||
|
Subroutine
get_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 28 | 1 | my ( $self ) = @_; | |||||||||||||
| 29 | return { | ||||||||||||||
| 30 | 1 | expire_time => { | |||||||||||||
| 31 | description => | ||||||||||||||
| 32 | "Days older than which I'll delete sessions. (If you " . | ||||||||||||||
| 33 | "give me 60 I'll delete sessions older than 60 days.) ", | ||||||||||||||
| 34 | is_required => 'yes', | ||||||||||||||
| 35 | }, | ||||||||||||||
| 36 | analyze => { | ||||||||||||||
| 37 | description => | ||||||||||||||
| 38 | "If enabled I'll only run an analysis and " . | ||||||||||||||
| 39 | "perform no removals.", | ||||||||||||||
| 40 | is_boolean => 'yes', | ||||||||||||||
| 41 | } | ||||||||||||||
| 42 | }; | ||||||||||||||
| 43 | } | ||||||||||||||
| 44 | |||||||||||||||
| 45 | # VALIDATION | ||||||||||||||
| 46 | |||||||||||||||
| 47 | 0 | ▶ | sub validate_param { | ||||||||||||
|
Subroutine
validate_param: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||
| 48 | 0 | my ( $self, $param_name, $value ) = @_; | |||||||||||||
| 49 | 0 | ▶ | if ( $param_name eq 'expire_time' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 50 | 0 | my $days = int( $value ); | |||||||||||||
| 51 | 0 | ▶ | unless ( $days ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 52 | 0 | return "Cannot find valid number from value"; | |||||||||||||
| 53 | } | ||||||||||||||
| 54 | 0 | $self->param( expire_time => $days ); | |||||||||||||
| 55 | } | ||||||||||||||
| 56 | } | ||||||||||||||
| 57 | |||||||||||||||
| 58 | # TASK | ||||||||||||||
| 59 | |||||||||||||||
| 60 | 0 | ▶ | sub run_task { | ||||||||||||
|
Subroutine
run_task: not called CC=13 SCAR=52.0
Pod
undocumented
| |||||||||||||||
| 61 | 0 | my ( $self ) = @_; | |||||||||||||
| 62 | 0 | my ( $removed, $kept, $count ); | |||||||||||||
| 63 | 0 | my $action = 'clean sessions'; | |||||||||||||
| 64 | 0 | my $session_info = CTX->lookup_session_config; | |||||||||||||
| 65 | 0 | ▶ | unless ( $session_info->{datasource} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 66 | 0 | my $msg = "'Cannot run: nothing defined in 'session_info.datasource'"; | |||||||||||||
| 67 | 0 | return $self->_fail( $action, $msg ); | |||||||||||||
| 68 | } | ||||||||||||||
| 69 | 0 | my $dbh = CTX->datasource( $session_info->{datasource} ); | |||||||||||||
| 70 | |||||||||||||||
| 71 | 0 | $count = eval { $self->_get_session_ids( $dbh ) }; | |||||||||||||
| 72 | 0 | ▶ | if ( $@ ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 73 | 0 | return $self->_fail( $action, "Failed to get session IDs: $@" ); | |||||||||||||
| 74 | } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | 0 | ▶ | eval { open( IDLIST, '<', $TEMP_TRACK_FILE ) || die $! }; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 77 | 0 | ▶ | if ( $@ ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 78 | 0 | return $self->_fail( $action, "Cannot reopen session list: $@" ); | |||||||||||||
| 79 | } | ||||||||||||||
| 80 | |||||||||||||||
| 81 | 0 | my $below_thresh = time - ( $self->param( 'expire_time' ) * 86400 ); | |||||||||||||
| 82 | 0 | my $current = 0; | |||||||||||||
| 83 | |||||||||||||||
| 84 | 0 | my $sql = "SELECT a_session FROM sessions WHERE id = ?"; | |||||||||||||
| 85 | 0 | my $analyze = $self->param( 'analyze' ); | |||||||||||||
| 86 | ID: | ||||||||||||||
| 87 | 0 | ▶ | while ( <IDLIST> ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 88 | 0 | chomp; | |||||||||||||
| 89 | 0 | my $id = $_; | |||||||||||||
| 90 | |||||||||||||||
| 91 | # don't move the prepare outside the while even tho it makes | ||||||||||||||
| 92 | # sense, but we want to keep a single handle open | ||||||||||||||
| 93 | |||||||||||||||
| 94 | 0 | my ( $raw_data ); | |||||||||||||
| 95 | my ( $sth ); | ||||||||||||||
| 96 | 0 | eval { | |||||||||||||
| 97 | 0 | $dbh->prepare( $sql ); | |||||||||||||
| 98 | 0 | $sth->execute( $id ); | |||||||||||||
| 99 | 0 | ( $raw_data ) = $sth->fetchrow_array; | |||||||||||||
| 100 | 0 | $sth->finish; | |||||||||||||
| 101 | }; | ||||||||||||||
| 102 | 0 | ▶ | if ( $@ ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 103 | 0 | return $self->_fail( $action, "Cannot get session from db: $@" ); | |||||||||||||
| 104 | } | ||||||||||||||
| 105 | |||||||||||||||
| 106 | 0 | my $session = $self->_decode( $raw_data ); | |||||||||||||
| 107 | 0 | ▶ | my $timestamp = $session->{timestamp} || 0; | ||||||||||||
|
Condition0%0/2
$session->{'timestamp'} || 0
MC/DC0%0/1
$session->{'timestamp'} || 0
$session->{'timestamp'}
Truth table0%0/2
A
A= $session->{'timestamp'}
| |||||||||||||||
| 108 | 0 | my $do_delete = 0; | |||||||||||||
| 109 | |||||||||||||||
| 110 | # ...empty session | ||||||||||||||
| 111 | 0 | ▶ | if ( scalar keys %{ $session } == 0 ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 112 | 0 | $do_delete++; | |||||||||||||
| 113 | } | ||||||||||||||
| 114 | |||||||||||||||
| 115 | # ...expired session | ||||||||||||||
| 116 | elsif ( $timestamp < $below_thresh ) { | ||||||||||||||
| 117 | 0 | $do_delete++; | |||||||||||||
| 118 | } | ||||||||||||||
| 119 | else { | ||||||||||||||
| 120 | 0 | $kept++; | |||||||||||||
| 121 | } | ||||||||||||||
| 122 | |||||||||||||||
| 123 | 0 | ▶ | if ( $do_delete ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 124 | 0 | my $delete_sql = "DELETE FROM sessions WHERE id = '$id'"; | |||||||||||||
| 125 | 0 | ▶ | $analyze || $dbh->do( $delete_sql ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 126 | 0 | $removed++; | |||||||||||||
| 127 | } | ||||||||||||||
| 128 | 0 | $current++; | |||||||||||||
| 129 | } | ||||||||||||||
| 130 | 0 | close( IDLIST ); | |||||||||||||
| 131 | |||||||||||||||
| 132 | 0 | my $msg = "Results: ($count) total; ($removed) removed; ($kept) kept"; | |||||||||||||
| 133 | 0 | ▶ | if ( $analyze ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 134 | 0 | $msg .= " (analysis only, no sessions removed)"; | |||||||||||||
| 135 | } | ||||||||||||||
| 136 | 0 | $self->_ok( $action, $msg ); | |||||||||||||
| 137 | } | ||||||||||||||
| 138 | |||||||||||||||
| 139 | 0 | ▶ | sub tear_down_task { | ||||||||||||
|
Subroutine
tear_down_task: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 140 | 0 | my ( $self ) = @_; | |||||||||||||
| 141 | 0 | ▶ | if ( -f $TEMP_TRACK_FILE ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 142 | 0 | unlink( $TEMP_TRACK_FILE ); | |||||||||||||
| 143 | } | ||||||||||||||
| 144 | } | ||||||||||||||
| 145 | |||||||||||||||
| 146 | # Grab all the session_ids and print them to a file, one per line, so | ||||||||||||||
| 147 | # we only have to keep one handle open | ||||||||||||||
| 148 | |||||||||||||||
| 149 | 0 | ▶ | sub _get_session_ids { | ||||||||||||
|
Subroutine
_get_session_ids: not called CC=3 SCAR=24.8
| |||||||||||||||
| 150 | 0 | my ( $self, $dbh ) = @_; | |||||||||||||
| 151 | 0 | my $sql = qq/ SELECT id FROM sessions /; | |||||||||||||
| 152 | 0 | my $sth = $dbh->prepare( $sql ); | |||||||||||||
| 153 | 0 | $sth->execute; | |||||||||||||
| 154 | |||||||||||||||
| 155 | 0 | my $count = 0; | |||||||||||||
| 156 | 0 | ▶ | open( IDLIST, '>', $TEMP_TRACK_FILE ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 157 | || oi_error "Cannot open tracking file $TEMP_TRACK_FILE: $!"; | ||||||||||||||
| 158 | 0 | my ( $id ); | |||||||||||||
| 159 | 0 | $sth->bind_col( 1, \$id ); | |||||||||||||
| 160 | 0 | ▶ | while ( $sth->fetch ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 161 | 0 | print IDLIST "$id\n"; | |||||||||||||
| 162 | 0 | $count++; | |||||||||||||
| 163 | } | ||||||||||||||
| 164 | 0 | $sth->finish; | |||||||||||||
| 165 | 0 | close( IDLIST ); | |||||||||||||
| 166 | 0 | return $count; | |||||||||||||
| 167 | } | ||||||||||||||
| 168 | |||||||||||||||
| 169 | 0 | ▶ | sub _decode { | ||||||||||||
|
Subroutine
_decode: not called CC=3 SCAR=24.8
| |||||||||||||||
| 170 | 0 | my ( $self, $type, $data ) = @_; | |||||||||||||
| 171 | 0 | ▶ | if ( $type eq 'base64' ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 172 | 0 | return thaw( decode_base64( $data ) ); | |||||||||||||
| 173 | } | ||||||||||||||
| 174 | elsif ( $type eq 'storable' ) { | ||||||||||||||
| 175 | 0 | return thaw( $data ); | |||||||||||||
| 176 | } | ||||||||||||||
| 177 | else { | ||||||||||||||
| 178 | 0 | oi_error "Cannot decode type [$type] unknown\n"; | |||||||||||||
| 179 | } | ||||||||||||||
| 180 | } | ||||||||||||||
| 181 | |||||||||||||||
| 182 | 87 | OpenInteract2::Manage->register_factory_type( get_name() => __PACKAGE__ ); | |||||||||||||
| 183 | |||||||||||||||
| 184 | 87 | 1; | |||||||||||||
| 185 | |||||||||||||||
| 186 | __END__ | ||||||||||||||