# # modules/restart.pm - check, if FastCGI script or user/system # modules was modified # # Developed by Lubomir Host 'rajo' # Copyright (c) 2004 Platon SDG, http://platon.sk/ # Licensed under terms of GNU General Public License. # All rights reserved. # # Changelog: # 09/02/2004 - created # # $Platon: scripts/perl/finance-manager/modules/restart.pm,v 1.2 2004/02/11 12:05:25 rajo Exp $ package restart; use strict; use Carp; use vars qw($VERSION @ISA @EXPORT $AUTOLOAD); @EXPORT = qw( add_files add_loaded_modules restart ); $VERSION = '0.1'; sub new { # {{{ my ($this, $hash) = @_; my $self; if (ref $hash eq 'HASH') { #warn "Using HASH"; $self = { restart_func => $hash->{restart_func}, CHECK_USER_MODULES => $hash->{CHECK_USER_MODULES}, CHECK_SYSTEM_MODULES => $hash->{CHECK_SYSTEM_MODULES}, }; } else { # Defaults: #warn "Using defaults..."; $self = { restart_func => sub { exit 0; }, CHECK_USER_MODULES => 'true', CHECK_SYSTEM_MODULES => 'false', }; } my $class = ref($this) || $this; bless $self, $class; return $self; } # }}} sub add_files ($;@) { # {{{ my ($self, @files) = @_; push @{$self->{checked_files}}, @files; } # }}} sub add_loaded_modules ($;) { # {{{ my ($self) = shift; my @keys = sort keys %INC; #warn scalar(@keys) . " modules loaded"; foreach my $file (@keys) { if ($INC{$file} =~ m#^/#) { if ($self->{CHECK_SYSTEM_MODULES} eq 'true') { #warn "Adding system module '$file'\t=>'$INC{$file}'\n"; push @{$self->{checked_files}}, $INC{$file}; } } elsif ($self->{CHECK_USER_MODULES} eq 'true') { #warn "Adding user module '$file'\t=>'$INC{$file}'\n"; push @{$self->{checked_files}}, $INC{$file}; } } } # }}} sub restart ($;@) { # {{{ my ($self, @files) = @_; my @check_files = @{$self->{checked_files}}; foreach my $file (@files) { push @check_files, $file if defined $file; } if (defined $self->{checked_files}) { foreach my $file (@check_files) { my $modif = -M $file; my $seconds = defined($modif) ? -86400.0 * $modif : 0.0; if (defined($seconds) and $seconds > 0.0) { carp "File '$file' modified $seconds seconds after startup, restarting ..."; &{$self->{restart_func}}; # call function, e.g. exit(0) } } } else { croak "Inavlid usage of restart.pm module!"; } } # }}} 1; __END__ =head1 NAME restart - restart program (e.g. FastCGI script) if modules was moodified =head1 SYNOPSIS use restart; $restart = restart->new({ restart_func => sub { $req->LastCall() }, CHECK_USER_MODULES => 'true', CHECK_SYSTEM_MODULES => 'false', }); $restart->add_loaded_modules(); $restart->add_files(@filename_list); $restart->restart(@other_files); =head1 EXAMPLE #!/usr/bin/perl -w use strict; use lib qw( ./modules ); use FCGI; use restart; use vars qw ( $req $restart ); $req = FCGI::Request(); $restart = restart->new({ restart_func => sub { $req->LastCall() }, CHECK_USER_MODULES => 'true', CHECK_SYSTEM_MODULES => 'false', }); # All modules loaded, push them to the restart.pm: $restart->add_loaded_modules(); my $runlimit = 1000; my $runcount = 0; while ( $runcount++ < $runlimit && ($req->Accept() >= 0) ) { # YOUR CODE HERE print "Content-type: text/html\r\n\r\n"; $restart->restart($ENV{SCRIPT_FILENAME}); $req->Finish(); } 1; =head1 DESCRIPTION The restart module cleanly restart your FastCGI script (or other program), when modules are modified. =head1 AUTHORS Lubomir Host 'rajo', =head1 COPYRIGHT Copyright (c) 2004 Platon SDG, http://platon.sk/ Licensed under terms of GNU General Public License. All rights reserved. =cut # vim: ts=4 # vim600: fdm=marker fdl=0 fdc=3