Log --run-service and --send-data. Spool to separate files with .TS suffixes. Use /tmp until --lib is valid. Daemonize early. Replace __TS_MICRO__ in task options.

This commit is contained in:
Daniel Nichter
2013-03-26 09:08:37 -06:00
parent 6e1e70477e
commit cbc67774b3

View File

@@ -4724,14 +4724,42 @@ sub main {
exit $exit_status;
}
# ########################################################################
# Daemonize first so all output goes to the --log.
# ########################################################################
my $daemon;
if ( !$o->get('send-data') ) {
if ( $o->get('daemonize') ) {
$daemon = new Daemon(o=>$o);
$daemon->daemonize();
PTDEBUG && _d('I am a daemon now');
}
elsif ( $o->get('pid') ) {
$daemon = new Daemon(o=>$o);
$daemon->make_PID_file();
}
# If we daemonized, the parent has already exited and we're the child.
# We shared a copy of every Cxn with the parent, and the parent's copies
# were destroyed but the dbhs were not disconnected because the parent
# attrib was true. Now, as the child, set it false so the dbhs will be
# disconnected when our Cxn copies are destroyed. If we didn't daemonize,
# then we're not really a parent (since we have no children), so set it
# false to auto-disconnect the dbhs when our Cxns are destroyed.
$cxn->{parent} = 0;
}
# ########################################################################
# Connect to the Percona web API.
# ########################################################################
# TODO: --send-data should not use this because it calls init_agent()
my $lib_dir = $o->get('lib');
my ($client, $agent);
eval {
($client, $agent) = connect_to_percona(
api_key => $api_key,
lib_dir => $o->get('lib'),
lib_dir => $lib_dir,
Cxn => $cxn,
agent_uuid => $o->get('agent-uuid'), # optional
);
@@ -4764,26 +4792,6 @@ sub main {
# external errors, like Percona web API not responding, should be
# retried forever.
# ########################################################################
# Daemonize first so all output goes to the --log.
my $daemon;
if ( $o->get('daemonize') ) {
$daemon = new Daemon(o=>$o);
$daemon->daemonize();
PTDEBUG && _d('I am a daemon now');
}
elsif ( $o->get('pid') ) {
$daemon = new Daemon(o=>$o);
$daemon->make_PID_file();
}
# If we daemonized, the parent has already exited and we're the child.
# We shared a copy of every Cxn with the parent, and the parent's copies
# were destroyed but the dbhs were not disconnected because the parent
# attrib was true. Now, as the child, set it false so the dbhs will be
# disconnected when our Cxn copies are destroyed. If we didn't daemonize,
# then we're not really a parent (since we have no children), so set it
# false to auto-disconnect the dbhs when our Cxns are destroyed.
$cxn->{parent} = 0;
# Check and init the config file.
my $config_file = get_config_file();
@@ -4806,6 +4814,15 @@ sub main {
}
}
# Check --lib. Until the agent is configured, the default lib dir
# may not work. Save stuff in /tmp so if we stop and start again
# we don't try to create a new agent again.
if ( !-d $lib_dir || !-w $lib_dir ) {
_info("--lib $lib_dir does not exist or is not writeable,"
. " using /tmp until the agent is configured");
$lib_dir = '/tmp';
}
# Wait time between checking for new config and services.
# Use the tool's built-in default until a config is gotten,
# then config->{check-interval} will be pass in.
@@ -4824,7 +4841,7 @@ sub main {
agent => $agent,
client => $client,
interval => $check_wait,
lib_dir => $o->get('lib'),
lib_dir => $lib_dir,
);
_info("pt-agent exit $exit_status, oktorun $oktorun");
@@ -5056,7 +5073,12 @@ sub init_agent {
link => $agent_uri,
);
_info("Agent " . $agent->name . " initialized and ready");
save_agent(
agent => $agent,
lib_dir => $lib_dir,
);
_info("Agent " . $agent->name . "(" . $agent->uuid . ") is ready");
return $agent;
}
@@ -5545,7 +5567,14 @@ sub run_service {
my $lib_dir = $args{lib_dir};
my $cxn = $args{Cxn};
# TODO: where should this output go?
my $log_file = "$lib_dir/$service.run.log";
close STDOUT;
open STDOUT, '>>', $log_file
or die "Cannot open log file $log_file: $OS_ERROR";
close STDERR;
open STDERR, ">&STDOUT"
or die "Cannot dupe STDERR to STDOUT: $OS_ERROR";
_info("Running $service service");
$service = load_service(
@@ -5574,7 +5603,7 @@ sub run_service {
my @output_files;
my $final_exit_status = 0;
my $spool_file = "$spool_dir/" . $service->name;
my $spool_file = "$spool_dir/" . $service->name . '.' . int(time);
my $taskno = 0;
TASK:
foreach my $task ( @$tasks ) {
@@ -5587,7 +5616,7 @@ sub run_service {
my $output_file;
my $output = $task->output || '';
if ( $output eq 'spool' ) {
$output_file = $spool_file;
$output_file = $spool_file;
push @output_files, $spool_file;
# TODO: mkdir the spool dif?
}
@@ -5689,6 +5718,8 @@ sub replace_special_vars {
my $cmd = $args{cmd};
my $output_files = $args{output_files};
my $ts_micro = sprintf('%.6f', time);
my $new_cmd = join(' ',
map {
my $word = $_;
@@ -5700,6 +5731,7 @@ sub replace_special_vars {
die "Run$runno has no output for $word to access.\n";
}
}
$word =~ s/__TS_MICRO__/$ts_micro/g;
$word;
}
split(/\s+/, $cmd)
@@ -5737,6 +5769,14 @@ sub send_data {
my $service_dir = $spool_dir . '/' . $service;
my $service_file = $lib_dir . '/services/' . $service;
my $log_file = "$lib_dir/$service.send.log";
close STDOUT;
open STDOUT, '>>', $log_file
or die "Cannot open log file $log_file: $OS_ERROR";
close STDERR;
open STDERR, ">&STDOUT"
or die "Cannot dupe STDERR to STDOUT: $OS_ERROR";
# Re-create the Service resource object from the saved service file.
# TODO: test
if ( !-f $service_file ) {