lib/Convert/Pheno/HTTP/Jobs.pm

filter: Statementstmt 94.8% 492 / 519 Branchbran 70.9% 186 / 262 Conditioncond 55.7% 131 / 235 MC/DCmcdc 26.0% 31 / 119 Subroutinesub 96.3% 52 / 54 Podpod 0.0% 0 / 20 totaltotal 73.7% 892 / 1209 CC 234 SCAR 33.8
CC
234
Cov
80%
delete_files
101.9
_recover_publication
42.1
perform
33.5
CRAP 29.3
« lib/Convert/Pheno/FHIR/Profile/MCode.pm ↑ Summary lib/Convert/Pheno/Source/CDISC/DatasetXML.pm »
1 package Convert::Pheno::HTTP::Jobs;
2  
3 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 32 ▶use Cwd qw(abs_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 32 ▶use Digest::SHA qw(sha256_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 32 ▶use Encode qw(decode FB_DEFAULT);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 32 ▶use File::Copy qw(copy);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 32 ▶use File::Find qw(find);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 32 ▶use File::Path qw(make_path remove_tree);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 32 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 32 ▶use Fcntl qw(LOCK_EX LOCK_SH LOCK_NB);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 32 ▶use IPC::Open3 qw(open3);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 32 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 32 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 32 ▶use Mojo::Util qw(sha1_sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 32 ▶use Path::Tiny qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 32 ▶use POSIX qw(WNOHANG);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 32 ▶use Symbol qw(gensym);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 32 ▶use Convert::Pheno::HTTP::Service qw(execute execute_files catalog);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 32 ▶use Convert::Pheno::IO::Atomic qw(write_atomically);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 32 my $JSON = JSON::XS->new->utf8->canonical->pretty;
24 32 my $MAX_CONCURRENT_JOBS = 16;
25  
26 80 ▶sub _validate_job_limit {
Subroutine _validate_job_limit: called CC=6 SCAR=17.9
27 80 my ($value, $maximum) = @_;
28 80 ▶ $maximum //= $MAX_CONCURRENT_JOBS;
Condition66%2/3
$maximum //= $MAX_CONCURRENT_JOBS
l!l&&r!l&&!r
18620
MC/DC0%0/2
$maximum //= $MAX_CONCURRENT_JOBS
$maximum $MAX_CONCURRENT_JOBS
Truth table66%2/3
A //= B
A= $maximumB= $MAX_CONCURRENT_JOBS
ABresult
1X1
011
000
29 80 ▶ die "Maximum concurrent jobs must be an integer from 1 to $maximum\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value and "$value" =~ /\A[1-9][0-9]*\z/ and $value <= $maximum 10 70 80
Condition100%9/9
defined $value and not ref $value and "$value" =~ /\A[1-9][0-9]*\z/ and $value <= $maximum
!ll&&!rl&&r
7370
defined $value and not ref $value and "$value" =~ /\A[1-9][0-9]*\z/
!ll&&!rl&&r
3473
defined $value and not ref $value
!ll&&!rl&&r
1277
MC/DC100%4/4
defined $value and not ref $value and "$value" =~ /\A[1-9][0-9]*\z/ and $value <= $maximum
defined $value not ref $value "$value" =~ /\A[1-9][0-9]*\z/ $value <= $maximum
Truth table100%5/5
A and not(B) and C and D
A= defined $valueB= not ref $valueC= "$value" =~ /\A[1-9][0-9]*\z/D= $value <= $maximum
ABCDresult
0XXX0
10XX0
110X0
11100
11111
30 unless defined($value) && !ref($value) && "$value" =~ /\A[1-9][0-9]*\z/
31 && $value <= $maximum;
32 70 return 0 + $value;
33 }
34  
35 1856 ▶sub _metadata_lock {
Subroutine _metadata_lock: called CC=3 SCAR=11.3
36 1856 my ($file, $mode) = @_;
37 # Lock a stable sibling, not the JSON inode replaced by atomic writes.
38 # Windows readers must not overlap replacement (including its backup gap).
39 # Keep this file in place: unlinking it could create two independent locks.
40 1856 my $lockfile = path($file)->parent->child('.metadata.lock');
41 1856 ▶ open my $lock, '>>', $lockfile
Branch: 1 branch
BranchTrueFalseTotal
open my $lock, ">>", $lockfile or die "Cannot open job metadata lock <$lockfile>: $!\n" 1856 0 1856
42 or die "Cannot open job metadata lock <$lockfile>: $!\n";
43 1856 ▶ flock($lock, $mode)
Branch: 1 branch
BranchTrueFalseTotal
flock $lock, $mode or die "Cannot lock job metadata <$lockfile>: $!\n" 1856 0 1856
44 or die "Cannot lock job metadata <$lockfile>: $!\n";
45 1856 return $lock;
46 }
47  
48 169 ▶sub _write {
Subroutine _write: called CC=1 SCAR=0.0
49 169 my ($file, $data) = @_;
50 169 my $lock = _metadata_lock($file, LOCK_EX);
51 169 ▶ write_atomically($file, sub { path($_[0])->spew_raw($JSON->encode($data)) });
Subroutine __ANON__: called CC=1 SCAR=0.0
52 }
53 819 ▶sub _read {
Subroutine _read: called CC=1 SCAR=0.0
54 819 my ($file) = @_;
55 819 my $lock = _metadata_lock($file, LOCK_SH);
56 819 return $JSON->decode(path($file)->slurp_raw);
57 }
58 105 ▶sub _id { sha1_sum(join ':', $$, rand(), time(), {}) }
Subroutine _id: called CC=1 SCAR=0.0
59 668 ▶sub _resolved_path {
Subroutine _resolved_path: called CC=4 SCAR=17.9
60 668 my ($value) = @_;
61 668 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 668 668
62 668 $value = "$value";
63 668 my $resolved = abs_path($value);
64 668 ▶ if (!defined $resolved) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $resolved) 0 668 668
65 0 my $candidate = path($value);
66 0 my $parent = abs_path("@{[$candidate->parent]}");
67 0 ▶ $resolved = defined($parent)
Branch: 1 branch
BranchTrueFalseTotal
defined $parent ? : 0 0 0
68 ? File::Spec->catfile($parent, $candidate->basename)
69 : File::Spec->rel2abs($value);
70 }
71 668 return File::Spec->canonpath($resolved);
72 }
73 518 ▶sub _path_key {
Subroutine _path_key: called CC=3 SCAR=12.3
74 518 my ($value) = @_;
75 518 my $key = _resolved_path($value);
76 518 ▶ return unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 518 518
77 518 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 518 518
78 0 $key =~ tr{\\}{/};
79 0 $key = lc $key;
80 }
81 518 return $key;
82 }
83 253 ▶sub _same_path {
Subroutine _same_path: called CC=3 SCAR=12.5
84 253 my ($left, $right) = @_;
85 253 ▶ return defined($left) && defined($right) && _path_key($left) eq _path_key($right);
Condition50%3/6
defined $left && defined $right && _path_key($left) eq _path_key($right)
!ll&&!rl&&r
024229
defined $left and defined $right
!ll&&!rl&&r
00253
MC/DC33%1/3
defined $left && defined $right && _path_key($left) eq _path_key($right)
defined $left defined $right _path_key($left) eq _path_key($right)
Truth table50%2/4
A and B && C
A= defined $leftB= defined $rightC= _path_key($left) eq _path_key($right)
ABCresult
0XX0
10X0
1100
1111
86 }
87 41 ▶sub _worker_lock {
Subroutine _worker_lock: called CC=3 SCAR=11.0
88 41 my ($dir) = @_;
89 41 ▶ open my $fh, '>>', path($dir, '.worker.lock') or die "Cannot lock run folder\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">>", path($dir, ".worker.lock") or die "Cannot lock run folder\n" 41 0 41
90 41 ▶ flock($fh, LOCK_EX | LOCK_NB) or die "A previous conversion is still finishing; wait before reopening this workspace\n";
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 6 or die "A previous conversion is still finishing; wait before reopening this workspace\n" 40 1 41
91 40 return $fh;
92 }
93  
94 66 ▶sub _recover_publication {
Subroutine _recover_publication: called CC=29 SCAR=37.4
95 66 my ($dir, $status) = @_;
96 66 my $journal = path($dir, 'publication.json');
97 66 ▶ return $status unless -f $journal;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $journal 41 25 66
98 25 my $publication = _read($journal);
99 25 my $final = $status->{outputDirectory};
100 25 my $default = path($dir, 'outputs');
101 25 my $is_default = _same_path($final, $default);
102 die "Invalid publication record\n" unless defined($final)
103 && _same_path($publication->{final}, $final)
104 25 ▶ && (path($final)->basename eq 'convert-pheno-'.$status->{id} || $is_default);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $final and _same_path($publication->{"final"}, $final) and path($final)->basename eq 'convert-pheno-' . $status->{"id"} || $is_default 0 25 25
Condition44%4/9
defined $final and _same_path($publication->{"final"}, $final) and path($final)->basename eq 'convert-pheno-' . $status->{"id"} || $is_default
!ll&&!rl&&r
0025
defined $final and _same_path($publication->{"final"}, $final)
!ll&&!rl&&r
0025
path($final)->basename eq 'convert-pheno-' . $status->{"id"} or $is_default
l!l&&r!l&&!r
1960
MC/DC0%0/4
defined $final and _same_path($publication->{"final"}, $final) and path($final)->basename eq 'convert-pheno-' . $status->{"id"} || $is_default
defined $final _same_path($publication->{"final"}, $final) path($final)->basename eq 'convert-pheno-' . $status->{"id"} $is_default
Truth table40%2/5
A and B and C or D
A= defined $finalB= _same_path($publication->{"final"}, $final)C= path($final)->basename eq 'convert-pheno-' . $status->{"id"}D= $is_default
ABCDresult
0XXX0
10XX0
11000
111X1
11011
105 my $staging = $is_default ? path($dir,'staging')
106 25 ▶ : path($final)->parent->child('.convert-pheno-'.$status->{id});
Branch: 1 branch
BranchTrueFalseTotal
$is_default ? : 6 19 25
107 25 ▶ die "Invalid export staging record\n" unless _same_path($publication->{staging}, $staging);
Branch: 1 branch
BranchTrueFalseTotal
unless _same_path($publication->{"staging"}, $staging) 0 25 25
108 # Ownership markers prevent cleanup from following a replaced directory.
109 # A final folder is published atomically only after every output was copied.
110 25 for my $folder ($final, "$staging") {
111 50 ▶ next unless -e $folder || -l $folder;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $folder or -l $folder 25 25 50
Condition66%2/3
-e $folder or -l $folder
l!l&&r!l&&!r
25025
MC/DC50%1/2
-e $folder or -l $folder
-e $folder -l $folder
Truth table66%2/3
A or B
A= -e $folderB= -l $folder
ABresult
1X1
011
000
112 25 my $marker = path($folder, '.convert-pheno-owner');
113 next if _same_path($folder, $final) && !-e $marker && !-l $marker && !-l $folder
114 && _same_path(abs_path($folder), $folder) && $status->{status} eq 'completed'
115 25 ▶ && _same_path($status->{directory}, $final);
Branch: 1 branch
BranchTrueFalseTotal
if _same_path($folder, $final) and not -e $marker and not -l $marker and not -l $folder and _same_path(abs_path($folder), $folder) and $status->{"status"} eq "completed" and _same_path($status->{"directory"}, $final) 1 24 25
Condition72%13/18
_same_path($folder, $final) and not -e $marker and not -l $marker and not -l $folder and _same_path(abs_path($folder), $folder) and $status->{"status"} eq "completed" and _same_path($status->{"directory"}, $final)
!ll&&!rl&&r
2401
_same_path($folder, $final) and not -e $marker and not -l $marker and not -l $folder and _same_path(abs_path($folder), $folder) and $status->{"status"} eq "completed"
!ll&&!rl&&r
2401
_same_path($folder, $final) and not -e $marker and not -l $marker and not -l $folder and _same_path(abs_path($folder), $folder)
!ll&&!rl&&r
2401
_same_path($folder, $final) and not -e $marker and not -l $marker and not -l $folder
!ll&&!rl&&r
2401
_same_path($folder, $final) and not -e $marker and not -l $marker
!ll&&!rl&&r
2401
_same_path($folder, $final) and not -e $marker
!ll&&!rl&&r
2221
MC/DC28%2/7
_same_path($folder, $final) and not -e $marker and not -l $marker and not -l $folder and _same_path(abs_path($folder), $folder) and $status->{"status"} eq "completed" and _same_path($status->{"directory"}, $final)
_same_path($folder, $final) not -e $marker not -l $marker not -l $folder _same_path(abs_path($folder), $folder) $status->{"status"} eq "completed" _same_path($status->{"directory"}, $final)
Truth table37%3/8
A and not(B) and not(C) and not(D) and E and F and G
A= _same_path($folder, $final)B= not -e $markerC= not -l $markerD= not -l $folderE= _same_path(abs_path($folder), $folder)F= $status->{"status"} eq "completed"G= _same_path($status->{"directory"}, $final)
ABCDEFGresult
0XXXXXX0
10XXXXX0
110XXXX0
1110XXX0
11110XX0
111110X0
11111100
11111111
116 die "Export folder changed; automatic recovery was not performed\n"
117 if -l $folder || !_same_path(abs_path($folder), $folder) || !-f $marker || -l $marker
118 24 ▶ || $marker->slurp_raw ne $publication->{owner};
Branch: 1 branch
BranchTrueFalseTotal
if -l $folder or not _same_path(abs_path($folder), $folder) or not -f $marker or -l $marker or $marker->slurp_raw ne $publication->{"owner"} 1 23 24
Condition41%5/12
-l $folder or not _same_path(abs_path($folder), $folder) or not -f $marker or -l $marker or $marker->slurp_raw ne $publication->{"owner"}
l!l&&r!l&&!r
0123
-l $folder or not _same_path(abs_path($folder), $folder) or not -f $marker or -l $marker
l!l&&r!l&&!r
0024
-l $folder or not _same_path(abs_path($folder), $folder) or not -f $marker
l!l&&r!l&&!r
0024
-l $folder or not _same_path(abs_path($folder), $folder)
l!l&&r!l&&!r
0024
MC/DC20%1/5
-l $folder or not _same_path(abs_path($folder), $folder) or not -f $marker or -l $marker or $marker->slurp_raw ne $publication->{"owner"}
-l $folder not _same_path(abs_path($folder), $folder) not -f $marker -l $marker $marker->slurp_raw ne $publication->{"owner"}
Truth table33%2/6
A or not(B) or not(C) or D or E
A= -l $folderB= not _same_path(abs_path($folder), $folder)C= not -f $markerD= -l $markerE= $marker->slurp_raw ne $publication->{"owner"}
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
119 }
120 24 ▶ if (-d $final) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $final) 23 1 24
121 23 $status = $publication->{completed};
122 23 _write(path($dir,'status.json'), $status);
123 23 my $marker = path($final,'.convert-pheno-owner');
124 23 ▶ unlink $marker or die "Cannot clear publication marker\n" if -e $marker;
Branch: 1 branch
BranchTrueFalseTotal
if -e $marker 22 1 23
Condition60%3/5
-e $marker && (unlink($marker) || die("Cannot clear publication marker\n"))
!ll
1220
unlink $marker or die "Cannot clear publication marker\n"
l!l
220
MC/DC0%0/2
-e $marker && (unlink($marker) || die("Cannot clear publication marker\n"))
-e $marker unlink $marker
Truth table33%1/3
A && B
A= -e $markerB= unlink $marker
ABresult
0X0
100
111
125 }
126 24 ▶ if (-d $staging) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $staging) 1 23 24
127 1 remove_tree("$staging", {error => \my $errors});
128 1 ▶ die "Cannot clean incomplete export\n" if @$errors;
Branch: 1 branch
BranchTrueFalseTotal
if @$errors 0 1 1
129 }
130 24 ▶ unlink $journal or die "Cannot clear publication record\n";
Branch: 1 branch
BranchTrueFalseTotal
unlink $journal or die "Cannot clear publication record\n" 24 0 24
131 24 return $status;
132 }
133  
134 32 ▶sub new {
Subroutine new: called CC=15 SCAR=27.3
Pod undocumented
135 32 my ($class, %args) = @_;
136 32 ▶ die "Job storage and worker executable are required\n" unless $args{root} && $args{worker};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"root"} and $args{"worker"} 0 32 32
Condition33%1/3
$args{"root"} and $args{"worker"}
!ll&&!rl&&r
0032
MC/DC0%0/2
$args{"root"} and $args{"worker"}
$args{"root"} $args{"worker"}
Truth table33%1/3
A and B
A= $args{"root"}B= $args{"worker"}
ABresult
0X0
100
111
137 32 make_path($args{root}, { mode => 0700 });
138 32 my $self = bless { %args, root => _resolved_path($args{root}), queue => [], grants => {}, active => {} }, $class;
139 # Recovery may rewrite run state, so only one supervisor may own this store.
140 # Keep the handle open for its lifetime; do not unlink the lock file.
141 32 ▶ open my $lock, '>>', path($self->{root}, '.supervisor.lock') or die "Cannot lock job storage\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $lock, ">>", path($self->{"root"}, ".supervisor.lock") or die "Cannot lock job storage\n" 32 0 32
142 32 ▶ flock($lock, LOCK_EX | LOCK_NB) or die "This job storage is already in use by another Convert-Pheno service\n";
Branch: 1 branch
BranchTrueFalseTotal
flock $lock, 6 or die "This job storage is already in use by another Convert-Pheno service\n" 31 1 32
143 31 $self->{storage_lock} = $lock;
144 # Desktop supplies its platform-detected CPU limit. Standalone services
145 # default to one worker unless the operator explicitly configures a limit.
146 31 ▶ $self->{max_allowed_jobs} = _validate_job_limit($ENV{CONVERT_PHENO_JOB_LIMIT} // 1);
Condition100%2/2
$ENV{"CONVERT_PHENO_JOB_LIMIT"} // 1
l!l
823
MC/DC100%1/1
$ENV{"CONVERT_PHENO_JOB_LIMIT"} // 1
$ENV{"CONVERT_PHENO_JOB_LIMIT"}
Truth table100%2/2
A
A= $ENV{"CONVERT_PHENO_JOB_LIMIT"}
Aresult
11
00
147 31 my $settings_file = path($self->{root}, 'scheduler-settings.json');
148 31 ▶ my $settings = -f $settings_file ? _read($settings_file) : {maxConcurrentJobs => 1};
Branch: 1 branch
BranchTrueFalseTotal
-f $settings_file ? : 3 28 31
149 31 $self->{max_concurrent_jobs} = _validate_job_limit($settings->{maxConcurrentJobs});
150 $self->{max_concurrent_jobs} = $self->{max_allowed_jobs}
151 31 ▶ if $self->{max_concurrent_jobs} > $self->{max_allowed_jobs};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"max_concurrent_jobs"} > $self->{"max_allowed_jobs"} 2 29 31
152 31 my %worker_locks;
153 # Preflight all runs before changing any state. A surviving child still owns
154 # its run even if its former supervisor's store lock has been released.
155 31 for my $dir (path($self->{root})->children) {
156 57 ▶ next unless $dir->basename =~ /\A[a-f0-9]{40}\z/ && -f "$dir/status.json";
Branch: 1 branch
BranchTrueFalseTotal
unless $dir->basename =~ /\A[a-f0-9]{40}\z/ and -f "$dir/status.json" 36 21 57
Condition66%2/3
$dir->basename =~ /\A[a-f0-9]{40}\z/ and -f "$dir/status.json"
!ll&&!rl&&r
36021
MC/DC50%1/2
$dir->basename =~ /\A[a-f0-9]{40}\z/ and -f "$dir/status.json"
$dir->basename =~ /\A[a-f0-9]{40}\z/ -f "$dir/status.json"
Truth table66%2/3
A and B
A= $dir->basename =~ /\A[a-f0-9]{40}\z/B= -f "$dir/status.json"
ABresult
0X0
100
111
157 21 $worker_locks{"$dir"} = _worker_lock($dir);
158 }
159 30 for my $dir (path($self->{root})->children) {
160 55 ▶ next unless -f "$dir/status.json";
Branch: 1 branch
BranchTrueFalseTotal
unless -f "$dir/status.json" 35 20 55
161 20 ▶ my $status = eval { _read("$dir/status.json") } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $status = eval { do { _read("$dir/status.json") } } or next 20 0 20
162 20 $status = _recover_publication($dir, $status);
163 19 ▶ if ($status->{status} =~ /\A(?:queued|running|cancelling)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status->{"status"} =~ /\A(?:queued|running|cancelling)\z/) 2 17 19
164 2 $status->{status} = 'interrupted';
165 2 $status->{message} = 'The service stopped before this run completed';
166 2 $status->{finished} = time();
167 2 _write("$dir/status.json", $status);
168 2 remove_tree("$dir/staging");
169 }
170 19 unlink "$dir/request.json";
171 }
172 29 return $self;
173 }
174  
175 15 ▶sub settings {
Subroutine settings: called CC=1 SCAR=0.0
Pod undocumented
176 15 my ($self) = @_;
177 15 return {maxConcurrentJobs => $self->{max_concurrent_jobs}, maxAllowedConcurrentJobs => $self->{max_allowed_jobs}};
178 }
179  
180 19 ▶sub update_settings {
Subroutine update_settings: called CC=5 SCAR=16.3
Pod undocumented
181 19 my ($self, $settings) = @_;
182 19 ▶ die "The job service has stopped\n" if $self->{stopped};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stopped"} 0 19 19
183 die "Provide only maxConcurrentJobs\n" unless ref($settings) eq 'HASH'
184 19 ▶ && keys(%$settings) == 1 && exists $settings->{maxConcurrentJobs};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $settings eq "HASH" and keys %$settings == 1 and exists $settings->{"maxConcurrentJobs"} 1 18 19
Condition66%4/6
ref $settings eq "HASH" and keys %$settings == 1 and exists $settings->{"maxConcurrentJobs"}
!ll&&!rl&&r
1018
ref $settings eq "HASH" and keys %$settings == 1
!ll&&!rl&&r
0118
MC/DC33%1/3
ref $settings eq "HASH" and keys %$settings == 1 and exists $settings->{"maxConcurrentJobs"}
ref $settings eq "HASH" keys %$settings == 1 exists $settings->{"maxConcurrentJobs"}
Truth table50%2/4
A and B and C
A= ref $settings eq "HASH"B= keys %$settings == 1C= exists $settings->{"maxConcurrentJobs"}
ABCresult
0XX0
10X0
1100
1111
185 18 my $limit = _validate_job_limit($settings->{maxConcurrentJobs}, $self->{max_allowed_jobs});
186 8 _write(path($self->{root}, 'scheduler-settings.json'), {maxConcurrentJobs => $limit});
187 8 $self->{max_concurrent_jobs} = $limit;
188 8 $self->_next;
189 8 return $self->settings;
190 }
191  
192 52 ▶sub register_file {
Subroutine register_file: called CC=9 SCAR=24.8
Pod undocumented
193 52 my ($self, $file) = @_;
194 52 ▶ die "Select an existing file or directory\n" if !defined($file) || ref($file) || -l $file;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $file or ref $file or -l $file 0 52 52
Condition33%2/6
not defined $file or ref $file or -l $file
l!l&&r!l&&!r
0052
not defined $file or ref $file
l!l&&r!l&&!r
0052
MC/DC0%0/3
not defined $file or ref $file or -l $file
not defined $file ref $file -l $file
Truth table25%1/4
not(A) or B or C
A= not defined $fileB= ref $fileC= -l $file
ABCresult
1XX1
01X1
0011
0000
195 52 my $resolved = _resolved_path($file);
196 52 ▶ die "Selected location is unavailable\n" unless $resolved && (-f $resolved || -d $resolved);
Branch: 1 branch
BranchTrueFalseTotal
unless $resolved and -f $resolved || -d $resolved 0 52 52
Condition50%3/6
$resolved and -f $resolved || -d $resolved
!ll&&!rl&&r
0052
-f $resolved or -d $resolved
l!l&&r!l&&!r
39130
MC/DC0%0/3
$resolved and -f $resolved || -d $resolved
$resolved -f $resolved -d $resolved
Truth table50%2/4
A and B or C
A= $resolvedB= -f $resolvedC= -d $resolved
ABCresult
0XX0
1000
11X1
1011
197 52 my $id = _id();
198 52 $self->{grants}{$id} = $resolved;
199 52 ▶ return { id => $id, filename => path($resolved)->basename,
Branch: 2 branches
BranchTrueFalseTotal
-d $resolved ? : 13 39 52
-f $resolved ? : 39 13 52
200 directory => -d $resolved ? JSON::XS::true : JSON::XS::false,
201 bytes => -f $resolved ? -s $resolved : 0 };
202 }
203  
204 91 ▶sub resolve_grant {
Subroutine resolve_grant: called CC=7 SCAR=22.7
Pod undocumented
205 91 my ($self, $id) = @_;
206 die "Unknown or expired file selection; select the file again\n"
207 91 ▶ unless defined($id) && !ref($id) && exists $self->{grants}{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id and not ref $id and exists $self->{"grants"}{$id} 2 89 91
Condition50%3/6
defined $id and not ref $id and exists $self->{"grants"}{$id}
!ll&&!rl&&r
0289
defined $id and not ref $id
!ll&&!rl&&r
0091
MC/DC33%1/3
defined $id and not ref $id and exists $self->{"grants"}{$id}
defined $id not ref $id exists $self->{"grants"}{$id}
Truth table50%2/4
A and not(B) and C
A= defined $idB= not ref $idC= exists $self->{"grants"}{$id}
ABCresult
0XX0
10X0
1100
1111
208 89 my $file = $self->{grants}{$id};
209 89 ▶ die "Selected location changed or is no longer available\n"
Branch: 1 branch
BranchTrueFalseTotal
unless not -l $file and -e $file and _same_path(abs_path($file), $file) 0 89 89
Condition33%2/6
not -l $file and -e $file and _same_path(abs_path($file), $file)
!ll&&!rl&&r
0089
not -l $file and -e $file
!ll&&!rl&&r
0089
MC/DC0%0/3
not -l $file and -e $file and _same_path(abs_path($file), $file)
not -l $file -e $file _same_path(abs_path($file), $file)
Truth table25%1/4
not(A) and B and C
A= not -l $fileB= -e $fileC= _same_path(abs_path($file), $file)
ABCresult
0XX0
10X0
1100
1111
210 unless !-l $file && -e $file && _same_path(abs_path($file), $file);
211 89 return $file;
212 }
213  
214 32 ▶sub submit {
Subroutine submit: called CC=25 SCAR=34.9
Pod undocumented
215 32 my ($self, $request) = @_;
216 32 ▶ die "The job service has stopped\n" if $self->{stopped};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stopped"} 1 31 32
217 31 ▶ die "Request must be an object\n" unless ref($request) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $request eq "HASH" 0 31 31
218 31 my %allowed = map { $_ => 1 } qw(conversion input output options destination);
219 31 ▶ die "Unknown job field\n" if grep { !$allowed{$_} } keys %$request;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not $allowed{$_};} keys %$request 0 31 31
220 31 ▶ die "The conversion queue is full\n" if @{$self->{queue}} >= 16;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"queue"};} >= 16 0 31 31
221 31 my $conversion = $request->{conversion};
222 31 ▶ my ($route) = grep { defined($conversion) && !ref($conversion) && $_->{id} eq $conversion } @{catalog()->{data}};
Condition50%3/6
defined $conversion && !ref($conversion) && $_->{'id'} eq $conversion
!ll&&!rl&&r
0133331
defined $conversion and not ref $conversion
!ll&&!rl&&r
001364
MC/DC33%1/3
defined $conversion && !ref($conversion) && $_->{'id'} eq $conversion
defined $conversion not ref $conversion $_->{'id'} eq $conversion
Truth table50%2/4
A and not(B) && C
A= defined $conversionB= not ref $conversionC= $_->{'id'} eq $conversion
ABCresult
0XX0
10X0
1100
1111
223 31 ▶ die "Unknown conversion\n" unless $route;
Branch: 1 branch
BranchTrueFalseTotal
unless $route 0 31 31
224 31 ▶ die "$route->{unavailableReason}\n" unless $route->{available};
Branch: 1 branch
BranchTrueFalseTotal
unless $route->{"available"} 0 31 31
225 31 my $input = $request->{input};
226 die "Provide JSON data or selected input files\n" unless ref($input) eq 'HASH'
227 31 ▶ && keys(%$input) == 1 && (exists $input->{data} || ref($input->{files}) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $input eq "HASH" and keys %$input == 1 and exists $input->{"data"} || ref $input->{"files"} eq "HASH" 0 31 31
Condition44%4/9
ref $input eq "HASH" and keys %$input == 1 and exists $input->{"data"} || ref $input->{"files"} eq "HASH"
!ll&&!rl&&r
0031
ref $input eq "HASH" and keys %$input == 1
!ll&&!rl&&r
0031
exists $input->{"data"} or ref $input->{"files"} eq "HASH"
l!l&&r!l&&!r
2290
MC/DC0%0/4
ref $input eq "HASH" and keys %$input == 1 and exists $input->{"data"} || ref $input->{"files"} eq "HASH"
ref $input eq "HASH" keys %$input == 1 exists $input->{"data"} ref $input->{"files"} eq "HASH"
Truth table40%2/5
A and B and C or D
A= ref $input eq "HASH"B= keys %$input == 1C= exists $input->{"data"}D= ref $input->{"files"} eq "HASH"
ABCDresult
0XXX0
10XX0
11000
111X1
11011
228 31 my %files;
229 31 ▶ for my $role (keys %{$input->{files} || {}}) {
Condition100%2/2
$input->{'files'} || {}
l!l
292
MC/DC100%1/1
$input->{'files'} || {}
$input->{'files'}
Truth table100%2/2
A
A= $input->{'files'}
Aresult
11
00
230 31 ▶ die "Selected files must be an array\n" unless ref($input->{files}{$role}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $input->{"files"}{$role} eq "ARRAY" 0 31 31
231 $files{$role} = [map {
232 31 my $file = $self->resolve_grant($_);
233 31 +{path => $file, filename => path($file)->basename};
234 31 } @{$input->{files}{$role}}];
235 }
236 31 ▶ my $destination = exists $request->{destination} ? $self->resolve_grant($request->{destination}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $request->{'destination'} ? : 15 16 31
237 31 ▶ die "Output destination must be a directory\n" if defined($destination) && !-d $destination;
Branch: 1 branch
BranchTrueFalseTotal
if defined $destination and not -d $destination 0 31 31
Condition66%2/3
defined $destination and not -d $destination
!ll&&!rl&&r
16150
MC/DC0%0/2
defined $destination and not -d $destination
defined $destination not -d $destination
Truth table66%2/3
A and not(B)
A= defined $destinationB= not -d $destination
ABresult
0X0
100
111
238 31 my $id = _id();
239 31 my $dir = path($self->{root}, $id);
240 31 $dir->mkpath({mode => 0700});
241 my $status = { id => $id, conversion => $conversion, status => 'queued', created => time(),
242 outputDirectory => _resolved_path($destination ? File::Spec->catdir($destination, 'convert-pheno-'.$id)
243 : File::Spec->catdir("$dir", 'outputs')),
244 options => $request->{options} || {}, output => $request->{output} || {},
245 31 ▶ sources => [map { map { $_->{filename} } @$_ } values %files] };
Branch: 1 branch
BranchTrueFalseTotal
$destination ? : 15 16 31
Condition50%2/4
$request->{'options'} || {}
l!l
310
$request->{'output'} || {}
l!l
310
MC/DC0%0/1
$request->{'options'} || {}
$request->{'options'}
MC/DC0%0/1
$request->{'output'} || {}
$request->{'output'}
Truth table50%1/2
A
A= $request->{'options'}
Aresult
11
00
Truth table50%1/2
A
A= $request->{'output'}
Aresult
11
00
246 _write($dir->child('request.json'), { conversion => $conversion, files => \%files,
247 request => { (exists($input->{data}) ? (input => {data => $input->{data}}) : ()),
248 options => $request->{options} || {}, output => $request->{output} || {} },
249 31 ▶ destination => $destination });
Branch: 1 branch
BranchTrueFalseTotal
exists $input->{'data'} ? : 2 29 31
Condition50%2/4
$request->{'options'} || {}
l!l
310
$request->{'output'} || {}
l!l
310
MC/DC0%0/1
$request->{'options'} || {}
$request->{'options'}
MC/DC0%0/1
$request->{'output'} || {}
$request->{'output'}
Truth table50%1/2
A
A= $request->{'options'}
Aresult
11
00
Truth table50%1/2
A
A= $request->{'output'}
Aresult
11
00
250 31 _write($dir->child('status.json'), $status);
251 31 push @{$self->{queue}}, $id;
252 31 $self->_next;
253 31 return $status;
254 }
255  
256 817 ▶sub _directory {
Subroutine _directory: called CC=6 SCAR=19.0
257 817 my ($self, $id) = @_;
258 817 ▶ die "Unknown run\n" unless defined($id) && !ref($id) && $id =~ /\A[a-f0-9]{40}\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id and not ref $id and $id =~ /\A[a-f0-9]{40}\z/ 1 816 817
Condition50%3/6
defined $id and not ref $id and $id =~ /\A[a-f0-9]{40}\z/
!ll&&!rl&&r
01816
defined $id and not ref $id
!ll&&!rl&&r
00817
MC/DC33%1/3
defined $id and not ref $id and $id =~ /\A[a-f0-9]{40}\z/
defined $id not ref $id $id =~ /\A[a-f0-9]{40}\z/
Truth table50%2/4
A and not(B) and C
A= defined $idB= not ref $idC= $id =~ /\A[a-f0-9]{40}\z/
ABCresult
0XX0
10X0
1100
1111
259 816 my $dir = path($self->{root}, $id);
260 816 ▶ die "Unknown run\n" unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 816 816
261 816 my $lock = _metadata_lock($dir->child('status.json'), LOCK_SH);
262 816 ▶ die "Unknown run\n" unless -f $dir->child('status.json');
Branch: 1 branch
BranchTrueFalseTotal
unless -f $dir->child("status.json") 0 816 816
263 816 return $dir;
264 }
265 725 ▶sub status {
Subroutine status: called CC=3 SCAR=11.2
Pod undocumented
266 725 my ($self,$id)=@_;
267 725 my $dir = $self->_directory($id);
268 724 my $status = _read($dir->child('status.json'));
269 # A worker records completed state before clearing its crash-recovery
270 # journal. Do not expose that transient state as a finished run.
271 $status = {%$status, status => 'running'}
272 724 ▶ if $status->{status} eq 'completed' && -f $dir->child('publication.json');
Branch: 1 branch
BranchTrueFalseTotal
if $status->{"status"} eq "completed" and -f $dir->child("publication.json") 0 724 724
Condition66%2/3
$status->{"status"} eq "completed" and -f $dir->child("publication.json")
!ll&&!rl&&r
5871370
MC/DC0%0/2
$status->{"status"} eq "completed" and -f $dir->child("publication.json")
$status->{"status"} eq "completed" -f $dir->child("publication.json")
Truth table66%2/3
A and B
A= $status->{"status"} eq "completed"B= -f $dir->child("publication.json")
ABresult
0X0
100
111
273 724 return $status;
274 }
275 8 ▶sub delete_history {
Subroutine delete_history: called CC=2 SCAR=6.9
Pod undocumented
276 8 my ($self, $id) = @_;
277 8 my $status = $self->status($id);
278 die "Cancel this run and wait for it to stop before deleting it from history\n"
279 7 ▶ unless $status->{status} =~ /\A(?:completed|failed|cancelled|interrupted)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $status->{"status"} =~ /\A(?:completed|failed|cancelled|interrupted)\z/ 2 5 7
280 # Hide the entry, not its directory: default output files live beside status.json.
281 # Never follow an outputDirectory or source path when removing a history entry.
282 5 $status->{deletedFromHistory} = JSON::XS::true;
283 5 _write($self->_directory($id)->child('status.json'), $status);
284 5 return {id => $id, deletedFromHistory => JSON::XS::true};
285 }
286 11 ▶sub delete_files {
Subroutine delete_files: called CC=43 SCAR=46.2
Pod undocumented
287 11 my ($self, $id) = @_;
288 die "This run is still finishing; wait before deleting its files\n"
289 11 ▶ if $self->{active}{$id};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"active"}{$id} 0 11 11
290 11 my $dir = $self->_directory($id);
291 11 my $status = $self->status($id);
292 die "Cancel this run and wait for it to stop before deleting files\n"
293 11 ▶ unless $status->{status} =~ /\A(?:completed|failed|cancelled|interrupted)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $status->{"status"} =~ /\A(?:completed|failed|cancelled|interrupted)\z/ 1 10 11
294 10 ▶ die "Run folder has moved or is a symbolic link\n"
Branch: 1 branch
BranchTrueFalseTotal
if -l $dir or not _same_path(abs_path($dir), $dir) 0 10 10
Condition33%1/3
-l $dir or not _same_path(abs_path($dir), $dir)
l!l&&r!l&&!r
0010
MC/DC0%0/2
-l $dir or not _same_path(abs_path($dir), $dir)
-l $dir not _same_path(abs_path($dir), $dir)
Truth table33%1/3
A or not(B)
A= -l $dirB= not _same_path(abs_path($dir), $dir)
ABresult
1X1
011
000
295 if -l $dir || !_same_path(abs_path($dir), $dir);
296 10 ▶ my $output = $status->{directory} || $status->{outputDirectory};
Condition33%1/3
$status->{'directory'} || $status->{'outputDirectory'}
l!l&&r!l&&!r
1000
MC/DC0%0/2
$status->{'directory'} || $status->{'outputDirectory'}
$status->{'directory'} $status->{'outputDirectory'}
Truth table33%1/3
A || B
A= $status->{'directory'}B= $status->{'outputDirectory'}
ABresult
1X1
011
000
297 10 my $default = $dir->child('outputs');
298 10 ▶ if (defined $output && (-e $output || -l $output)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $output and -e $output || -l $output) 10 0 10
Condition33%2/6
defined $output and -e $output || -l $output
!ll&&!rl&&r
0010
-e $output or -l $output
l!l&&r!l&&!r
1000
MC/DC0%0/3
defined $output and -e $output || -l $output
defined $output -e $output -l $output
Truth table25%1/4
A and B or C
A= defined $outputB= -e $outputC= -l $output
ABCresult
0XX0
1000
11X1
1011
299 # Only the published run subfolder is eligible, never its selected parent.
300 die "Output folder has moved or is not owned by this run\n"
301 if -l $output || !-d $output || !_same_path(abs_path($output), $output)
302 || (!_same_path($output, $default) && path($output)->basename ne "convert-pheno-$id")
303 10 ▶ || !_same_path($output, $status->{outputDirectory});
Branch: 1 branch
BranchTrueFalseTotal
if -l $output or not -d $output or not _same_path(abs_path($output), $output) or not _same_path($output, $default) and path($output)->basename ne "convert-pheno-$id" or not _same_path($output, $status->{"outputDirectory"}) 2 8 10
Condition60%9/15
-l $output or not -d $output or not _same_path(abs_path($output), $output) or not _same_path($output, $default) and path($output)->basename ne "convert-pheno-$id" or not _same_path($output, $status->{"outputDirectory"})
l!l&&r!l&&!r
208
-l $output or not -d $output or not _same_path(abs_path($output), $output) or not _same_path($output, $default) and path($output)->basename ne "convert-pheno-$id"
l!l&&r!l&&!r
028
-l $output or not -d $output or not _same_path(abs_path($output), $output)
l!l&&r!l&&!r
0010
-l $output or not -d $output
l!l&&r!l&&!r
0010
not _same_path($output, $default) and path($output)->basename ne "convert-pheno-$id"
!ll&&!rl&&r
712
MC/DC33%2/6
-l $output or not -d $output or not _same_path(abs_path($output), $output) or not _same_path($output, $default) and path($output)->basename ne "convert-pheno-$id" or not _same_path($output, $status->{"outputDirectory"})
-l $output not -d $output not _same_path(abs_path($output), $output) not _same_path($output, $default) path($output)->basename ne "convert-pheno-$id" not _same_path($output, $status->{"outputDirectory"})
Truth table37%3/8
A or not(B) or not(C) or not(D) and E or not(F)
A= -l $outputB= not -d $outputC= not _same_path(abs_path($output), $output)D= not _same_path($output, $default)E= path($output)->basename ne "convert-pheno-$id"F= not _same_path($output, $status->{"outputDirectory"})
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
00011X1
0000X11
0001011
0000X00
0001000
304 8 my %expected;
305 8 ▶ for my $entry (@{$status->{result}{artifacts} || []}) {
Condition50%1/2
$status->{'result'}{'artifacts'} || []
l!l
80
MC/DC0%0/1
$status->{'result'}{'artifacts'} || []
$status->{'result'}{'artifacts'}
Truth table50%1/2
A
A= $status->{'result'}{'artifacts'}
Aresult
11
00
306 8 my $name = $entry->{filename};
307 8 ▶ die "Invalid output filename\n" unless defined($name) && $name !~ m{[/\\]} && $name ne '.' && $name ne '..';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and not $name =~ m([/\\]) and $name ne "." and $name ne ".." 0 8 8
Condition33%3/9
defined $name and not $name =~ m([/\\]) and $name ne "." and $name ne ".."
!ll&&!rl&&r
008
defined $name and not $name =~ m([/\\]) and $name ne "."
!ll&&!rl&&r
008
defined $name and not $name =~ m([/\\])
!ll&&!rl&&r
008
MC/DC0%0/4
defined $name and not $name =~ m([/\\]) and $name ne "." and $name ne ".."
defined $name not $name =~ m([/\\]) $name ne "." $name ne ".."
Truth table20%1/5
A and not(B) and C and D
A= defined $nameB= not $name =~ m([/\\])C= $name ne "."D= $name ne ".."
ABCDresult
0XXX0
10XX0
110X0
11100
11111
308 8 $expected{$name} = 1;
309 }
310 8 for my $file (path($output)->children) {
311 die "Output folder contains unrecognized files; remove them manually before deleting this run\n"
312 10 ▶ unless $expected{$file->basename} && -f $file && !-l $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $expected{$file->basename} and -f $file and not -l $file 2 8 10
Condition66%4/6
$expected{$file->basename} and -f $file and not -l $file
!ll&&!rl&&r
208
$expected{$file->basename} and -f $file
!ll&&!rl&&r
208
MC/DC33%1/3
$expected{$file->basename} and -f $file and not -l $file
$expected{$file->basename} -f $file not -l $file
Truth table50%2/4
A and B and not(C)
A= $expected{$file->basename}B= -f $fileC= not -l $file
ABCresult
0XX0
10X0
1100
1111
313 }
314 }
315 6 for my $file ($dir->children) {
316 17 ▶ die "Run folder contains unrecognized files; deletion was not performed\n"
Branch: 1 branch
BranchTrueFalseTotal
if -l $file or not $file->basename =~ /\A(?:status\.json|request\.json|\.worker\.lock|\.metadata\.lock|mapping-.+|outputs|staging)\z/ 0 17 17
Condition33%1/3
-l $file or not $file->basename =~ /\A(?:status\.json|request\.json|\.worker\.lock|\.metadata\.lock|mapping-.+|outputs|staging)\z/
l!l&&r!l&&!r
0017
MC/DC0%0/2
-l $file or not $file->basename =~ /\A(?:status\.json|request\.json|\.worker\.lock|\.metadata\.lock|mapping-.+|outputs|staging)\z/
-l $file not $file->basename =~ /\A(?:status\.json|request\.json|\.worker\.lock|\.metadata\.lock|mapping-.+|outputs|staging)\z/
Truth table33%1/3
A or not(B)
A= -l $fileB= not $file->basename =~ /\A(?:status\.json|request\.json|\.worker\.lock|\.metadata\.lock|mapping-.+|outputs|staging)\z/
ABresult
1X1
011
000
317 if -l $file || ($file->basename !~ /\A(?:status\.json|request\.json|\.worker\.lock|\.metadata\.lock|mapping-.+|outputs|staging)\z/);
318 }
319 # A previous output can be an input to another queued/running conversion.
320 6 for my $other (@{$self->list}) {
321 15 ▶ next unless $other->{status} =~ /\A(?:queued|running|cancelling)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $other->{"status"} =~ /\A(?:queued|running|cancelling)\z/ 11 4 15
322 4 my $request = _read($self->_directory($other->{id})->child('request.json'));
323 4 ▶ for my $files (values %{$request->{files} || {}}) {
Condition50%1/2
$request->{'files'} || {}
l!l
40
MC/DC0%0/1
$request->{'files'} || {}
$request->{'files'}
Truth table50%1/2
A
A= $request->{'files'}
Aresult
11
00
324 4 for my $entry (@$files) {
325 4 ▶ for my $candidate ("$dir", defined($output) ? $output : ()) {
Branch: 1 branch
BranchTrueFalseTotal
defined $output ? : 4 0 4
326 6 my $entry_key = _path_key($entry->{path});
327 6 my $candidate_key = _path_key($candidate);
328 6 ▶ die "Another active run uses these files; wait for it to finish\n"
Branch: 1 branch
BranchTrueFalseTotal
if $entry_key eq $candidate_key or index($entry_key, "$candidate_key/") == 0 2 4 6
Condition66%2/3
$entry_key eq $candidate_key or index($entry_key, "$candidate_key/") == 0
l!l&&r!l&&!r
024
MC/DC50%1/2
$entry_key eq $candidate_key or index($entry_key, "$candidate_key/") == 0
$entry_key eq $candidate_key index($entry_key, "$candidate_key/") == 0
Truth table66%2/3
A or B
A= $entry_key eq $candidate_keyB= index($entry_key, "$candidate_key/") == 0
ABresult
1X1
011
000
329 if $entry_key eq $candidate_key || index($entry_key, "$candidate_key/") == 0;
330 }
331 }
332 }
333 }
334 4 ▶ if (defined $output && -d $output) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $output and -d $output) 4 0 4
Condition33%1/3
defined $output and -d $output
!ll&&!rl&&r
004
MC/DC0%0/2
defined $output and -d $output
defined $output -d $output
Truth table33%1/3
A and B
A= defined $outputB= -d $output
ABresult
0X0
100
111
335 4 ▶ for my $file (path($output)->children) { unlink $file or die "Could not delete an output file\n" }
Branch: 1 branch
BranchTrueFalseTotal
unlink $file or die "Could not delete an output file\n" 4 0 4
336 4 ▶ rmdir $output or die "Could not remove the output folder\n";
Branch: 1 branch
BranchTrueFalseTotal
rmdir $output or die "Could not remove the output folder\n" 4 0 4
337 }
338 4 remove_tree("$dir", {error => \my $errors});
339 4 ▶ die "Could not completely remove the run folder\n" if @$errors;
Branch: 1 branch
BranchTrueFalseTotal
if @$errors 0 4 4
340 4 return {id => $id, deletedFromDisk => JSON::XS::true};
341 }
342 17 ▶sub list {
Subroutine list: called CC=6 SCAR=17.9
Pod undocumented
343 17 my ($self, %args)=@_;
344 60 my $runs = [sort { $b->{created} <=> $a->{created} } map { $self->status($_->basename) }
345 grep {
346 69 ▶ if ($_->basename =~ /\A[a-f0-9]{40}\z/ && -d $_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->basename =~ /\A[a-f0-9]{40}\z/ and -d $_) { } 52 17 69
Condition66%2/3
$_->basename =~ /\A[a-f0-9]{40}\z/ and -d $_
!ll&&!rl&&r
17052
MC/DC50%1/2
$_->basename =~ /\A[a-f0-9]{40}\z/ and -d $_
$_->basename =~ /\A[a-f0-9]{40}\z/ -d $_
Truth table66%2/3
A and B
A= $_->basename =~ /\A[a-f0-9]{40}\z/B= -d $_
ABresult
0X0
100
111
347 52 my $lock = _metadata_lock($_->child('status.json'), LOCK_SH);
348 52 -f $_->child('status.json');
349 }
350 17 else { 0 }
351 17 } path($self->{root})->children];
352 17 ▶ $runs = [grep { !$_->{deletedFromHistory} } @$runs] unless $args{include_deleted};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"include_deleted"} 16 1 17
353 17 my %positions;
354 17 my $position = 0;
355 17 $positions{$_} = ++$position for @{$self->{queue}};
356 17 $_->{queuePosition} = $positions{$_->{id}} for grep { exists $positions{$_->{id}} } @$runs;
357 17 return $runs;
358 }
359 2 ▶sub delete_all {
Subroutine delete_all: called CC=5 SCAR=16.1
Pod undocumented
360 2 my ($self, $files) = @_;
361 2 my (@deleted, @skipped, @failed);
362 2 for my $run (@{$self->list(include_deleted => $files)}) {
363 10 ▶ if ($run->{status} !~ /\A(?:completed|failed|cancelled|interrupted)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($run->{"status"} =~ /\A(?:completed|failed|cancelled|interrupted)\z/) 2 8 10
364 2 push @skipped, $run->{id}; next;
365 }
366 8 ▶ my $ok = eval { $files ? $self->delete_files($run->{id}) : $self->delete_history($run->{id}); 1 };
Branch: 1 branch
BranchTrueFalseTotal
$files ? : 5 3 8
367 8 ▶ if ($ok) { push @deleted, $run->{id} }
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 5 3 8
368 3 else { my $message = "$@"; chomp $message; push @failed, {id=>$run->{id}, message=>$message} }
369 }
370 2 return {deleted=>\@deleted, skipped=>\@skipped, failed=>\@failed};
371 }
372  
373 3301 ▶sub _next {
Subroutine _next: called CC=7 SCAR=20.0
374 3301 my ($self)=@_;
375 3301 ▶ return if $self->{stopped};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stopped"} 35 3266 3301
376 # Each independent conversion owns one process and a separate run folder.
377 # Lowering the limit never interrupts existing workers.
378 3266 ▶ while (@{$self->{queue}} && keys(%{$self->{active}}) < $self->{max_concurrent_jobs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"queue"};} and keys %{$$self{"active"};} < $self->{"max_concurrent_jobs"}) 26 3266 3292
Condition100%3/3
@{$self->{"queue"};} and keys %{$$self{"active"};} < $self->{"max_concurrent_jobs"}
!ll&&!rl&&r
1483178326
MC/DC100%2/2
@{$self->{"queue"};} and keys %{$$self{"active"};} < $self->{"max_concurrent_jobs"}
@{$self->{"queue"};} keys %{$$self{"active"};} < $self->{"max_concurrent_jobs"}
Truth table100%3/3
A and B
A= @{$self->{"queue"};}B= keys %{$$self{"active"};} < $self->{"max_concurrent_jobs"}
ABresult
0X0
100
111
379 26 my $id = shift @{$self->{queue}};
380 26 my $dir = $self->_directory($id);
381 26 my $status = $self->status($id);
382 26 $status->{status} = 'running';
383 26 _write($dir->child('status.json'), $status);
384 26 my ($stdin, $stdout, $stderr) = (gensym(), gensym(), gensym());
385 26 my $pid = eval { open3($stdin, $stdout, $stderr, $^X, $self->{worker}, "$dir") };
386 26 ▶ if (!$pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 0 26 26
387 0 $status->{status} = 'failed'; $status->{message} = 'Could not start the conversion worker';
388 0 $status->{finished} = time();
389 0 _write($dir->child('status.json'), $status);
390 0 unlink $dir->child('request.json');
391 0 next;
392 }
393 26 close $stdin;
394 26 $self->{active}{$id} = { id => $id, pid => $pid, stdout => $stdout, stderr => $stderr };
395 }
396 0 ▶ $self->{timer} = Mojo::IOLoop->recurring(0.2 => sub { $self->poll })
Subroutine __ANON__: not called CC=1 SCAR=6.9
397 3266 ▶ if keys(%{$self->{active}}) && !defined($self->{timer});
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$$self{"active"};} and not defined $self->{"timer"} 14 3252 3266
Condition100%3/3
keys %{$$self{"active"};} and not defined $self->{"timer"}
!ll&&!rl&&r
12324014
MC/DC100%2/2
keys %{$$self{"active"};} and not defined $self->{"timer"}
keys %{$$self{"active"};} not defined $self->{"timer"}
Truth table100%3/3
A and not(B)
A= keys %{$$self{"active"};}B= not defined $self->{"timer"}
ABresult
0X0
100
111
398 }
399  
400 3262 ▶sub poll {
Subroutine poll: called CC=10 SCAR=23.2
Pod undocumented
401 3262 my ($self)=@_;
402 3262 for my $id (keys %{$self->{active}}) {
403 6347 my $active = $self->{active}{$id};
404 6347 ▶ next if waitpid($active->{pid}, WNOHANG) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if waitpid($active->{"pid"}, 1) == 0 6321 26 6347
405 26 close $active->{stdout}; close $active->{stderr};
406 26 my $dir = $self->_directory($active->{id});
407 26 my $status = $self->status($active->{id});
408 26 my $recovered = eval { _recover_publication($dir, $status) };
409 26 ▶ if ($recovered) { $status = $recovered }
Branch: 1 branch
BranchTrueFalseTotal
if ($recovered) { } 26 0 26
410 else {
411 0 $status->{status} = 'failed';
412 0 $status->{message} = 'Export recovery could not safely clean the output folder. Files were retained for manual review.';
413 0 $status->{finished} = time();
414 0 _write($dir->child('status.json'), $status);
415 }
416 26 ▶ if ($status->{status} =~ /\A(?:running|cancelling)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status->{"status"} =~ /\A(?:running|cancelling)\z/) 6 20 26
417 6 ▶ $status->{status} = $active->{cancelled} ? 'cancelled' : 'failed';
Branch: 1 branch
BranchTrueFalseTotal
$active->{'cancelled'} ? : 5 1 6
418 6 ▶ $status->{message} = $active->{cancelled} ? 'Conversion cancelled' : 'Conversion worker stopped unexpectedly';
Branch: 1 branch
BranchTrueFalseTotal
$active->{'cancelled'} ? : 5 1 6
419 6 _write($dir->child('status.json'), $status);
420 }
421 26 ▶ remove_tree($dir->child('staging')) if $status->{status} ne 'completed';
Branch: 1 branch
BranchTrueFalseTotal
if $status->{"status"} ne "completed" 6 20 26
422 26 unlink $dir->child('request.json');
423 26 delete $self->{active}{$id};
424 }
425 Mojo::IOLoop->remove(delete $self->{timer})
426 3262 ▶ if !keys(%{$self->{active}}) && defined($self->{timer});
Branch: 1 branch
BranchTrueFalseTotal
if not keys %{$$self{"active"};} and defined $self->{"timer"} 14 3248 3262
Condition100%3/3
not keys %{$$self{"active"};} and defined $self->{"timer"}
!ll&&!rl&&r
32252314
MC/DC100%2/2
not keys %{$$self{"active"};} and defined $self->{"timer"}
not keys %{$$self{"active"};} defined $self->{"timer"}
Truth table100%3/3
not(A) and B
A= not keys %{$$self{"active"};}B= defined $self->{"timer"}
ABresult
0X0
100
111
427 3262 $self->_next;
428 }
429  
430 15 ▶sub cancel {
Subroutine cancel: called CC=4 SCAR=13.9
Pod undocumented
431 15 my ($self,$id)=@_;
432 15 $self->poll;
433 15 my $status = $self->status($id);
434 15 ▶ if ($status->{status} eq 'queued') {
Branch: 2 branches
BranchTrueFalseTotal
if ($status->{'status'} eq 'queued') { } 5 10 15
elsif ($status->{'status'} eq 'running') { } 5 5 10
435 5 @{$self->{queue}} = grep { $_ ne $id } @{$self->{queue}};
436 5 $status->{status} = 'cancelled';
437 5 $status->{finished} = time();
438 5 unlink $self->_directory($id)->child('request.json');
439 } elsif ($status->{status} eq 'running') {
440 die "This run does not belong to the active worker\n"
441 5 ▶ unless $self->{active}{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"active"}{$id} 0 5 5
442 5 $status->{status} = 'cancelling';
443 5 $self->{active}{$id}{cancelled} = 1;
444 5 kill 'KILL', $self->{active}{$id}{pid};
445 }
446 15 _write($self->_directory($id)->child('status.json'),$status);
447 15 return $status;
448 }
449  
450 29 ▶sub cancel_pending {
Subroutine cancel_pending: called CC=1 SCAR=0.0
Pod undocumented
451 29 my ($self)=@_;
452 # Detach the pending queue before polling, so none of these jobs can start
453 # while the bulk cancellation is being processed.
454 29 my @pending = @{$self->{queue}};
455 29 $self->{queue} = [];
456 29 return [map { $self->cancel($_) } @pending];
457 }
458  
459 31 ▶sub shutdown {
Subroutine shutdown: called CC=5 SCAR=16.1
Pod undocumented
460 31 my ($self)=@_;
461 31 ▶ return if $self->{stopped};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stopped"} 4 27 31
462 # Prevent polling/cancellation from starting queued work during shutdown.
463 27 $self->{stopped} = 1;
464 27 $self->cancel_pending;
465 27 $self->cancel($_) for keys %{$self->{active}};
466 27 for my $active (values %{$self->{active}}) {
467 5 waitpid($active->{pid},0);
468 }
469 27 $self->poll;
470 27 ▶ close(delete $self->{storage_lock}) if $self->{storage_lock};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"storage_lock"} 27 0 27
471 }
472  
473 38 ▶sub artifact {
Subroutine artifact: called CC=7 SCAR=21.1
Pod undocumented
474 38 my ($self,$id,$artifact)=@_;
475 38 my $status = $self->status($id);
476 38 ▶ die "Run has no completed outputs\n" unless $status->{status} eq 'completed';
Branch: 1 branch
BranchTrueFalseTotal
unless $status->{"status"} eq "completed" 0 38 38
477 38 ▶ my ($entry) = grep { $_->{id} eq $artifact } @{$status->{result}{artifacts} || []};
Condition50%1/2
$status->{'result'}{'artifacts'} || []
l!l
380
MC/DC0%0/1
$status->{'result'}{'artifacts'} || []
$status->{'result'}{'artifacts'}
Truth table50%1/2
A
A= $status->{'result'}{'artifacts'}
Aresult
11
00
478 38 ▶ die "Unknown output file\n" unless $entry;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry 1 37 38
479 37 my $file = path($status->{directory},$entry->{filename});
480 37 ▶ die "Output file is missing or changed\n" if !-f $file || -l $file || -s $file != $entry->{bytes};
Branch: 1 branch
BranchTrueFalseTotal
if not -f $file or -l $file or -s $file != $entry->{"bytes"} 0 37 37
Condition33%2/6
not -f $file or -l $file or -s $file != $entry->{"bytes"}
l!l&&r!l&&!r
0037
not -f $file or -l $file
l!l&&r!l&&!r
0037
MC/DC0%0/3
not -f $file or -l $file or -s $file != $entry->{"bytes"}
not -f $file -l $file -s $file != $entry->{"bytes"}
Truth table25%1/4
not(A) or B or C
A= not -f $fileB= -l $fileC= -s $file != $entry->{"bytes"}
ABCresult
1XX1
01X1
0011
0000
481 37 return ($file, $entry);
482 }
483  
484 1 ▶sub preview {
Subroutine preview: called CC=7 SCAR=20.9
Pod undocumented
485 1 my ($self,$id,$artifact)=@_;
486 1 my ($file,$entry)=$self->artifact($id,$artifact);
487 return {kind => 'xlsx', text => 'Open the spreadsheet to inspect the complete report', truncated => JSON::XS::true}
488 1 ▶ if $entry->{kind} eq 'xlsx';
Branch: 1 branch
BranchTrueFalseTotal
if $entry->{"kind"} eq "xlsx" 0 1 1
489 1 ▶ open my $fh,'<:raw',$file or die "Cannot open output\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $file or die "Cannot open output\n" 1 0 1
490 1 my $bytes=''; read($fh,$bytes,262144); close $fh;
491 1 my $truncated = length($bytes) < -s $file;
492 1 my $text = decode('UTF-8',$bytes,FB_DEFAULT);
493 1 ▶ my $data = !$truncated && $entry->{kind} eq 'json' ? eval { $JSON->decode($bytes) } : undef;
Branch: 1 branch
BranchTrueFalseTotal
!$truncated && $entry->{'kind'} eq 'json' ? : 1 0 1
Condition33%1/3
not $truncated and $entry->{"kind"} eq "json"
!ll&&!rl&&r
001
MC/DC0%0/2
not $truncated and $entry->{"kind"} eq "json"
not $truncated $entry->{"kind"} eq "json"
Truth table33%1/3
not(A) and B
A= not $truncatedB= $entry->{"kind"} eq "json"
ABresult
0X0
100
111
494 return {text => $text, (defined($data) ? (data => $data) : ()),
495 1 ▶ truncated => $truncated ? JSON::XS::true : JSON::XS::false, kind => $entry->{kind}};
Branch: 2 branches
BranchTrueFalseTotal
defined $data ? : 1 0 1
$truncated ? : 0 1 1
496 }
497  
498 2 ▶sub input_preview {
Subroutine input_preview: called CC=13 SCAR=26.1
Pod undocumented
499 2 my ($self,$handle)=@_;
500 2 my $file=$self->resolve_grant($handle);
501 2 ▶ if (-d $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $file) 1 1 2
502 1 ▶ opendir my $dh, $file or die "Cannot read selected directory\n";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $file or die "Cannot read selected directory\n" 1 0 1
503 1 my (@names, $bytes, $truncated);
504 1 ▶ while (defined(my $name = readdir $dh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $name = readdir $dh)) 1003 0 1003
505 1003 ▶ next if $name eq '.' || $name eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "." or $name eq ".." 2 1001 1003
Condition100%3/3
$name eq "." or $name eq ".."
l!l&&r!l&&!r
111001
MC/DC100%2/2
$name eq "." or $name eq ".."
$name eq "." $name eq ".."
Truth table100%3/3
A or B
A= $name eq "."B= $name eq ".."
ABresult
1X1
011
000
506 1001 ▶ if (@names >= 1000 || ($bytes || 0) + length($name) + 1 > 262144) { $truncated = 1; last }
Branch: 1 branch
BranchTrueFalseTotal
if (@names >= 1000 or ($bytes || 0) + length($name) + 1 > 262144) 1 1000 1001
Condition80%4/5
@names >= 1000 or ($bytes || 0) + length($name) + 1 > 262144
l!l&&r!l&&!r
101000
$bytes || 0
l!l
9991
MC/DC100%2/2
@names >= 1000 or ($bytes || 0) + length($name) + 1 > 262144
@names >= 1000 $bytes
Truth table100%3/3
A or B
A= @names >= 1000B= $bytes
ABresult
1X1
011
000
507 1000 push @names, $name;
508 1000 $bytes += length($name) + 1;
509 }
510 1 closedir $dh;
511 1 ▶ return {text=>join("\n",sort @names),truncated=>$truncated ? JSON::XS::true : JSON::XS::false,
Branch: 1 branch
BranchTrueFalseTotal
$truncated ? : 1 0 1
512 previewNote=>'Directory listing: up to 1,000 entries and 256 KiB.'};
513 }
514 1 ▶ open my $fh,'<:raw',$file or die "Cannot read selected input\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $file or die "Cannot read selected input\n" 1 0 1
515 1 my $bytes=''; read($fh,$bytes,262144); close $fh;
516 1 my ($kind) = $file =~ /\.(json|csv|tsv|yaml|xml)\z/i;
517 1 ▶ return {text=>decode('UTF-8',$bytes,FB_DEFAULT),truncated=>length($bytes)<-s $file ? JSON::XS::true : JSON::XS::false,
Branch: 2 branches
BranchTrueFalseTotal
length $bytes < -s $file ? : 1 0 1
defined $kind ? : 1 0 1
518 (defined($kind) ? (kind => lc($kind)) : ())};
519 }
520  
521 2 ▶sub save_mapping {
Subroutine save_mapping: called CC=5 SCAR=16.5
Pod undocumented
522 2 my ($self,$text)=@_;
523 2 ▶ die "Mapping text is missing or too large\n" if !defined($text) || ref($text) || length($text)>1048576;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $text or ref $text or length $text > 1048576 0 2 2
Condition33%2/6
not defined $text or ref $text or length $text > 1048576
l!l&&r!l&&!r
002
not defined $text or ref $text
l!l&&r!l&&!r
002
MC/DC0%0/3
not defined $text or ref $text or length $text > 1048576
not defined $text ref $text length $text > 1048576
Truth table25%1/4
not(A) or B or C
A= not defined $textB= ref $textC= length $text > 1048576
ABCresult
1XX1
01X1
0011
0000
524 2 my $directory=path($self->{root},'mappings'); $directory->mkpath({mode=>0700});
525 2 my $file=$directory->child(_id().'.yaml');
526 2 $file->spew_utf8($text);
527 2 my $ok=eval {
528 2 require Convert::Pheno::IO::CSVHandler;
529 2 Convert::Pheno::IO::CSVHandler::read_mapping_file({mapping_file=>"$file",
530 schema_file=>File::Spec->catfile($Convert::Pheno::share_dir,'schema','mapping-v2.json')});
531 1 1;
532 };
533 2 ▶ if (!$ok) {my $error=$@; unlink $file; die $error}
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 1 2
534 1 return $self->register_file("$file");
535 }
536  
537 # Worker entry point: request and outputs stay on disk, outside the HTTP event
538 # loop. The request is private operational state, never an application log.
539 20 ▶sub perform {
Subroutine perform: called CC=24 SCAR=35.1
Pod undocumented
540 20 my ($class,$directory)=@_;
541 20 my $dir=path($directory);
542 20 my $lock = _worker_lock($dir);
543 20 my $status=_read($dir->child('status.json'));
544 20 ▶ die "This run is no longer active\n" unless $status->{status} eq 'running';
Branch: 1 branch
BranchTrueFalseTotal
unless $status->{"status"} eq "running" 0 20 20
545 20 my $job=_read($dir->child('request.json'));
546 20 my $staging=$dir->child('staging'); $staging->mkpath({mode=>0700});
547 20 my $ok=eval {
548 20 my @fingerprints;
549 20 for my $role (keys %{$job->{files}}) {
550 20 for my $entry (@{$job->{files}{$role}}) {
551 20 my $file=$entry->{path};
552 20 ▶ die "Selected input is no longer available\n" if !-e $file || -l $file;
Branch: 1 branch
BranchTrueFalseTotal
if not -e $file or -l $file 0 20 20
Condition33%1/3
not -e $file or -l $file
l!l&&r!l&&!r
0020
MC/DC0%0/2
not -e $file or -l $file
not -e $file -l $file
Truth table33%1/3
not(A) or B
A= not -e $fileB= -l $file
ABresult
1X1
011
000
553 20 ▶ if (-d $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $file) 0 20 20
554 0 ▶ find({no_chdir=>1,wanted=>sub { die "Directory packages must not contain symbolic links\n" if -l $File::Find::name }},$file);
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if -l $File::Find::name 0 0 0
555 }
556 20 ▶ if (-f $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file) 20 0 20
557 20 ▶ open my $fh,'<:raw',$file or die "Cannot read selected input\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $file or die "Cannot read selected input\n" 20 0 20
558 20 my $sha=Digest::SHA->new(256)->addfile($fh)->hexdigest; close $fh;
559 20 push @fingerprints,{filename=>$entry->{filename},role=>$role,sha256=>$sha};
560 20 ▶ if ($role eq 'mapping') {
Branch: 1 branch
BranchTrueFalseTotal
if ($role eq "mapping") 2 18 20
561 2 my $snapshot=$dir->child('mapping-'.$entry->{filename});
562 2 ▶ copy($file,$snapshot) or die "Cannot snapshot mapping\n";
Branch: 1 branch
BranchTrueFalseTotal
copy($file, $snapshot) or die "Cannot snapshot mapping\n" 2 0 2
563 2 $entry->{path}="$snapshot";
564 }
565 }
566 }
567 }
568 20 my $delivery={directory=>"$staging",workspace=>"$staging",native=>1};
569 my $result=exists($job->{request}{input})
570 ? execute($job->{conversion},$job->{request},$delivery)
571 20 ▶ : execute_files($job->{conversion},$job->{request},$job->{files},$delivery);
Branch: 1 branch
BranchTrueFalseTotal
exists $job->{'request'}{'input'} ? : 2 18 20
572 my $final=path(_resolved_path($job->{destination}
573 20 ▶ ? path($job->{destination},'convert-pheno-'.$status->{id}) : $dir->child('outputs')));
Branch: 1 branch
BranchTrueFalseTotal
$job->{'destination'} ? : 15 5 20
574 20 ▶ die "Output destination already exists\n" if -e $final;
Branch: 1 branch
BranchTrueFalseTotal
if -e $final 0 20 20
575 20 ▶ if ($job->{destination}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($job->{'destination'}) { } 15 5 20
576 # Copy to a private sibling before the final rename so cross-volume
577 # exports cannot expose a half-written run directory.
578 15 my $sibling=path(_resolved_path(path($job->{destination},'.convert-pheno-'.$status->{id})));
579 15 ▶ die "Output staging directory already exists\n" if -e $sibling;
Branch: 1 branch
BranchTrueFalseTotal
if -e $sibling 0 15 15
580 15 my $owner = _id();
581 15 my $completed = {%$status, status=>'completed', result=>$result, directory=>"$final",
582 fingerprints=>\@fingerprints, engineVersion=>$Convert::Pheno::VERSION, finished=>time()};
583 # Journal ownership before copying large outputs. Recovery publishes
584 # completed state if rename succeeded, otherwise removes only our copy.
585 15 ▶ mkdir "$sibling",0700 or die "Cannot create export staging folder\n";
Branch: 1 branch
BranchTrueFalseTotal
mkdir "$sibling", 448 or die "Cannot create export staging folder\n" 15 0 15
586 15 $sibling->child('.convert-pheno-owner')->spew_raw($owner);
587 15 _write($dir->child('publication.json'), {owner=>$owner,staging=>"$sibling",final=>"$final",completed=>$completed});
588 15 ▶ eval { for my $file ($staging->children) { copy($file,$sibling->child($file->basename)) or die "Could not export output\n" } 1 }
Branch: 1 branch
BranchTrueFalseTotal
copy($file, $sibling->child($file->basename)) or die "Could not export output\n" 29 0 29
589 0 ▶ or do { my $err=$@; remove_tree($sibling); die $err };
Branch: 1 branch
BranchTrueFalseTotal
eval { do { foreach my $file ($staging->children) { copy($file, $sibling->child($file->basename)) or die "Could not export output\n"; } ; 1 } } or do { my $err = $@; remove_tree($sibling); die $err } 15 0 15
590 15 ▶ rename $sibling,$final or die "Could not publish output directory\n";
Branch: 1 branch
BranchTrueFalseTotal
rename $sibling, $final or die "Could not publish output directory\n" 15 0 15
591 15 remove_tree($staging);
592 } else {
593 5 my $owner = _id();
594 5 my $completed = {%$status, status=>'completed', result=>$result, directory=>"$final",
595 fingerprints=>\@fingerprints, engineVersion=>$Convert::Pheno::VERSION, finished=>time()};
596 5 $staging->child('.convert-pheno-owner')->spew_raw($owner);
597 5 _write($dir->child('publication.json'), {owner=>$owner,staging=>"$staging",final=>"$final",completed=>$completed});
598 5 ▶ rename $staging,$final or die "Could not publish output directory\n";
Branch: 1 branch
BranchTrueFalseTotal
rename $staging, $final or die "Could not publish output directory\n" 5 0 5
599 }
600 20 $status->{status}='completed'; $status->{result}=$result; $status->{directory}="$final";
601 20 $status->{fingerprints}=\@fingerprints; $status->{engineVersion}=$Convert::Pheno::VERSION;
602 20 1;
603 };
604 20 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 20 20
605 0 $status->{status}='failed'; $status->{message}="$@";
606 0 remove_tree($staging);
607 }
608 20 $status->{finished}=time();
609 20 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 20 0 20
610 20 unlink $dir->child('request.json');
611 20 $status = _recover_publication($dir, $status);
612 } else {
613 0 _write($dir->child('status.json'),$status);
614 0 unlink $dir->child('request.json');
615 }
616 20 return $ok;
617 }
618  
619 32 1;
« lib/Convert/Pheno/FHIR/Profile/MCode.pm ↑ Summary lib/Convert/Pheno/Source/CDISC/DatasetXML.pm »