Skip to content

Commit 3f0877d

Browse files
authored
Merge pull request #1215 from matsduf/redefine-batch_status-api
Redefines batch_status to replace get_batch_job_result
2 parents 2b0c4a0 + 4406fb2 commit 3f0877d

File tree

5 files changed

+207
-15
lines changed

5 files changed

+207
-15
lines changed

lib/Zonemaster/Backend/DB.pm

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,60 @@ sub get_batch_job_result {
765765
return \%result;
766766
}
767767

768+
=head2 batch_status
769+
770+
Returns number of tests per category (finished, running, waiting) for the given
771+
batch, provided as C<batch_id>.
772+
773+
If one or more of parameters C<list_running_tests>, C<list_finished_tests> or
774+
C<list_waiting_tests> are included with true value, the C<hash_id> values for
775+
that category is also included.
776+
777+
=cut
778+
779+
# Standard SQL, can be here
780+
sub batch_status {
781+
my ( $self, $test_params ) = @_;
782+
783+
my $batch_id = $test_params->{batch_id};
784+
785+
die Zonemaster::Backend::Error::ResourceNotFound->new( message => "Unknown batch", data => { batch_id => $batch_id } )
786+
unless defined $self->batch_exists_in_db( $batch_id );
787+
788+
my $dbh = $self->dbh;
789+
790+
my %result;
791+
$result{waiting_count} = 0;
792+
$result{running_count} = 0;
793+
$result{finished_count} = 0;
794+
795+
my $query = "
796+
SELECT hash_id, progress
797+
FROM test_results
798+
WHERE batch_id=?";
799+
800+
my $sth1 = $dbh->prepare( $query );
801+
$sth1->execute( $batch_id );
802+
803+
while ( my $h = $sth1->fetchrow_hashref ) {
804+
if ( $h->{progress} eq '0' ) {
805+
$result{waiting_count}++;
806+
push(@{$result{waiting_tests}}, $h->{hash_id}) if $test_params->{list_waiting_tests};
807+
}
808+
elsif ( $h->{progress} eq '100' ) {
809+
$result{finished_count}++;
810+
push(@{$result{finished_tests}}, $h->{hash_id}) if $test_params->{list_finished_tests};
811+
}
812+
else {
813+
$result{running_count}++;
814+
push(@{$result{running_tests}}, $h->{hash_id}) if $test_params->{list_running_tests};
815+
}
816+
}
817+
818+
return \%result;
819+
}
820+
821+
768822
=head2 process_unfinished_tests($queue_label, $test_run_timeout)
769823
770824
Append a new log entry C<BACKEND_TEST_AGENT:UNABLE_TO_FINISH_TEST> to all the

lib/Zonemaster/Backend/RPCAPI.pm

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -737,9 +737,11 @@ sub batch_create {
737737
return $result;
738738
}
739739

