You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Tracy12 <j_...@yahoo.com> on 2007/01/19 01:53:22 UTC

mod_perl best practices cleanup

Hi,

I could get my Perl (mod_perl) Authentication handler with the CGI:Session
package, It is working fine.

We decided to keep the session data on LAN (file). 

As there will be new file created for each Session, We just wanted to know
how to cleanup this folder, basically to clean all the expired sessions.
Which part of the perl auth handler should do this cleanup

How can we do the above , Sample code segment in our PerlAuth handler looks
as follows

my $session = new CGI::Session() or die CGI::Session->errstr;

$session = new CGI::Session(undef, undef, {Directory=>'../tmp/sessions'});


if ( $session->is_expired ) {
 $session = $session->new();
}
if ( $session->is_empty ) {
        $session = $session->new();
 }

-- 
View this message in context: http://www.nabble.com/mod_perl-best-practices-cleanup-tf3037692.html#a8442512
Sent from the mod_perl - General mailing list archive at Nabble.com.


Re: mod_perl best practices cleanup

Posted by Rhesa <rh...@rhesa.com>.
Tracy12 wrote:
> Looks like cron job will do, but is there any docs to write such a script.
> 

See CGI::Session::ExpireSessions 
(http://search.cpan.org/perldoc?CGI%3A%3ASession%3A%3AExpireSessions) for a 
complete solution).

HTH,
Rhesa


Re: mod_perl best practices cleanup

Posted by Tracy12 <j_...@yahoo.com>.
Looks like cron job will do, but is there any docs to write such a script.

Meantime I found a sample as follows, as I am not having any DSN  but only a
folder /tmp to store session data will this work or how this can be modified



#!/usr/bin/perl

use constant DSN        => 'driver:file';
use constant DSN_ARGS   => {};

use CGI::Session;

CGI::Session->find( DSN, sub {}, DSN_ARGS ) or die CGI::Session->errstr;



Jonathan Vanasco-3 wrote:
> 
>>> Which part of the perl auth handler should do this cleanup
> 
> Just to add though -- if you really want to do a cleanup within MP,  
> use the cleanup handler which works after the client connection is  
> terminated.  also try setting a global timestamp so you only do the  
> cleanup code once every 15 minutes or so, instead of on every request.
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/mod_perl-best-practices-cleanup-tf3037692.html#a8442858
Sent from the mod_perl - General mailing list archive at Nabble.com.


Re: mod_perl best practices cleanup

Posted by Robert Landrum <rl...@aol.net>.
Tracy12 wrote:
> I tried to declare 
> use vars qw( $SESSION_CLEANUP_COUNTER); and increment within the code doent
> seem to work,
> 
> Shoud i increment this in a specific hanlder.
> 

You really need to heed the advice of the list and consider using the a 
cron job to expire old sessions.  It's really not that hard to do.

#!/usr/bin/perl

use strict;
my $path = shift;  # path to session directory
my $exp = shift;   # time sessions are valid (in seconds)
my $now = time;
opendir(DIR,$path) or die "could not open dir: $path: $!";
while(my $file = readdir(DIR)) {
   my $fp = "$path/$file";
   my $mtime = (stat($fp))[9];
   if($now > ($mtime + $exp)) {
     unlink($fp);  # remove the file if expired
   }
}
closedir(DIR);

__END__

I didn't test this, so it probably doesn't work as written.  But it 
should be close enough to give you the idea.  All it does is delete 
files when they have been sitting around for more than a certain number 
of seconds.

Add that to cron as something like

* * * * * /path/to/session_remover.pl /path/to/session/dir 3600

That'll check for newly expired sessions every minute and delete any 
that are older than 3600 seconds (1 hour).

Good luck,

Rob

Re: mod_perl best practices cleanup

Posted by Jonathan Vanasco <jv...@2xlp.com>.
On Jan 21, 2007, at 5:49 PM, Tracy12 wrote:

>
> If have a Authentication handler written in perl,
>
> How can count the no of times it has been accessed by clients,  
> (Some think
> like no of hits .  Counter).
>
> Is there a specific handler to do this ?
>
> I tried to declare
> use vars qw( $SESSION_CLEANUP_COUNTER); and increment within the  
> code doent
> seem to work,
>
> Shoud i increment this in a specific hanlder.
>
> Thanks


variables in modperl are copy on write.  that means  
$SESSION_CLEANUP_COUNTER is independent to each apache child.
you need to look for shared memory modules on cpan, or use a database.



// Jonathan Vanasco

| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
- - - - - - - - - - - - - - - -
| FindMeOn.com - The cure for Multiple Web Personality Disorder
| Web Identity Management and 3D Social Networking
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
- - - - - - - - - - - - - - - -
| RoadSound.com - Tools For Bands, Stuff For Fans
| Collaborative Online Management And Syndication Tools
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
- - - - - - - - - - - - - - - -



Re: mod_perl best practices cleanup

Posted by Tracy12 <j_...@yahoo.com>.
If have a Authentication handler written in perl,

How can count the no of times it has been accessed by clients, (Some think
like no of hits .  Counter).

Is there a specific handler to do this ?

I tried to declare 
use vars qw( $SESSION_CLEANUP_COUNTER); and increment within the code doent
seem to work,

Shoud i increment this in a specific hanlder.

Thanks



Jonathan Vanasco-3 wrote:
> 
>>> Which part of the perl auth handler should do this cleanup
> 
> Just to add though -- if you really want to do a cleanup within MP,  
> use the cleanup handler which works after the client connection is  
> terminated.  also try setting a global timestamp so you only do the  
> cleanup code once every 15 minutes or so, instead of on every request.
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/mod_perl-best-practices-cleanup-tf3037692.html#a8480717
Sent from the mod_perl - General mailing list archive at Nabble.com.


Re: mod_perl best practices cleanup

Posted by Tracy12 <j_...@yahoo.com>.
I introduced a


 use vars qw( $SESSION_CLEANUP_COUNTER);

also having if not defined value =0,

and 

$SESSION_CLEANUP_COUNTER

When it reaches the threshold do the cleanup, but it always go to the
undefined clause and return zero,
had use vars qw been deprecated?




Tracy12 wrote:
> 
> I have a general understanding about cleanup handler
> 
>  But what is meant by the global timestamp and cleaning up in every 15
> minutes,
> 
>  Programatically How can we achieve it
>  Are u refereing to a global variable
> 
> 
> 
> Jonathan Vanasco-3 wrote:
>> 
>>>> Which part of the perl auth handler should do this cleanup
>> 
>> Just to add though -- if you really want to do a cleanup within MP,  
>> use the cleanup handler which works after the client connection is  
>> terminated.  also try setting a global timestamp so you only do the  
>> cleanup code once every 15 minutes or so, instead of on every request.
>> 
>> 
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/mod_perl-best-practices-cleanup-tf3037692.html#a8444348
Sent from the mod_perl - General mailing list archive at Nabble.com.


Re: mod_perl best practices cleanup

Posted by Tracy12 <j_...@yahoo.com>.
I have a general understanding about cleanup handler

 But what is meant by the global timestamp and cleaning up in every 15
minutes,

 Programatically How can we achieve it
 Are u refereing to a global variable



Jonathan Vanasco-3 wrote:
> 
>>> Which part of the perl auth handler should do this cleanup
> 
> Just to add though -- if you really want to do a cleanup within MP,  
> use the cleanup handler which works after the client connection is  
> terminated.  also try setting a global timestamp so you only do the  
> cleanup code once every 15 minutes or so, instead of on every request.
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/mod_perl-best-practices-cleanup-tf3037692.html#a8444035
Sent from the mod_perl - General mailing list archive at Nabble.com.


Re: mod_perl best practices cleanup

Posted by Jonathan Vanasco <jv...@2xlp.com>.
>> Which part of the perl auth handler should do this cleanup

Just to add though -- if you really want to do a cleanup within MP,  
use the cleanup handler which works after the client connection is  
terminated.  also try setting a global timestamp so you only do the  
cleanup code once every 15 minutes or so, instead of on every request.


Re: mod_perl best practices cleanup

Posted by Jonathan Vanasco <jv...@2xlp.com>.
On Jan 18, 2007, at 7:53 PM, Tracy12 wrote:

> We decided to keep the session data on LAN (file).
>
> As there will be new file created for each Session, We just wanted  
> to know
> how to cleanup this folder, basically to clean all the expired  
> sessions.
> Which part of the perl auth handler should do this cleanup

none.

use a cron job to clear out files that haven't been modified within X  
time.  run it ever 15-30 minutes.

you can also store sessions in a db, and just run a cron job that  
deletes old sessions every so often.

while its possible to run a cleanup in your code, its way easier to  
just run a cron job.


// Jonathan Vanasco

| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
- - - - - - - - - - - - - - - -
| FindMeOn.com - The cure for Multiple Web Personality Disorder
| Web Identity Management and 3D Social Networking
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
- - - - - - - - - - - - - - - -
| RoadSound.com - Tools For Bands, Stuff For Fans
| Collaborative Online Management And Syndication Tools
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
- - - - - - - - - - - - - - - -