From d849856dcd50ac7468f1a9085065171e7177ee45 Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Fri, 19 Nov 2021 13:21:45 -0500 Subject: [PATCH] RunCommandLogs: add helpers for if it is running/failed/signaled --- src/lib/Hydra/Schema/Result/RunCommandLogs.pm | 41 +++++++++++++++++++ t/Schema/Result/RunCommandLogs.t | 14 ++++++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/lib/Hydra/Schema/Result/RunCommandLogs.pm b/src/lib/Hydra/Schema/Result/RunCommandLogs.pm index 00bfad76..54e1ac1d 100644 --- a/src/lib/Hydra/Schema/Result/RunCommandLogs.pm +++ b/src/lib/Hydra/Schema/Result/RunCommandLogs.pm @@ -257,4 +257,45 @@ sub did_succeed { return $self->exit_code == 0; } + +=head2 is_running + +Looks in the database to see if the task has been marked as completed. +Does not actually examine to see if the process is running anywhere. + +Return: + +* true if the task does not have a marked end date +* false if the task does have a recorded end +=cut +sub is_running { + my ($self) = @_; + + return !defined($self->end_time); +} + +=head2 did_fail_with_signal + +Looks in the database to see if the task failed with a signal. + +Return: + +* true if the task is not running and failed with a signal. +* false if the task is running or exited with an exit code. +=cut +sub did_fail_with_signal { + my ($self) = @_; + + if ($self->is_running()) { + return 0; + } + + if ($self->did_succeed()) { + return 0; + } + + return defined($self->signal); +} + + 1; diff --git a/t/Schema/Result/RunCommandLogs.t b/t/Schema/Result/RunCommandLogs.t index a0823e4c..4953278f 100644 --- a/t/Schema/Result/RunCommandLogs.t +++ b/t/Schema/Result/RunCommandLogs.t @@ -54,6 +54,8 @@ subtest "Starting a process" => sub { my $runlog = new_run_log(); $runlog->started(); is($runlog->did_succeed(), undef, "The process has not yet succeeded."); + ok($runlog->is_running(), "The process is running."); + ok(!$runlog->did_fail_with_signal(), "The process was not killed by a signal."); is($runlog->start_time, within(time() - 1, 2), "The start time is recent."); is($runlog->end_time, undef, "The end time is undefined."); is($runlog->exit_code, undef, "The exit code is undefined."); @@ -66,6 +68,8 @@ subtest "The process completed (success)" => sub { $runlog->started(); $runlog->completed_with_child_error(0, 123); ok($runlog->did_succeed(), "The process did succeed."); + ok(!$runlog->is_running(), "The process is not running."); + ok(!$runlog->did_fail_with_signal(), "The process was not killed by a signal."); is($runlog->start_time, within(time() - 1, 2), "The start time is recent."); is($runlog->end_time, within(time() - 1, 2), "The end time is recent."); is($runlog->error_number, undef, "The error number is undefined"); @@ -79,6 +83,8 @@ subtest "The process completed (errored)" => sub { $runlog->started(); $runlog->completed_with_child_error(21760, 123); ok(!$runlog->did_succeed(), "The process did not succeed."); + ok(!$runlog->is_running(), "The process is not running."); + ok(!$runlog->did_fail_with_signal(), "The process was not killed by a signal."); is($runlog->start_time, within(time() - 1, 2), "The start time is recent."); is($runlog->end_time, within(time() - 1, 2), "The end time is recent."); is($runlog->error_number, undef, "The error number is undefined"); @@ -92,12 +98,14 @@ subtest "The process completed (status 15, child error 0)" => sub { $runlog->started(); $runlog->completed_with_child_error(15, 0); ok(!$runlog->did_succeed(), "The process did not succeed."); + ok(!$runlog->is_running(), "The process is not running."); + ok($runlog->did_fail_with_signal(), "The process was killed by a signal."); is($runlog->start_time, within(time() - 1, 2), "The start time is recent."); is($runlog->end_time, within(time() - 1, 2), "The end time is recent."); is($runlog->error_number, undef, "The error number is undefined"); is($runlog->exit_code, undef, "The exit code is undefined."); is($runlog->signal, 15, "Signal 15 was sent."); - is($runlog->core_dumped, 1, "There was no core dump."); + is($runlog->core_dumped, 0, "There was no core dump."); }; subtest "The process completed (signaled)" => sub { @@ -105,6 +113,8 @@ subtest "The process completed (signaled)" => sub { $runlog->started(); $runlog->completed_with_child_error(393, 234); ok(!$runlog->did_succeed(), "The process did not succeed."); + ok(!$runlog->is_running(), "The process is not running."); + ok($runlog->did_fail_with_signal(), "The process was killed by a signal."); is($runlog->start_time, within(time() - 1, 2), "The start time is recent."); is($runlog->end_time, within(time() - 1, 2), "The end time is recent."); is($runlog->error_number, undef, "The error number is undefined"); @@ -118,6 +128,8 @@ subtest "The process failed to start" => sub { $runlog->started(); $runlog->completed_with_child_error(-1, 2); ok(!$runlog->did_succeed(), "The process did not succeed."); + ok(!$runlog->is_running(), "The process is running."); + ok(!$runlog->did_fail_with_signal(), "The process was not killed by a signal."); is($runlog->start_time, within(time() - 1, 2), "The start time is recent."); is($runlog->end_time, within(time() - 1, 2), "The end time is recent."); is($runlog->error_number, 2, "The error number is saved");