740+
# Deprecated to be removed in v2025.2.
740741
$json_schemas{get_batch_job_result} = joi->object->strict->props(
741742
batch_id => $zm_validator->batch_id->required
742743
);
744+
# Deprecated to be removed in v2025.2.
743745
sub get_batch_job_result {
744746
my ( $self, $params ) = @_;
745747

@@ -756,15 +758,29 @@ sub get_batch_job_result {
756758
return $result;
757759
}
758760

759-
# Experimental
760-
$json_schemas{batch_status} = $json_schemas{get_batch_job_result};
761+
762+
$json_schemas{batch_status} = {
763+
type => 'object',
764+
additionalProperties => 0,
765+
required => [ 'batch_id' ],
766+
properties => {
767+
batch_id => $zm_validator->batch_id->required,
768+
list_waiting_tests => joi->boolean->compile,
769+
list_running_tests => joi->boolean->compile,
770+
list_finished_tests => joi->boolean->compile,
771+
}
772+
};
761773
sub batch_status {
762-
my $result = get_batch_job_result( @_ );
763-
return {
764-
running_count => $result->{nb_running},
765-
finished_count => $result->{nb_finished},
766-
finished_job_ids => $result->{finished_job_ids},
774+
my ( $self, $params ) = @_;
775+
776+
my $result;
777+
eval {
778+
$result = $self->{db}->batch_status($params);
767779
};
780+
if ($@) {
781+
handle_exception( $@ );
782+
}
783+
return $result;
768784
}
769785

770786
sub _get_locale {

script/zmb

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,8 +584,7 @@ sub cmd_add_batch_job {
584584
);
585585
}
586586

587-
588-
=head2 get_batch_job_result
587+
=head2 get_batch_job_result (** Deprecated to be removed in Zonemaster version v2025.2. Use C<batch_status> instead. **)
589588
590589
zmb [GLOBAL OPTIONS] get_batch_job_result [OPTIONS]
591590
@@ -613,6 +612,80 @@ sub cmd_get_batch_job_result {
613612
}
614613

615614

615+
=head2 batch_status
616+
617+
zmb [GLOBAL OPTIONS] batch_status [OPTIONS]
618+
619+
Options:
620+
--batch-id BATCH-ID|--bi BATCH-ID
621+
--list-waiting-tests true|false|null
622+
--list-running-tests true|false|null
623+
--list-finished-tests true|false|null
624+
625+
--lw # Same as "--list-waiting-tests true"
626+
--lr # Same as "--list-running-tests true"
627+
--lf # Same as "--list-finished-tests true"
628+
629+
"--batch-id" is mandatory.
630+
631+
The command provides the number of tests waiting to be run, tests running and
632+
test finished, respectively, for the batch.
633+
634+
"--list-waiting-tests", "--list-running-tests" and "--list-finished-tests" are
635+
optional. If given the test IDs of tests waiting to be run, tests running
636+
and test finished, respectively, are listed.
637+
638+
"--lw", "--lr" and "--lf" are option.
639+
640+
"--lw" must not be combined with "--list-waiting-tests". "--lr" must not be
641+
combined with "--list-running-tests". "--lf" must not be combined with
642+
"--list-finished-tests".
643+
=cut
644+
645+
sub cmd_batch_status {
646+
my @opts = @_;
647+
648+
my $opt_batch_id;
649+
my $opt_list_waiting_tests;
650+
my $opt_lw;
651+
my $opt_list_running_tests;
652+
my $opt_lr;
653+
my $opt_list_finished_tests;
654+
my $opt_lf;
655+
656+
GetOptionsFromArray(
657+
\@opts,
658+
'batch-id|bi=s' => \$opt_batch_id,
659+
'list-waiting-tests=s' => \$opt_list_waiting_tests,
660+
'lw' => \$opt_lw,
661+
'list-running-tests=s' => \$opt_list_running_tests,
662+
'lr' => \$opt_lr,
663+
'list-finished-tests=s' => \$opt_list_finished_tests,
664+
'lf' => \$opt_lf,
665+
) or pod2usage( 2 );
666+
667+
pod2usage( "'--lw' and '--list-waiting-test' must not be combined" ) if defined $opt_list_waiting_tests and $opt_lw;
668+
pod2usage( "'--lr' and '--list-running-test' must not be combined" ) if defined $opt_list_running_tests and $opt_lr;
669+
pod2usage( "'--lf' and '--list-finished-test' must not be combined" ) if defined $opt_list_finished_tests and $opt_lf;
670+
671+
my %params;
672+
$params{batch_id} = $opt_batch_id;
673+
$params{list_waiting_tests} = json_tern( $opt_list_waiting_tests ) if $opt_list_waiting_tests and json_tern( $opt_list_waiting_tests );
674+
$params{list_running_tests} = json_tern( $opt_list_running_tests ) if $opt_list_running_tests and json_tern( $opt_list_running_tests );
675+
$params{list_finished_tests} = json_tern( $opt_list_finished_tests ) if $opt_list_finished_tests and json_tern( $opt_list_finished_tests );
676+
677+
$params{list_waiting_tests} = JSON::PP::true if $opt_lw;
678+
$params{list_running_tests} = JSON::PP::true if $opt_lr;
679+
$params{list_finished_tests} = JSON::PP::true if $opt_lf;
680+
681+
return to_jsonrpc(
682+
id => 1,
683+
method => 'batch_status',
684+
params => \%params,
685+
);
686+
}
687+
688+
616689
sub show_commands {
617690
my %specials = (
618691
man => 'Show the full manual page.',
@@ -643,8 +716,10 @@ sub get_commands {
643716
grep { $_ =~ /^cmd_/ } grep { defined &{"main\::$_"} } keys %{"main\::"};
644717
}
645718

719+
646720
sub json_tern {
647721
my $value = shift;
722+
648723
if ( $value eq 'true' ) {
649724
return JSON::PP::true;
650725
}

script/zonemaster_backend_rpcapi.psgi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,12 @@ my $router = router {
169169
action => "domain_history"
170170
};
171171

172+
# Deprecated to be removed v2025.2
172173
connect "get_batch_job_result" => {
173174
handler => $handler,
174175
action => "get_batch_job_result"
175176
};
176177

177-
# Experimental
178178
connect "batch_status" => {
179179
handler => $handler,
180180
action => "batch_status"

t/batches.t

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use POSIX qw( strftime );
88
use Time::Local qw( timelocal_modern );
99
use Test::Exception;
1010
use Test::More; # see done_testing()
11+
use Test::Differences;
1112

1213
my $t_path;
1314
BEGIN {
@@ -150,7 +151,8 @@ subtest 'RPCAPI add_batch_job' => sub {
150151
};
151152
};
152153

153-
subtest 'RPCAPI get_batch_job_result' => sub {
154+
subtest 'RPCAPI get_batch_job_result and batch_status' => sub {
155+
# "get_batch_job_result deprecated to be removed by v2025.2
154156
my $config = Zonemaster::Backend::Config->parse( $config );
155157
my $rpcapi = init_backend( $config );
156158
subtest 'batch job exists' => sub {
@@ -166,13 +168,26 @@ subtest 'RPCAPI get_batch_job_result' => sub {
166168

167169
is( $batch_id, 1, 'correct batch job id returned' );
168170

169-
my $res = $rpcapi->get_batch_job_result( { batch_id => $batch_id } );
171+
{
172+
my $res = $rpcapi->get_batch_job_result( { batch_id => $batch_id } );
173+
174+
is( $res->{nb_running}, scalar @domains, 'correct number of runninng tests' );
175+
is( $res->{nb_finished}, 0, 'correct number of finished tests' );
176+
}
170177

171-
is( $res->{nb_running}, @domains, 'correct number of runninng tests' );
172-
is( $res->{nb_finished}, 0, 'correct number of finished tests' );
178+
{
179+
my $res = $rpcapi->batch_status( { batch_id => $batch_id } );
180+
181+
is( $res->{waiting_count}, scalar @domains, 'correct number of runninng tests' );
182+
is( $res->{running_count}, 0, 'correct number of finished tests' );
183+
is( $res->{finished_count}, 0, 'correct number of finished tests' );
184+
ok( !exists $res->{waiting_tests}, 'list of waiting tests expected to be absent' );
185+
ok( !exists $res->{running_tests}, 'list of running tests expected to be absent' );
186+
ok( !exists $res->{finished_tests}, 'list of finished tests to be absent' );
187+
}
173188
};
174189

175-
subtest 'unknown batch' => sub {
190+
subtest 'unknown batch (get_batch_job_result)' => sub {
176191
my $unknown_batch = 10;
177192
dies_ok {
178193
$rpcapi->get_batch_job_result( { batch_id => $unknown_batch } );
@@ -182,6 +197,17 @@ subtest 'RPCAPI get_batch_job_result' => sub {
182197
is( $res->{message}, 'Unknown batch', 'correct error message' );
183198
is( $res->{data}->{batch_id}, $unknown_batch, 'correct data type returned' );
184199
};
200+
201+
subtest 'unknown batch (batch_status)' => sub {
202+
my $unknown_batch = 10;
203+
dies_ok {
204+
$rpcapi->batch_status( { batch_id => $unknown_batch } );
205+
} 'getting results for an unknown batch_id should die';
206+
my $res = $@;
207+
is( $res->{error}, 'Zonemaster::Backend::Error::ResourceNotFound', 'correct error type' );
208+
is( $res->{message}, 'Unknown batch', 'correct error message' );
209+
is( $res->{data}->{batch_id}, $unknown_batch, 'correct data type returned' );
210+
};
185211
};
186212

187213
subtest 'batch with several domains' => sub {
@@ -201,11 +227,32 @@ subtest 'batch with several domains' => sub {
201227

202228
is( $res, 1, 'correct batch job id returned' );
203229

230+
# "get_batch_job_result deprecated to be removed by v2025.2
204231
$res = $rpcapi->get_batch_job_result( { batch_id => 1 } );
205232

206233
is( $res->{nb_running}, @domains, 'correct number of runninng tests' );
207234
is( $res->{nb_finished}, 0, 'correct number of finished tests' );
208235

236+
# No lists of test IDs requested
237+
$res = $rpcapi->batch_status( { batch_id => 1 } );
238+
239+
is( $res->{waiting_count}, scalar @domains, 'correct number of running tests' );
240+
is( $res->{running_count}, 0, 'correct number of finished tests' );
241+
is( $res->{finished_count}, 0, 'correct number of finished tests' );
242+
ok( !exists $res->{waiting_tests}, 'list of waiting tests expected to be absent' );
243+
ok( !exists $res->{running_tests}, 'list of running tests expected to be absent' );
244+
ok( !exists $res->{finished_tests}, 'list of finished tests expected to be absent' );
245+
246+
# List of waiting test IDs requested
247+
$res = $rpcapi->batch_status( { batch_id => 1, list_waiting_tests => 1 } );
248+
249+
is( $res->{waiting_count}, scalar @domains, 'correct number of runninng tests' );
250+
is( $res->{running_count}, 0, 'correct number of finished tests' );
251+
is( $res->{finished_count}, 0, 'correct number of finished tests' );
252+
is( scalar @{ $res->{waiting_tests} }, scalar @domains, 'correct number of elements in waiting_tests' );
253+
ok( !exists $res->{running_tests}, 'list of running tests expected to be absent' );
254+
ok( !exists $res->{finished_tests}, 'list of finished tests expected to be absent' );
255+
209256
subtest 'table "test_results" contains 2 entries' => sub {
210257
my ( $count ) = $dbh->selectrow_array( q[ SELECT count(*) FROM test_results ] );
211258
is( $count, @domains, 'two rows in table' );

0 commit comments

Comments
 (0)