| 1 | package App::MultiModule::API; | ||||||||||||||||||||||||||||||||||
| 2 | 1 | $App::MultiModule::API::VERSION = '1.171870'; | |||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict;use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Sereal::Encoder qw(encode_sereal); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Sereal::Decoder qw(looks_like_sereal decode_sereal); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||
| 9 | =head2 new | ||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||
| 11 | Constructor | ||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||
| 13 | =over 4 | ||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||
| 15 | =item state | ||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||
| 17 | Directory path where various run-time files are kept. Defaults to state/. | ||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||
| 19 | =back | ||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||
| 22 | =cut | ||||||||||||||||||||||||||||||||||
| 23 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 24 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||||
| 25 | 0 | my %args = @_; | |||||||||||||||||||||||||||||||||
| 26 | 0 | my $debug = $args{debug}; | |||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | $debug = 5 unless defined $debug; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | $args{state_dir} = 'state' unless $args{state_dir}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 29 | my $self = { | ||||||||||||||||||||||||||||||||||
| 30 | state_dir => $args{state_dir}, | ||||||||||||||||||||||||||||||||||
| 31 | 0 | }; | |||||||||||||||||||||||||||||||||
| 32 | 0 | bless ($self, $class); | |||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||
| 34 | 0 | return $self; | |||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||
| 37 | =head1 METHODS | ||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||
| 39 | =cut | ||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | sub _read_file { | ||||||||||||||||||||||||||||||||
|
Subroutine
_read_file: not called CC=13 SCAR=52.0
| |||||||||||||||||||||||||||||||||||
| 41 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 42 | 0 | my $filename = shift; | |||||||||||||||||||||||||||||||||
| 43 | 0 | my %args = @_; | |||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | return undef unless my $state_dir = $self->{state_dir}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | mkdir $state_dir unless -e $state_dir; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | return undef unless -r "$state_dir/$filename"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 47 | 0 | my $ret; | |||||||||||||||||||||||||||||||||
| 48 | 0 | eval { | |||||||||||||||||||||||||||||||||
| 49 | 0 | my $f; | |||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | die "passed state_dir $state_dir not writable" | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 51 | unless -w $state_dir; | ||||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | die "passed state_dir $state_dir not a directory" | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 53 | unless -d $state_dir; | ||||||||||||||||||||||||||||||||||
| 54 | 0 | my $file_path = "$state_dir/$filename"; | |||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | open my $fh, '<', $file_path or die "failed to open $file_path for reading: $!"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | while(<$fh>) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 57 | 0 | $f .= $_; | |||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | close $fh or die "failed to close $file_path: $!"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | if(looks_like_sereal($f)) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | $ret = decode_sereal $f or die 'returned false'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 62 | } else { | ||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | $ret = do $file_path or die "failed to deserialize $file_path: $@"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||
| 65 | }; | ||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | die "App::MultiModule::API::_read_file failed: $@\n" if $@; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 67 | 0 | return $ret; | |||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | sub _write_file { | ||||||||||||||||||||||||||||||||
|
Subroutine
_write_file: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||||
| 72 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 73 | 0 | my $filename = shift; | |||||||||||||||||||||||||||||||||
| 74 | 0 | my $contents = shift; | |||||||||||||||||||||||||||||||||
| 75 | 0 | my %args = @_; | |||||||||||||||||||||||||||||||||
| 76 | 0 | eval { | |||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | mkdir $self->{state_dir} unless -e $self->{state_dir}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | open my $fh, '>', "$self->{state_dir}/$filename.tmp" | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 79 | or die "open failed: $!\n"; | ||||||||||||||||||||||||||||||||||
| 80 | # print $fh Data::Dumper::Dumper $contents or die "print failed: $!\n"; | ||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | print $fh encode_sereal $contents or die "print failed: $!\n"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | close $fh or die "close failed: $!\n"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | rename "$self->{state_dir}/$filename.tmp","$self->{state_dir}/$filename" or die "rename failed: $!\n"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 84 | }; | ||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | die "App::MultiModule::API::_write_file failed: $@\n" if $@; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | sub _my_read_file { | ||||||||||||||||||||||||||||||||
|
Subroutine
_my_read_file: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||
| 89 | 0 | my $path = shift; | |||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | open my $fh, '<', $path or die "failed to open $path: $!\n"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | read $fh, my $ret, 1024000 | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 92 | or die "failed to read from $path: $!\n"; | ||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | close $fh or die "failed to close $path: $!\n"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 94 | 0 | return $ret; | |||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||
| 97 | =head2 get_task_status($task_name) | ||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||
| 99 | Returns the saved status | ||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||
| 101 | Example: | ||||||||||||||||||||||||||||||||||
| 102 | status => { | ||||||||||||||||||||||||||||||||||
| 103 | 'is_my_pid' => 0, | ||||||||||||||||||||||||||||||||||
| 104 | 'is_running' => 1, | ||||||||||||||||||||||||||||||||||
| 105 | 'save_ts' => 1370987265, | ||||||||||||||||||||||||||||||||||
| 106 | 'task_count' => 1, | ||||||||||||||||||||||||||||||||||
| 107 | 'fd_count' => 5, | ||||||||||||||||||||||||||||||||||
| 108 | 'cmdline' => '/usr/bin/perlbin/MultiModule-qtqueue-pMultiModuleTest::-oalert:test_alert_queue,this:that-mOtherExternalModule', | ||||||||||||||||||||||||||||||||||
| 109 | 'stat' => '3577 (MultiModule) S 1 13564 19489 34848 13564 4202496 3202 0 0 0 16 3 0 0 20 0 1 0 36033429 71139328 2949 18446744073709551615 4194304 4198756 140735555873936 140735555873176 139685157236755 0 0 4096 16384 18446744071580469929 0 0 17 2 0 0 0 0 0 | ||||||||||||||||||||||||||||||||||
| 110 | ', | ||||||||||||||||||||||||||||||||||
| 111 | 'pid' => 3577, | ||||||||||||||||||||||||||||||||||
| 112 | 'statm' => '17368 2949 624 2 0 5133 0 | ||||||||||||||||||||||||||||||||||
| 113 | }, | ||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||
| 115 | =cut | ||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | sub get_task_status { | ||||||||||||||||||||||||||||||||
|
Subroutine
get_task_status: not called CC=18 SCAR=58.3
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 117 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 118 | 0 | my $task_name = shift; | |||||||||||||||||||||||||||||||||
| 119 | 0 | my %args = @_; | |||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||
| 121 | 0 | my $status; | |||||||||||||||||||||||||||||||||
| 122 | 0 | eval { | |||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | $status = $self->_read_file($task_name . '.status') | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 124 | or die 'no status'; | ||||||||||||||||||||||||||||||||||
| 125 | }; | ||||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | if($@) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 127 | return { | ||||||||||||||||||||||||||||||||||
| 128 | 0 | initial_state => 1, | |||||||||||||||||||||||||||||||||
| 129 | }; | ||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||
| 132 | 0 | my $status_pid; my $status_cmdline; | |||||||||||||||||||||||||||||||||
| 133 | 0 | eval { | |||||||||||||||||||||||||||||||||
| 134 | 0 | $status_pid = $status->{pid}; | |||||||||||||||||||||||||||||||||
| 135 | die "loaded status does not have required attribute 'cmdline'" | ||||||||||||||||||||||||||||||||||
| 136 | 0 | ▶ | unless $status_cmdline = $status->{cmdline}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 137 | die "loaded status does not have required attribute 'save_ts'" | ||||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | unless my $status_save_ts = $status->{save_ts}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 139 | }; | ||||||||||||||||||||||||||||||||||
| 140 | 0 | ▶ | if($@) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | die "App::MultiModule::API::get_task_status: $@\n" if $@; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||
| 144 | #default the status to 'not running' | ||||||||||||||||||||||||||||||||||
| 145 | 0 | $status->{is_running} = 0; | |||||||||||||||||||||||||||||||||
| 146 | 0 | $status->{is_my_pid} = 0; | |||||||||||||||||||||||||||||||||
| 147 | #what does it take to claim the process in the status file is | ||||||||||||||||||||||||||||||||||
| 148 | #running | ||||||||||||||||||||||||||||||||||
| 149 | #1. the PID has to be running | ||||||||||||||||||||||||||||||||||
| 150 | #2. the cmdline saved in the status file has to match the cmdline running | ||||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | if($status_pid) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 152 | 0 | eval { | |||||||||||||||||||||||||||||||||
| 153 | 0 | my $status_pid_cmdline = _my_read_file("/proc/$status_pid/cmdline"); | |||||||||||||||||||||||||||||||||
| 154 | 0 | chomp $status_cmdline; chomp $status_pid_cmdline; | |||||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | die "not running\n" if $status_cmdline ne $status_pid_cmdline; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||
| 157 | 0 | $status->{is_running} = 1; | |||||||||||||||||||||||||||||||||
| 158 | 0 | ▶ | $status->{is_my_pid} = 1 | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 159 | if $status_pid == $$; | ||||||||||||||||||||||||||||||||||
| 160 | $status->{$_} = _my_read_file("/proc/$status_pid/$_") | ||||||||||||||||||||||||||||||||||
| 161 | 0 | for ('stat','statm'); | |||||||||||||||||||||||||||||||||
| 162 | 0 | ▶ | if(-d "/proc/$status_pid/fd/") { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 163 | 0 | eval { | |||||||||||||||||||||||||||||||||
| 164 | 0 | ▶ | opendir my $dh, "/proc/$status_pid/fd/" or die; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 165 | 0 | ▶ | my @files = grep { not /^\./ } readdir $dh or die; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 166 | 0 | closedir $dh; | |||||||||||||||||||||||||||||||||
| 167 | 0 | $status->{fd_count} = scalar @files; | |||||||||||||||||||||||||||||||||
| 168 | }; | ||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||
| 170 | 0 | ▶ | if(-d "/proc/$status_pid/task/") { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 171 | 0 | eval { | |||||||||||||||||||||||||||||||||
| 172 | 0 | ▶ | opendir my $dh, "/proc/$status_pid/task/" or die; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 173 | 0 | ▶ | my @files = grep { not /^\./ } readdir $dh or die; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 174 | 0 | closedir $dh; | |||||||||||||||||||||||||||||||||
| 175 | 0 | $status->{task_count} = scalar @files; | |||||||||||||||||||||||||||||||||
| 176 | }; | ||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||
| 178 | }; | ||||||||||||||||||||||||||||||||||
| 179 | 0 | ▶ | if($@) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 180 | #all of these are ignored, because it just means the process isn't | ||||||||||||||||||||||||||||||||||
| 181 | #running | ||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||
| 185 | 0 | return $status; | |||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||
| 188 | =head2 get_task_config($task_name) | ||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||
| 190 | Return the tasks's config. | ||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||
| 192 | Example: | ||||||||||||||||||||||||||||||||||
| 193 | config => { | ||||||||||||||||||||||||||||||||||
| 194 | 'increment_by' => 8427, | ||||||||||||||||||||||||||||||||||
| 195 | 'is_external' => 1 | ||||||||||||||||||||||||||||||||||
| 196 | }; | ||||||||||||||||||||||||||||||||||
| 197 | =cut | ||||||||||||||||||||||||||||||||||
| 198 | { | ||||||||||||||||||||||||||||||||||
| 199 | 1 | my $time_slice; | |||||||||||||||||||||||||||||||||
| 200 | 1 | my $cache; | |||||||||||||||||||||||||||||||||
| 201 | 0 | ▶ | sub get_task_config { | ||||||||||||||||||||||||||||||||
|
Subroutine
get_task_config: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 202 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 203 | 0 | my $task_name = shift; | |||||||||||||||||||||||||||||||||
| 204 | 0 | my %args = @_; | |||||||||||||||||||||||||||||||||
| 205 | 0 | ▶ | if(not $time_slice) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 206 | 0 | $time_slice = {}; | |||||||||||||||||||||||||||||||||
| 207 | 0 | $cache = {}; | |||||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||||
| 209 | 0 | ▶ | $time_slice->{$task_name} = time unless $time_slice->{$task_name}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 210 | 0 | ▶ | if($cache->{$task_name} and $time_slice->{$task_name} == time) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$cache->{$task_name} and $time_slice->{$task_name} == time
MC/DC0%0/2
$cache->{$task_name} and $time_slice->{$task_name} == time
$cache->{$task_name}
$time_slice->{$task_name} == time
Truth table0%0/3
A and B
A= $cache->{$task_name}B= $time_slice->{$task_name} == time
| |||||||||||||||||||||||||||||||||||
| 211 | # print STDERR "returning cached config for $task_name\n"; | ||||||||||||||||||||||||||||||||||
| 212 | 0 | return $cache->{$task_name}; | |||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||
| 214 | 0 | $time_slice->{$task_name} = time; | |||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||
| 216 | 0 | my $config; | |||||||||||||||||||||||||||||||||
| 217 | 0 | eval { | |||||||||||||||||||||||||||||||||
| 218 | 0 | $config = $self->_read_file($task_name . '.conf'); | |||||||||||||||||||||||||||||||||
| 219 | }; | ||||||||||||||||||||||||||||||||||
| 220 | # $self->debug("get_task_config for $task_name failed: $@") if $@; | ||||||||||||||||||||||||||||||||||
| 221 | 0 | ▶ | print STDERR "get_task_config for $task_name failed: $@\n" if $@; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 222 | 0 | $cache->{$task_name} = $config; | |||||||||||||||||||||||||||||||||
| 223 | 0 | return $config; | |||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||
| 227 | =head2 get_task_state($task_name) | ||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||
| 229 | Returns the saved state | ||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||
| 231 | Example: | ||||||||||||||||||||||||||||||||||
| 232 | state => | ||||||||||||||||||||||||||||||||||
| 233 | '.multimodule' => { | ||||||||||||||||||||||||||||||||||
| 234 | 'save_ts' => 1370987685 | ||||||||||||||||||||||||||||||||||
| 235 | }, | ||||||||||||||||||||||||||||||||||
| 236 | 'most_recent' => 10246, | ||||||||||||||||||||||||||||||||||
| 237 | 'sum_increment_by' => 6585 | ||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||
| 241 | =cut | ||||||||||||||||||||||||||||||||||
| 242 | 1 | ▶ | sub get_task_state { | ||||||||||||||||||||||||||||||||
|
Subroutine
get_task_state: not called CC=3 SCAR=23.2
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 243 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 244 | 0 | my $task_name = shift; | |||||||||||||||||||||||||||||||||
| 245 | 0 | my %args = @_; | |||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||
| 247 | 0 | my $state; | |||||||||||||||||||||||||||||||||
| 248 | 0 | eval { | |||||||||||||||||||||||||||||||||
| 249 | 0 | ▶ | $state = $self->_read_file($task_name . '.state') | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 250 | or die 'no state'; | ||||||||||||||||||||||||||||||||||
| 251 | }; | ||||||||||||||||||||||||||||||||||
| 252 | 0 | ▶ | if($@) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 253 | return { | ||||||||||||||||||||||||||||||||||
| 254 | 0 | '.multimodule' => { | |||||||||||||||||||||||||||||||||
| 255 | initial_state => 1, | ||||||||||||||||||||||||||||||||||
| 256 | }, | ||||||||||||||||||||||||||||||||||
| 257 | }; | ||||||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||||
| 259 | # die "App::MultiModule::API::get_task_state failed: $@\n" if $@; | ||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||
| 261 | 0 | return $state; | |||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||
| 265 | =head2 unfailsafe_task($task_name) | ||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||
| 267 | Causes a task to no longer be failsafe | ||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||
| 269 | =cut | ||||||||||||||||||||||||||||||||||
| 270 | 0 | ▶ | sub unfailsafe_task { | ||||||||||||||||||||||||||||||||
|
Subroutine
unfailsafe_task: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 271 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 272 | 0 | my $task_name = shift; | |||||||||||||||||||||||||||||||||
| 273 | 0 | my %args = @_; | |||||||||||||||||||||||||||||||||
| 274 | 0 | ▶ | return undef unless my $state_dir = $self->{state_dir}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 275 | 0 | my $file = "$state_dir/$task_name.failsafe"; | |||||||||||||||||||||||||||||||||
| 276 | 0 | ▶ | return 1 unless -e $file; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 277 | 0 | ▶ | unlink $file | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 278 | or die "App::MultiModule::API::unfailsafe_task: unable to unlink $file: $!"; | ||||||||||||||||||||||||||||||||||
| 279 | 0 | return 1; | |||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||
| 282 | =head2 failsafe_task($task_name) | ||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||
| 284 | Marks a task as failsafed. | ||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||
| 286 | =cut | ||||||||||||||||||||||||||||||||||
| 287 | 0 | ▶ | sub failsafe_task { | ||||||||||||||||||||||||||||||||
|
Subroutine
failsafe_task: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 288 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 289 | 0 | my $task_name = shift; | |||||||||||||||||||||||||||||||||
| 290 | 0 | my $message = shift; | |||||||||||||||||||||||||||||||||
| 291 | 0 | my %args = @_; | |||||||||||||||||||||||||||||||||
| 292 | 0 | ▶ | return undef unless my $state_dir = $self->{state_dir}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 293 | 0 | ▶ | local $SIG{ALRM} = sub { die "timed out\n"; }; | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 294 | 0 | my $file = "$state_dir/$task_name.failsafe"; | |||||||||||||||||||||||||||||||||
| 295 | 0 | alarm 2; | |||||||||||||||||||||||||||||||||
| 296 | 0 | eval { | |||||||||||||||||||||||||||||||||
| 297 | 0 | ▶ | open my $fh, '>', $file | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 298 | or die "failed to open $file for writing: $!"; | ||||||||||||||||||||||||||||||||||
| 299 | 0 | ▶ | print $fh Data::Dumper::Dumper $message | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 300 | or die "failed to write to $file: $!"; | ||||||||||||||||||||||||||||||||||
| 301 | 0 | ▶ | close $fh | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 302 | or die "failed to $file: $!"; | ||||||||||||||||||||||||||||||||||
| 303 | }; | ||||||||||||||||||||||||||||||||||
| 304 | 0 | alarm 0; | |||||||||||||||||||||||||||||||||
| 305 | 0 | ▶ | die "App::MultiModule::API::failsafe_task: failed: $@" if $@; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 306 | 0 | return 1; | |||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||
| 309 | =head2 task_is_failsafe($task_name) | ||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||
| 311 | Returns 'true' if the task is failsafe | ||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||
| 313 | =cut | ||||||||||||||||||||||||||||||||||
| 314 | 0 | ▶ | sub task_is_failsafe { | ||||||||||||||||||||||||||||||||
|
Subroutine
task_is_failsafe: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 315 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 316 | 0 | my $task_name = shift; | |||||||||||||||||||||||||||||||||
| 317 | 0 | my %args = @_; | |||||||||||||||||||||||||||||||||
| 318 | 0 | ▶ | return undef unless my $state_dir = $self->{state_dir}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 319 | 0 | ▶ | return 1 if -e "$state_dir/$task_name.failsafe"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||
| 323 | =head2 save_task_status($task_name, $status) | ||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||
| 325 | Save the task status. | ||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||
| 327 | =cut | ||||||||||||||||||||||||||||||||||
| 328 | 0 | ▶ | sub save_task_status { | ||||||||||||||||||||||||||||||||
|
Subroutine
save_task_status: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 329 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 330 | 0 | my $task_name = shift; my $status = shift; | |||||||||||||||||||||||||||||||||
| 331 | 0 | my %args = @_; | |||||||||||||||||||||||||||||||||
| 332 | 0 | ▶ | $status = {} unless $status; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 333 | 0 | ▶ | if($args{no_save_pid}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 334 | 0 | delete $status->{pid}; | |||||||||||||||||||||||||||||||||
| 335 | } else { | ||||||||||||||||||||||||||||||||||
| 336 | 0 | $status->{pid} = $$; | |||||||||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||||||||
| 338 | 0 | $status->{save_ts} = time; | |||||||||||||||||||||||||||||||||
| 339 | 0 | { open my $fh, '<', "/proc/$$/cmdline"; | |||||||||||||||||||||||||||||||||
| 340 | 0 | read $fh, $status->{cmdline}, 10240; | |||||||||||||||||||||||||||||||||
| 341 | 0 | close $fh; | |||||||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||||||||
| 343 | 0 | eval { | |||||||||||||||||||||||||||||||||
| 344 | 0 | $self->_write_file($task_name . '.status', $status); | |||||||||||||||||||||||||||||||||
| 345 | }; | ||||||||||||||||||||||||||||||||||
| 346 | 0 | ▶ | print STDERR "save_task_status _write_file call failed: $@\n" if $@; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||
| 349 | =head2 save_task_state($task_name, $state) | ||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||
| 351 | Save the task state. | ||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||
| 353 | =cut | ||||||||||||||||||||||||||||||||||
| 354 | 0 | ▶ | sub save_task_state { | ||||||||||||||||||||||||||||||||
|
Subroutine
save_task_state: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 355 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 356 | 0 | my $task_name = shift;my $state = shift; | |||||||||||||||||||||||||||||||||
| 357 | 0 | my %args = @_; | |||||||||||||||||||||||||||||||||
| 358 | 0 | ▶ | $state->{'.multimodule'} = {} unless $state->{'.multimodule'}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 359 | 0 | my $m = $state->{'.multimodule'}; | |||||||||||||||||||||||||||||||||
| 360 | # if($args{no_save_pid}) { | ||||||||||||||||||||||||||||||||||
| 361 | # delete $m->{pid}; | ||||||||||||||||||||||||||||||||||
| 362 | # } else { | ||||||||||||||||||||||||||||||||||
| 363 | # $m->{pid} = $$; | ||||||||||||||||||||||||||||||||||
| 364 | # } | ||||||||||||||||||||||||||||||||||
| 365 | 0 | $m->{save_ts} = time; | |||||||||||||||||||||||||||||||||
| 366 | # { open my $fh, '<', "/proc/$$/cmdline"; | ||||||||||||||||||||||||||||||||||
| 367 | # read $fh, $m->{cmdline}, 10240; | ||||||||||||||||||||||||||||||||||
| 368 | # close $fh; | ||||||||||||||||||||||||||||||||||
| 369 | # } | ||||||||||||||||||||||||||||||||||
| 370 | 0 | eval { | |||||||||||||||||||||||||||||||||
| 371 | 0 | $self->_write_file($task_name . '.state', $state); | |||||||||||||||||||||||||||||||||
| 372 | }; | ||||||||||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||
| 375 | =head2 get_task_status_files | ||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||
| 377 | Return an array of state files. | ||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||
| 379 | =cut | ||||||||||||||||||||||||||||||||||
| 380 | 0 | ▶ | sub get_task_status_files { | ||||||||||||||||||||||||||||||||
|
Subroutine
get_task_status_files: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 381 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 382 | 0 | my %args = @_; | |||||||||||||||||||||||||||||||||
| 383 | 0 | ▶ | return undef unless my $state_dir = $self->{state_dir}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||
| 385 | 0 | my @files = (); | |||||||||||||||||||||||||||||||||
| 386 | 0 | eval { | |||||||||||||||||||||||||||||||||
| 387 | 0 | ▶ | opendir(my $dh, $state_dir) or die "failed to opendir $state_dir: $!"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 388 | 0 | foreach my $file ( | |||||||||||||||||||||||||||||||||
| 389 | 0 | ▶ | grep { not /^\./ and -f "$state_dir/$_" and /\.status$/ } | ||||||||||||||||||||||||||||||||
|
Condition0%0/6
!/^\./ && -f "$state_dir/$_" && /\.status$/
not /^\./ and -f "$state_dir/$_"
MC/DC0%0/3
!/^\./ && -f "$state_dir/$_" && /\.status$/
not /^\./
-f "$state_dir/$_"
/\.status$/
Truth table0%0/4
not(A) and B && C
A= not /^\./B= -f "$state_dir/$_"C= /\.status$/
| |||||||||||||||||||||||||||||||||||
| 390 | readdir($dh)) { | ||||||||||||||||||||||||||||||||||
| 391 | 0 | $file =~ s/\..*//; | |||||||||||||||||||||||||||||||||
| 392 | 0 | push @files, $file; | |||||||||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||||||||
| 394 | 0 | ▶ | closedir $dh or die "failed to closedir $state_dir: $!\n"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 395 | }; | ||||||||||||||||||||||||||||||||||
| 396 | 0 | return @files; | |||||||||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||
| 399 | =head2 save_task_config($task_name, $config) | ||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||
| 401 | Save the task config. | ||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||
| 403 | =cut | ||||||||||||||||||||||||||||||||||
| 404 | 0 | ▶ | sub save_task_config { | ||||||||||||||||||||||||||||||||
|
Subroutine
save_task_config: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 405 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 406 | 0 | my $task_name = shift;my $config = shift; | |||||||||||||||||||||||||||||||||
| 407 | 0 | my %args = @_; | |||||||||||||||||||||||||||||||||
| 408 | 0 | eval { | |||||||||||||||||||||||||||||||||
| 409 | 0 | $self->_write_file($task_name . '.conf', $config); | |||||||||||||||||||||||||||||||||
| 410 | }; | ||||||||||||||||||||||||||||||||||
| 411 | #$self->debug("save_task_config for $task_name failed: $@") if $@; | ||||||||||||||||||||||||||||||||||
| 412 | 0 | ▶ | print STDERR "save_task_config for $task_name failed: $@\n" if $@; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||
| 415 | =head2 send_signal($task_name, $integer_UNIX_signal) | ||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||
| 417 | Send specified signal to task. | ||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||
| 419 | =cut | ||||||||||||||||||||||||||||||||||
| 420 | 0 | ▶ | sub send_signal { | ||||||||||||||||||||||||||||||||
|
Subroutine
send_signal: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 421 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 422 | 0 | my $task_name = shift; my $signal = shift; | |||||||||||||||||||||||||||||||||
| 423 | 0 | my %args = @_; | |||||||||||||||||||||||||||||||||
| 424 | 0 | my $pid; | |||||||||||||||||||||||||||||||||
| 425 | 0 | eval { | |||||||||||||||||||||||||||||||||
| 426 | 0 | ▶ | die "undef\n" unless my $status = $self->get_task_status($task_name); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 427 | 0 | ▶ | die "undef\n" unless $pid = $status->{pid}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 428 | 0 | ▶ | die "undef\n" if $pid == $$; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 429 | }; | ||||||||||||||||||||||||||||||||||
| 430 | 0 | ▶ | print STDERR "$$: send_signal failed: $@\n" if $@; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 431 | 0 | ▶ | return undef if $@; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 432 | 0 | ▶ | return undef unless $pid; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 433 | 0 | return kill $signal, $pid; | |||||||||||||||||||||||||||||||||
| 434 | } | ||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||||
| 438 | 1 | 1; | |||||||||||||||||||||||||||||||||