hydra-send-stats: add a --once option for testing

This commit is contained in:
Graham Christensen 2021-03-20 09:12:02 -04:00
parent 6bb180a0f2
commit 425c7ff17f
No known key found for this signature in database
GPG key ID: FE918C3A98C1030F
2 changed files with 25 additions and 1 deletions

View file

@ -6,6 +6,7 @@ use Hydra::Helper::Nix;
use Net::Statsd;
use File::Slurp;
use JSON;
use Getopt::Long qw(:config gnu_getopt);
STDERR->autoflush(1);
binmode STDERR, ":encoding(utf8)";
@ -65,6 +66,26 @@ sub sendQueueRunnerStats {
gauge("hydra.queue.machines.in_use", scalar(grep { $_->{currentJobs} > 0 } (values %{$json->{machines}})));
}
sub showHelp {
print <<EOF;
Usage: $0 [--once]
Send stats to statsd. The --once flag can be used to send one round
of stats and then exit.
Example:
\$ $0 --once
EOF
exit 0;
}
my $once = 0;
GetOptions("once" => \$once,
"help" => sub { showHelp() }
) or exit 1;
while (1) {
eval {
sendQueueRunnerStats();
@ -78,5 +99,8 @@ while (1) {
gauge("hydra.mem.dirty", $dirty);
}
if ($once) {
last;
}
sleep(30);
}

View file

@ -12,7 +12,7 @@ use Test2::V0;
my $db = Hydra::Model::DB->new;
hydra_setup($db);
my ($res, $stdout, $stderr) = captureStdoutStderr(60, ("hydra-send-stats"));
my ($res, $stdout, $stderr) = captureStdoutStderr(60, ("hydra-send-stats", "--once"));
is($stdout, "", "hydra-send-stats stdout should be empty");
is($stderr, "", "hydra-send-stats stderr should be empty");
is($res, 0, "hydra-send-stats --once should exit zero");