| version 1.2, 2004/05/16 13:50:12 |
version 1.3, 2004/05/16 15:09:12 |
|
|
| # 09/02/2004 - created |
# 09/02/2004 - created |
| # |
# |
| |
|
| # $Platon: perl-modules/Platon/FastCGI/Restart.pm,v 1.1.1.1 2004/05/16 13:32:04 rajo Exp $ |
# $Platon: perl-modules/Platon/FastCGI/Restart.pm,v 1.2 2004/05/16 13:50:12 rajo Exp $ |
| |
|
| package Platon::FastCGI::Restart; |
package Platon::FastCGI::Restart; |
| |
|
| use strict; |
use strict; |
| use Carp; |
use Carp; |
| use vars qw($VERSION $DEBUG @EXPORT $AUTOLOAD); |
use English; |
| |
use File::stat; |
| |
use Digest::MD5; |
| |
use vars qw($VERSION $DEBUG $CLASS @EXPORT $AUTOLOAD); |
| |
|
| $VERSION = sprintf("%d.%02d", q$Revision$ =~ /(\d+)\.(\d+)/); |
$VERSION = sprintf("%d.%02d", q$Revision$ =~ /(\d+)\.(\d+)/); |
| $DEBUG = 0 unless defined $DEBUG; |
$DEBUG = 0 unless defined $DEBUG; |
|
|
| #warn "Using HASH"; |
#warn "Using HASH"; |
| $self = { |
$self = { |
| restart_func => $hash->{restart_func}, |
restart_func => $hash->{restart_func}, |
| CHECK_USER_MODULES => $hash->{CHECK_USER_MODULES}, |
use_lock => defined($hash->{use_lock}) ? $hash->{use_lock} : 'true', |
| CHECK_SYSTEM_MODULES => $hash->{CHECK_SYSTEM_MODULES}, |
lock_dir => defined($hash->{lock_dir}) ? $hash->{lock_dir} : '/var/tmp', |
| |
max_restart_interval => defined($hash->{max_restart_interval}) ? $hash->{max_restart_interval} : 60, |
| |
CHECK_USER_MODULES => defined($hash->{CHECK_USER_MODULES}) ? $hash->{CHECK_USER_MODULES} : 'true', |
| |
CHECK_SYSTEM_MODULES => defined($hash->{CHECK_SYSTEM_MODULES}) ? $hash->{CHECK_SYSTEM_MODULES} : 'false', |
| }; |
}; |
| } |
} |
| else { |
else { |
| # Defaults: |
# Defaults: |
| #warn "Using defaults..."; |
|
| $self = { |
$self = { |
| restart_func => sub { exit 0; }, |
restart_func => &exit(0), |
| |
use_lock => 'true', |
| |
lock_dir => '/var/tmp', |
| |
max_restart_interval => 60, |
| CHECK_USER_MODULES => 'true', |
CHECK_USER_MODULES => 'true', |
| CHECK_SYSTEM_MODULES => 'false', |
CHECK_SYSTEM_MODULES => 'false', |
| }; |
}; |
| } |
} |
| my $class = ref($this) || $this; |
my $class = ref($this) || $this; |
| |
$CLASS = $class; |
| |
|
| bless $self, $class; |
bless $self, $class; |
| |
|
| Line 89 sub restart ($;@) |
|
| Line 98 sub restart ($;@) |
|
| foreach my $file (@files) { |
foreach my $file (@files) { |
| push @check_files, $file if defined $file; |
push @check_files, $file if defined $file; |
| } |
} |
| |
|
| |
my $lock_file; |
| |
if ($self->{use_lock} eq 'true') { |
| |
$lock_file = "$self->{lock_dir}/fastcgi_restart-$EUID-" . Digest::MD5::md5_hex(join('', @files)); |
| |
} |
| |
|
| if (defined $self->{checked_files}) { |
if (defined $self->{checked_files}) { |
| foreach my $file (@check_files) { |
foreach my $file (@check_files) { |
| my $modif = -M $file; |
my $modif = -M $file; |
| my $seconds = defined($modif) ? -86400.0 * $modif : 0.0; |
my $seconds = defined($modif) ? -86400.0 * $modif : 0.0; |
| if (defined($seconds) and $seconds > 0.0) { |
if (defined($seconds) and $seconds > 0.0) { |
| carp "File '$file' modified $seconds seconds after startup, restarting ..."; |
if (defined($lock_file)) { # use lockfiles |
| |
if (-f $lock_file) { # lock file exists |
| |
my $st = stat($lock_file); |
| |
my $lock_modif = time - $st->mtime(); |
| |
|
| |
if ($lock_modif < $self->{max_restart_interval}) { |
| |
carp "$CLASS: Max. restart interval is $self->{max_restart_interval}, lockfile created $lock_modif seconds ago. Restart delayled ..."; |
| |
return; |
| |
} |
| |
} |
| |
|
| |
# create lockfile |
| |
open FILE, ">$lock_file" or croak "Can't create lock file '$file': $!"; |
| |
print FILE $PID; # write pid to this file |
| |
close FILE; |
| |
} |
| |
|
| |
carp "$CLASS: File '$file' modified $seconds seconds after startup, restarting ..."; |
| &{$self->{restart_func}}; # call function, e.g. exit(0) |
&{$self->{restart_func}}; # call function, e.g. exit(0) |
| } |
} |
| } |
} |
| } |
} |
| else { |
else { |
| croak "Inavlid usage of restart.pm module!"; |
croak "$CLASS: Invalid module usage!"; |
| } |
} |
| |
|
| } # }}} |
} # }}} |
| |
|
| |
sub version ($;) |
| |
{ # {{{ |
| |
return $VERSION; |
| |
} # }}} |
| |
|
| 1; |
1; |
| |
|
| __END__ |
__END__ |
| Line 120 restart - restart program (e.g. FastCGI |
|
| Line 156 restart - restart program (e.g. FastCGI |
|
| use Platon::FastCGI::Restart; |
use Platon::FastCGI::Restart; |
| |
|
| $restart = Platon::FastCGI::Restart->new({ |
$restart = Platon::FastCGI::Restart->new({ |
| restart_func => sub { $req->LastCall() }, |
restart_func => sub { $req->LastCall() }, |
| CHECK_USER_MODULES => 'true', |
use_lock => 'true', |
| CHECK_SYSTEM_MODULES => 'false', |
lock_dir => '/var/tmp', |
| |
max_restart_interval => 60, |
| |
CHECK_USER_MODULES => 'true', |
| |
CHECK_SYSTEM_MODULES => 'false', |
| }); |
}); |
| |
|
| $restart->add_loaded_modules(); |
$restart->add_loaded_modules(); |
| Line 152 restart - restart program (e.g. FastCGI |
|
| Line 191 restart - restart program (e.g. FastCGI |
|
| CHECK_SYSTEM_MODULES => 'false', |
CHECK_SYSTEM_MODULES => 'false', |
| }); |
}); |
| |
|
| # All modules loaded, push them to the restart.pm: |
# All modules loaded, push them to the Platon::FastCGI::Restart: |
| $restart->add_loaded_modules(); |
$restart->add_loaded_modules(); |
| |
|
| my $runlimit = 1000; |
my $runlimit = 1000; |
| Line 175 The Platon::FastCGI::Restart module clea |
|
| Line 214 The Platon::FastCGI::Restart module clea |
|
| |
|
| =head1 AUTHORS |
=head1 AUTHORS |
| |
|
| Lubomir Host 'rajo', <rajo AT platon.sk> |
Lubomir Host 'rajo', <rajo AT platon.sk> |
| |
|
| =head1 COPYRIGHT |
=head1 COPYRIGHT |
| |
|
| Copyright (c) 2004 Platon SDG, http://platon.sk/ |
Copyright (c) 2004 Platon SDG, http://platon.sk/ |
| Licensed under terms of GNU General Public License. |
Licensed under terms of GNU General Public License. |
| All rights reserved. |
All rights reserved. |
| |
|
| =cut |
=cut |
| |
|