3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-20 02:49:49 +01:00

ProcessManager: log process start, end, and duration

This commit is contained in:
Pragmatic Software 2020-02-16 17:45:45 -08:00
parent 64ea719042
commit dca8e4482a

View File

@ -15,6 +15,7 @@ use warnings; use strict;
use feature 'unicode_strings'; use feature 'unicode_strings';
use Time::Duration qw/concise duration/; use Time::Duration qw/concise duration/;
use Time::HiRes qw/gettimeofday/;
use Getopt::Long qw/GetOptionsFromArray/; use Getopt::Long qw/GetOptionsFromArray/;
use POSIX qw/WNOHANG/; use POSIX qw/WNOHANG/;
use JSON; use JSON;
@ -67,7 +68,7 @@ sub ps_cmd {
$result .= "$process->{pid}: $process->{commands}->[0]"; $result .= "$process->{pid}: $process->{commands}->[0]";
if ($show_running_time or $show_all) { if ($show_running_time or $show_all) {
my $duration = concise duration (time - $process->{process_start}); my $duration = concise duration (gettimeofday - $process->{process_start});
$result .= " [$duration]"; $result .= " [$duration]";
} }
@ -133,13 +134,22 @@ sub kill_cmd {
sub add_process { sub add_process {
my ($self, $pid, $stuff) = @_; my ($self, $pid, $stuff) = @_;
$stuff->{process_start} = time; $stuff->{process_start} = gettimeofday;
$self->{processes}->{$pid} = $stuff; $self->{processes}->{$pid} = $stuff;
$self->{pbot}->{logger}->log("Starting process $pid: $stuff->{commands}->[0]\n");
} }
sub remove_process { sub remove_process {
my ($self, $pid) = @_; my ($self, $pid) = @_;
delete $self->{processes}->{$pid}; if (exists $self->{processes}->{$pid}) {
my $command = $self->{processes}->{$pid}->{commands}->[0];
my $duration = gettimeofday - $self->{processes}->{$pid}->{process_start};
$duration = sprintf "%0.3f", $duration;
$self->{pbot}->{logger}->log("Finished process $pid ($command): duration $duration seconds\n");
delete $self->{processes}->{$pid};
} else {
$self->{pbot}->{logger}->log("Finished unknown process $pid\n");
}
} }
sub execute_process { sub execute_process {
@ -190,6 +200,7 @@ sub execute_process {
# check for errors # check for errors
if ($@) { if ($@) {
$stuff->{result} = $@; $stuff->{result} = $@;
$stuff->{'timed-out'} = 1 if $stuff->{result} =~ /^Process .* timed-out at PBot\/ProcessManager/;
$self->{pbot}->{logger}->log("Error executing process: $stuff->{result}\n"); $self->{pbot}->{logger}->log("Error executing process: $stuff->{result}\n");
$stuff->{result} =~ s/ at PBot.*$//ms; $stuff->{result} =~ s/ at PBot.*$//ms;
} }