Large refactoring of pt-agent to make the initial start up experience more reliable, i.e. just run and wait till a good config makes everything else work.

This commit is contained in:
Daniel Nichter
2013-04-05 18:12:33 -06:00
parent 3d5325ae03
commit 2ac102da0b
2 changed files with 560 additions and 596 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -34,44 +34,21 @@ sub new {
log_file => $args{log_file}, log_file => $args{log_file},
pid_file => $args{pid_file}, pid_file => $args{pid_file},
daemonize => $args{daemonize}, daemonize => $args{daemonize},
force_log_file => $args{force_log_file},
}; };
return bless $self, $class; return bless $self, $class;
} }
sub run { sub run {
my ($self, %args) = @_; my ($self) = @_;
my $pid ||= $PID;
my $pid_file ||= $self->{pid_file};
my $log_file ||= $self->{log_file};
if ( $self->{daemonize} ) { # Just for brevity:
$self->_daemonize( my $daemonize = $self->{daemonize};
pid => $pid, my $pid_file = $self->{pid_file};
pid_file => $pid_file, my $log_file = $self->{log_file};
log_file => $log_file, my $force_log_file = $self->{force_log_file};
);
}
elsif ( $pid_file ) {
$self->_make_pid_file(
pid => $pid,
pid_file => $pid_file,
);
$self->{pid_file_owner} = $pid;
}
else {
PTDEBUG && _d('Neither --daemonize nor --pid was specified');
}
return; PTDEBUG && _d('Starting daemon');
}
sub _daemonize {
my ($self, %args) = @_;
my $pid = $args{pid};
my $pid_file = $args{pid_file};
my $log_file = $args{log_file};
PTDEBUG && _d('Daemonizing');
# First obtain the pid file or die trying. NOTE: we're still the parent # First obtain the pid file or die trying. NOTE: we're still the parent
# so the pid file will contain the parent's pid at first. This is done # so the pid file will contain the parent's pid at first. This is done
@@ -82,27 +59,43 @@ sub _daemonize {
if ( $pid_file ) { if ( $pid_file ) {
eval { eval {
$self->_make_pid_file( $self->_make_pid_file(
pid => $pid, # parent's pid pid => $PID, # parent's pid
pid_file => $pid_file, pid_file => $pid_file,
); );
}; };
if ( $EVAL_ERROR ) { die "$EVAL_ERROR\n" if $EVAL_ERROR;
die "Cannot daemonize: $EVAL_ERROR\n"; if ( !$daemonize ) {
# We're not going to daemonize, so mark the pid file as owned
# by the parent. Otherwise, daemonize/fork and the child will
# take ownership.
$self->{pid_file_owner} = $PID; # parent's pid
} }
} }
# Fork, exit parent, continue as child process. # Fork, exit parent, continue as child process.
defined (my $child_pid = fork()) if ( $daemonize ) {
or die "Cannot fork: $OS_ERROR"; defined (my $child_pid = fork()) or die "Cannot fork: $OS_ERROR";
if ( $child_pid ) { if ( $child_pid ) {
# I'm the parent. # I'm the parent.
PTDEBUG && _d('Forked child', $child_pid); PTDEBUG && _d('Forked child', $child_pid);
exit 0; exit 0;
} }
# I'm the child. First, open the log file, if any. Do this first # I'm the child.
# so that all daemon/child output goes there. POSIX::setsid() or die "Cannot start a new session: $OS_ERROR";
chdir '/' or die "Cannot chdir to /: $OS_ERROR";
# Now update the pid file to contain the child's pid.
if ( $pid_file ) {
$self->_update_pid_file(
pid => $PID, # child's pid
pid_file => $pid_file,
);
$self->{pid_file_owner} = $PID;
}
}
if ( $daemonize || $force_log_file ) {
# We used to only reopen STDIN to /dev/null if it's a tty because # We used to only reopen STDIN to /dev/null if it's a tty because
# otherwise it may be a pipe, in which case we didn't want to break # otherwise it may be a pipe, in which case we didn't want to break
# it. However, Perl -t is not reliable. This is true and false on # it. However, Perl -t is not reliable. This is true and false on
@@ -144,31 +137,12 @@ sub _daemonize {
or die "Cannot reopen STDERR to /dev/null: $OS_ERROR"; or die "Cannot reopen STDERR to /dev/null: $OS_ERROR";
} }
} }
# XXX: I don't think we need this?
# $OUTPUT_AUTOFLUSH = 1;
PTDEBUG && _d('I am child', $PID);
# Now update the pid file to contain the correct pid, i.e. the child's pid.
if ( $pid_file ) {
$self->_update_pid_file(
pid => $PID, # child's pid
pid_file => $pid_file,
);
$self->{pid_file_owner} = $PID;
} }
# Last: other misc daemon stuff. PTDEBUG && _d('Daemon running');
POSIX::setsid() or die "Cannot start a new session: $OS_ERROR";
chdir '/' or die "Cannot chdir to /: $OS_ERROR";
# We're not fully daemonized.
return; return;
} }
# Call this for non-daemonized scripts to make a PID file. # Call this for non-daemonized scripts to make a PID file.
sub _make_pid_file { sub _make_pid_file {
my ($self, %args) = @_; my ($self, %args) = @_;