|
From: <ssc...@us...> - 2003-07-14 23:08:34
|
Update of /cvsroot/popfile/engine/POPFile
In directory sc8-pr-cvs1:/tmp/cvs-serv24175
Modified Files:
Configuration.pm
Log Message:
update pid file checking to only delay startup if a "ghost" pid is found
Also provided some functions utilities can use to check if POPFile is running
Index: Configuration.pm
===================================================================
RCS file: /cvsroot/popfile/engine/POPFile/Configuration.pm,v
retrieving revision 1.22
retrieving revision 1.23
diff -C2 -d -r1.22 -r1.23
*** Configuration.pm 12 Jul 2003 05:59:02 -0000 1.22
--- Configuration.pm 14 Jul 2003 23:08:31 -0000 1.23
***************
*** 31,35 ****
# All the current configuration parameters are stored in this hash which
! # is intended to be globally accessed by modules that make use of this module,
# to register a configuration default entries are made in this hash in the form
#
--- 31,35 ----
# All the current configuration parameters are stored in this hash which
! # is intended to be globally accessed by modules that make use of this module,
# to register a configuration default entries are made in this hash in the form
#
***************
*** 41,44 ****
--- 41,52 ----
$self->{pid_file__} = '';
+ # The time to delay checking of the PID file
+
+ $self->{pid_delay__} = 5;
+
+ # The last time the PID was checked
+
+ $self->{pid_check__} = time;
+
bless $self, $type;
***************
*** 73,77 ****
# purposes must sort on download_count and then message_count
#
! # download_count is incremented every time POPFile forks to
# start a session for downloading messages (see Proxy::Proxy::service
# for details)
--- 81,85 ----
# purposes must sort on download_count and then message_count
#
! # download_count is incremented every time POPFile forks to
# start a session for downloading messages (see Proxy::Proxy::service
# for details)
***************
*** 88,92 ****
$self->global_config_( 'xtc', 1 );
! # Adding the X-POPFile-Link is on
$self->global_config_( 'xpl', 1 );
--- 96,100 ----
$self->global_config_( 'xtc', 1 );
! # Adding the X-POPFile-Link is on
$self->global_config_( 'xpl', 1 );
***************
*** 113,127 ****
$self->{pid_file__} = $self->config_( 'piddir' ) . 'popfile.pid';
! if ( -e $self->{pid_file__} ) {
! my $error = "\n\nAnother copy of POPFile appears to be running. \nIf this is not the case then delete the file \n$self->{pid_file__} and restart POPFile.\n\n";
!
! print STDERR $error;
!
return 0;
}
! if ( open PID, ">$self->{pid_file__}" ) {
! print PID "$$\n";
! close PID;
}
--- 121,157 ----
$self->{pid_file__} = $self->config_( 'piddir' ) . 'popfile.pid';
! if (defined($self->live_check_())) {
return 0;
}
! $self->write_pid_();
!
! return 1;
! }
!
! # ---------------------------------------------------------------------------------------------
! #
! # service
! #
! # service() is a called periodically to give the module a chance to do housekeeping work.
! #
! # If any problem occurs that requires POPFile to shutdown service() should return 0 and
! # the top level process will gracefully terminate POPFile including calling all stop()
! # methods. In normal operation return 1.#
! # ---------------------------------------------------------------------------------------------
! sub service
! {
! my ( $self ) = @_;
!
! my $time = time;
!
! if ( $self->{pid_check__} <= ( $time - $self->{pid_delay__})) {
!
! $self->{pid_check__} = $time;
!
! if ( !$self->check_pid_() ) {
! $self->write_pid_();
! $self->log_("New POPFile instance detected and signalled")
! }
}
***************
*** 129,132 ****
--- 159,164 ----
}
+
+
# ---------------------------------------------------------------------------------------------
#
***************
*** 142,148 ****
--- 174,283 ----
$self->save_configuration();
+ $self->delete_pid_();
+ }
+
+
+ # ---------------------------------------------------------------------------------------------
+ #
+ # live_check_
+ #
+ # Checks if an instance of POPFile is currently running. Takes 10 seconds.
+ # Returns the process-ID of the currently running POPFile, undef if none.
+ #
+ # ---------------------------------------------------------------------------------------------
+ sub live_check_
+ {
+ my ( $self ) = @_;
+
+ if ( $self->check_pid_() ) {
+
+ my $error = "\n\nA copy of POPFile appears to be running.\n Attempting to signal the previous copy.\n Waiting " . ($self->{pid_delay__} * 2) . " seconds for a reply.\n";
+
+ $self->delete_pid_();
+
+ print STDERR $error;
+
+ select(undef, undef, undef, ($self->{pid_delay__} * 2));
+
+ my $pid = $self->get_pid_();
+
+ if (defined($pid)) {
+ $error = "\n A copy of POPFile is running.\n It has signalled that it is alive with proccess ID: $pid\n";
+ print STDERR $error;
+ return $pid;
+ }
+ }
+ return undef;
+ }
+
+
+ # ---------------------------------------------------------------------------------------------
+ #
+ # check_pid_
+ #
+ # returns 1 if the pid file exists, 0 otherwise
+ #
+ # ---------------------------------------------------------------------------------------------
+
+ sub check_pid_
+ {
+ my ( $self ) = @_;
+ return (-e $self->{pid_file__});
+ }
+
+
+ # ---------------------------------------------------------------------------------------------
+ #
+ # get_pid_
+ #
+ # returns the pidfile proccess ID if a pid file is present, undef otherwise (0 might be a valid PID)
+ #
+ # ---------------------------------------------------------------------------------------------
+ sub get_pid_
+ {
+ my ( $self ) = @_;
+
+ if (open PID, $self->{pid_file__}) {
+ my $pid = <PID>;
+ $pid =~ s/[\r\n]//g;
+ close PID;
+ return $pid;
+ }
+
+ return undef;
+ }
+
+ # ---------------------------------------------------------------------------------------------
+ #
+ # write_pid_
+ #
+ # writes the current process-ID into the pid file
+ #
+ # ---------------------------------------------------------------------------------------------
+ sub write_pid_
+ {
+ my ( $self ) = @_;
+
+ if ( open PID, ">$self->{pid_file__}" ) {
+ print PID "$$\n";
+ close PID;
+ }
+ }
+
+ # ---------------------------------------------------------------------------------------------
+ #
+ # delete_pid_
+ #
+ # deletes the pid file
+ #
+ # ---------------------------------------------------------------------------------------------
+ sub delete_pid_
+ {
+ my ( $self ) = @_;
+
unlink( $self->{pid_file__} );
}
+
# ---------------------------------------------------------------------------------------------
#
***************
*** 284,288 ****
if ( open CONFIG, "<popfile.cfg" ) {
while ( <CONFIG> ) {
! s/(\015|\012)//g;
if ( /(\S+) (.+)/ ) {
my $parameter = $1;
--- 419,423 ----
if ( open CONFIG, "<popfile.cfg" ) {
while ( <CONFIG> ) {
! s/(\015|\012)//g;
if ( /(\S+) (.+)/ ) {
my $parameter = $1;
|