Skip to content

Commit 32e0ca1

Browse files
committed
Improve resource command and info file.
Fixes #256 Fixes #257 info files now contain the PID. yath resources will look for the newest pfile and/or info_file and use the most recent. Less noisy on failure to delete file.
1 parent 409652f commit 32e0ca1

File tree

6 files changed

+80
-32
lines changed

6 files changed

+80
-32
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ t/integration/test-broken-symlinks/broken-symlink.tx
3030
coverage.json
3131
lastlog.*
3232
*.test_info.json
33+
*.test_info.*.json

Changes

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{{$NEXT}}
22

3+
- Further improve resource command and file
4+
35
1.000131 2022-09-06 15:53:55-07:00 America/Los_Angeles
46

57
- Use relative file paths for job slot display

lib/App/Yath/Command/resources.pm

+48-21
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ our $VERSION = '1.000132';
66

77
use Term::Table();
88
use File::Spec();
9+
use Time::HiRes qw/sleep/;
910

1011
use App::Yath::Util qw/find_pfile/;
1112

@@ -29,26 +30,47 @@ A look at the state and resources used by a runner.
2930

3031
sub pfile_params { (no_fatal => 1) }
3132

33+
sub newest {
34+
my ($a, $b) = @_;
35+
return $a unless $b;
36+
return $b unless $a;
37+
38+
my @as = stat($a);
39+
my @bs = stat($b);
40+
return $a if $as[9] > $bs[9];
41+
return $b;
42+
}
43+
3244
sub state {
3345
my $self = shift;
3446

3547
return $self->{+STATE} if $self->{+STATE};
3648

37-
if (-e './.test_info.json') {
38-
my $data = Test2::Harness::Util::File::JSON->new(name => './.test_info.json')->read;
39-
return $self->{+STATE} = Test2::Harness::Runner::State->new(%$data);
49+
my $info_file;
50+
opendir(my $dh, "./") or die "Could not open current dir: $!";
51+
for my $file (readdir($dh)) {
52+
next unless $file =~ m{^\.test_info\.\S+\.json$};
53+
$info_file = newest($info_file, "./$file");
4054
}
4155

42-
if (my $pfile = find_pfile($self->settings, no_fatal => 1)) {
43-
my $data = Test2::Harness::Util::File::JSON->new(name => $pfile)->read();
44-
my $workdir = $data->{dir};
45-
my $settings = Test2::Harness::Util::File::JSON->new(name => "$workdir/settings.json")->read();
56+
my $pfile = find_pfile($self->settings, no_fatal => 1);
4657

47-
return $self->{+STATE} = Test2::Harness::Runner::State->new(
48-
job_count => $settings->{runner}->{job_count} // 1,
49-
workdir => $data->{dir},
50-
settings => $settings,
51-
);
58+
if (my $use = newest($info_file, $pfile)) {
59+
if ($info_file) {
60+
my $data = Test2::Harness::Util::File::JSON->new(name => $info_file)->read;
61+
return $self->{+STATE} = Test2::Harness::Runner::State->new(%$data);
62+
}
63+
64+
if (my $pfile = find_pfile($self->settings, no_fatal => 1)) {
65+
my $data = Test2::Harness::Util::File::JSON->new(name => $pfile)->read();
66+
my $workdir = $data->{dir};
67+
my $settings = Test2::Harness::Util::File::JSON->new(name => "$workdir/settings.json")->read();
68+
69+
return $self->{+STATE} = Test2::Harness::Runner::State->new(
70+
job_count => $settings->{runner}->{job_count} // 1,
71+
workdir => $data->{dir},
72+
);
73+
}
5274
}
5375

5476
die "No persistent runner, and no running test found.\n";
@@ -58,16 +80,21 @@ sub run {
5880
my $self = shift;
5981

6082
my $state = $self->state;
61-
$state->poll;
62-
63-
print "\n==== Resource state ====\n";
64-
for my $resource (@{$state->resources}) {
65-
my @lines = $resource->status_lines;
66-
next unless @lines;
67-
print "\nResource: " . ref($resource) . "\n";
68-
print join "\n" => @lines;
83+
84+
while (1) {
85+
$state->poll;
86+
87+
print "\r\e[2J\r\e[1;1H";
88+
print "\n==== Resource state ====\n";
89+
for my $resource (@{$state->resources}) {
90+
my @lines = $resource->status_lines;
91+
next unless @lines;
92+
print "\nResource: " . ref($resource) . "\n";
93+
print join "\n" => @lines;
94+
}
95+
print "\n\n";
96+
sleep 0.1;
6997
}
70-
print "\n\n";
7198

7299
return 0;
73100
}

lib/App/Yath/Command/run.pm

+4
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ sub run {
7979
return $self->SUPER::run(@_);
8080
}
8181

82+
sub write_test_info {
83+
$ENV{TEST2_HARNESS_NO_WRITE_TEST_INFO} //= 1;
84+
}
85+
8286
sub check_reload_state {
8387
my $self = shift;
8488

lib/App/Yath/Command/start.pm

+2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ file over and over again.
105105
sub run {
106106
my $self = shift;
107107

108+
$ENV{TEST2_HARNESS_NO_WRITE_TEST_INFO} //= 1;
109+
108110
my $settings = $self->settings;
109111
my $dir = $settings->workspace->workdir;
110112

lib/App/Yath/Command/test.pm

+23-11
Original file line numberDiff line numberDiff line change
@@ -255,24 +255,36 @@ sub DESTROY {
255255
}
256256
}
257257

258+
sub write_test_info {
259+
my $self = shift;
260+
261+
return if $ENV{TEST2_HARNESS_NO_WRITE_TEST_INFO};
262+
263+
my $info_file = "./.test_info.$$.json";
264+
265+
my $workdir = $self->workdir;
266+
Test2::Harness::Util::File::JSON->new(name => $info_file)->write({
267+
workdir => $self->workdir,
268+
job_count => $self->job_count,
269+
});
270+
271+
push @{$self->{+CLEANUP_SUBS}} => sub {
272+
return unless -e $info_file;
273+
return unless Test2::Harness::Util::File::JSON->new(name => $info_file)->read->{workdir} eq $workdir;
274+
unlink($info_file) or die "Could not unlink info file: $!";
275+
};
276+
277+
$ENV{TEST2_HARNESS_NO_WRITE_TEST_INFO} = 1;
278+
}
279+
258280
sub start {
259281
my $self = shift;
260282

261283
$self->ipc->start();
262284
$self->parse_args;
263285
$self->write_settings_to($self->workdir, 'settings.json');
264286

265-
unless ($ENV{TEST2_HARNESS_NO_WRITE_TEST_INFO}) {
266-
Test2::Harness::Util::File::JSON->new(name => './.test_info.json')->write({
267-
workdir => $self->workdir,
268-
job_count => $self->job_count,
269-
});
270-
271-
push @{$self->{+CLEANUP_SUBS}} => sub { unlink('./.test_info.json') or warn "Could not unlink: $!" };
272-
273-
$ENV{TEST2_HARNESS_NO_WRITE_TEST_INFO} = 1;
274-
}
275-
287+
$self->write_test_info();
276288
my $pop = $self->populate_queue();
277289
$self->terminate_queue();
278290

0 commit comments

Comments
 (0)