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},
pid_file => $args{pid_file},
daemonize => $args{daemonize},
force_log_file => $args{force_log_file},
};
return bless $self, $class;
}
sub run {
my ($self, %args) = @_;
my $pid ||= $PID;
my $pid_file ||= $self->{pid_file};
my $log_file ||= $self->{log_file};
my ($self) = @_;
if ( $self->{daemonize} ) {
$self->_daemonize(
pid => $pid,
pid_file => $pid_file,
log_file => $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');
}
# Just for brevity:
my $daemonize = $self->{daemonize};
my $pid_file = $self->{pid_file};
my $log_file = $self->{log_file};
my $force_log_file = $self->{force_log_file};
return;
}
sub _daemonize {
my ($self, %args) = @_;
my $pid = $args{pid};
my $pid_file = $args{pid_file};
my $log_file = $args{log_file};
PTDEBUG && _d('Daemonizing');
PTDEBUG && _d('Starting daemon');
# 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
@@ -82,27 +59,43 @@ sub _daemonize {
if ( $pid_file ) {
eval {
$self->_make_pid_file(
pid => $pid, # parent's pid
pid => $PID, # parent's pid
pid_file => $pid_file,
);
};
if ( $EVAL_ERROR ) {
die "Cannot daemonize: $EVAL_ERROR\n";
die "$EVAL_ERROR\n" if $EVAL_ERROR;
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.
defined (my $child_pid = fork())
or die "Cannot fork: $OS_ERROR";
if ( $daemonize ) {
defined (my $child_pid = fork()) or die "Cannot fork: $OS_ERROR";
if ( $child_pid ) {
# I'm the parent.
PTDEBUG && _d('Forked child', $child_pid);
exit 0;
}
# I'm the child. First, open the log file, if any. Do this first
# so that all daemon/child output goes there.
# I'm the child.
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
# 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
@@ -144,31 +137,12 @@ sub _daemonize {
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.
POSIX::setsid() or die "Cannot start a new session: $OS_ERROR";
chdir '/' or die "Cannot chdir to /: $OS_ERROR";
# We're not fully daemonized.
PTDEBUG && _d('Daemon running');
return;
}
# Call this for non-daemonized scripts to make a PID file.
sub _make_pid_file {
my ($self, %args) = @_;