System/Process.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« System/Package.pm ↑ Summary System/Route.pm »
1 #
2 # $Id$
3 #
4 # system::process Brik
5 #
6 package Metabrik::System::Process;
7 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use base qw(Metabrik::Shell::Command);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub brik_properties {
13 return {
14 revision => '$Revision$',
15 tags => [ qw(unstable) ],
16 author => 'GomoR <GomoR[at]metabrik.org>',
17 license => 'http://opensource.org/licenses/BSD-3-Clause',
18 attributes => {
19 datadir => [ qw(datadir) ],
20 force_kill => [ qw(0|1) ],
21 close_output_on_start => [ qw(0|1) ],
22 use_pidfile => [ qw(0|1) ],
23 pidfile => [ qw(file) ],
24 },
25 attributes_default => {
26 force_kill => 0,
27 close_output_on_start => 1,
28 use_pidfile => 1,
29 },
30 commands => {
31 parse_ps_output => [ qw(data) ],
32 list => [ ],
33 is_running => [ qw(process) ],
34 is_running_from_string => [ qw(string) ],
35 get_pid_from_string => [ qw(string) ],
36 get_pid_list_from_string => [ qw(string) ],
37 get_pid_list_from_regex => [ qw(regex) ],
38 get_process_info => [ qw(process) ],
39 kill => [ qw(process|pid) ],
40 start => [ qw($sub) ],
41 start_with_pidfile => [ qw($sub) ],
42 list_daemons => [ ],
43 get_latest_daemon_id => [ ],
44 kill_from_pidfile => [ qw(pidfile) ],
45 is_running_from_pidfile => [ qw(pidfile) ],
46 grep_by_name => [ qw(process_name) ],
47 get_new_pidfile => [ ],
48 get_latest_pidfile => [ ],
49 write_pidfile => [ qw(pidfile|OPTIONAL) ],
50 delete_pidfile => [ qw(pidfile) ],
51 wait_for_pidfile => [ qw(pidfile) ],
52 info_process_is_running => [ ],
53 info_process_is_not_running => [ ],
54 verbose_process_is_running => [ ],
55 verbose_process_is_not_running => [ ],
56 error_process_is_running => [ ],
57 error_process_is_not_running => [ ],
58 },
59 require_modules => {
60 'Daemon::Daemonize' => [ ],
61 'POSIX' => [ qw(:sys_wait_h) ],
62 'Time::HiRes' => [ qw(usleep) ],
63 'Metabrik::File::Find' => [ ],
64 },
65 require_binaries => {
66 ps => [ ],
67 },
68 need_packages => {
69 ubuntu => [ qw(procps) ],
70 debian => [ qw(procps) ],
71 kali => [ qw(procps) ],
72 },
73 };
74 }
75  
76 sub parse_ps_output {
77 my $self = shift;
78 my ($data) = @_;
79  
80 $self->brik_help_run_undef_arg('parse_ps_output', $data) or return;
81  
82 my @a = ();
83 my $first = shift @$data;
84 my $header = [ split(/\s+/, $first) ];
85 my $count = scalar(@$header) - 1;
86 for my $this (@$data) {
87 my @toks = split(/\s+/, $this);
88 my $h = {};
89 for my $n (0..$count) {
90 $h->{$header->[$n]} = $toks[$n];
91 }
92 my $ntoks = scalar(@toks) - 1;
93 if ($ntoks > $count) {
94 for my $n (($count+1)..$ntoks) {
95 $h->{$header->[-1]} .= ' '.$toks[$n];
96 }
97 }
98 push @a, $h;
99 }
100  
101 return \@a;
102 }
103  
104 sub list {
105 my $self = shift;
106  
107 my $res = $self->capture("ps awuxw") or return;
108  
109 return $self->parse_ps_output($res);
110 }
111  
112 sub is_running {
113 my $self = shift;
114 my ($process) = @_;
115  
116 $self->brik_help_run_undef_arg('is_running', $process) or return;
117  
118 my $list = $self->list or return;
119 for my $this (@$list) {
120 my $command = $this->{COMMAND};
121 if ($command =~ m{^\[.*\]$}) { # Example: "[migration/0]"
122 $self->log->debug("is_running: list[$command] process[$process]");
123 if ($command eq $process) {
124 return 1;
125 }
126 }
127 else {
128 my @toks = split(/\s+/, $command);
129 $toks[0] =~ s/^.*\/(.*?)$/$1/;
130 $self->log->debug("is_running: list[$toks[0]] process[$process]");
131 if ($toks[0] eq $process) {
132 return 1;
133 }
134 }
135 }
136  
137 return 0;
138 }
139  
140 sub is_running_from_string {
141 my $self = shift;
142 my ($string) = @_;
143  
144 $self->brik_help_run_undef_arg('is_running_from_string', $string) or return;
145  
146 my $list = $self->list or return;
147 for my $this (@$list) {
148 my $command = $this->{COMMAND};
149 if ($command =~ m{$string}i) {
150 return 1;
151 }
152 }
153  
154 return 0;
155 }
156  
157 sub get_pid_from_string {
158 my $self = shift;
159 my ($string) = @_;
160  
161 $self->brik_help_run_undef_arg('get_pid_from_string', $string) or return;
162  
163 my $list = $self->list or return;
164 for my $this (@$list) {
165 my $command = $this->{COMMAND};
166 if ($command =~ m{$string}i) {
167 return $this->{PID};
168 }
169 }
170  
171 return 0;
172 }
173  
174 sub get_pid_list_from_string {
175 my $self = shift;
176 my ($string) = @_;
177  
178 $self->brik_help_run_undef_arg('get_pid_list_from_string', $string)
179 or return;
180  
181 my @pids = ();
182  
183 my $list = $self->list or return;
184 for my $this (@$list) {
185 my $command = $this->{COMMAND};
186 if ($command =~ m{$string}i) {
187 push @pids, $this->{PID};
188 }
189 }
190  
191 return \@pids;
192 }
193  
194 sub get_pid_list_from_regex {
195 my $self = shift;
196 my ($regex) = @_;
197  
198 $self->brik_help_run_undef_arg('get_pid_list_from_regex', $regex)
199 or return;
200  
201 my @pids = ();
202  
203 my $list = $self->list or return;
204 for my $this (@$list) {
205 my $command = $this->{COMMAND};
206 if ($command =~ m{$regex}i) {
207 push @pids, $this->{PID};
208 }
209 }
210  
211 return \@pids;
212 }
213  
214 sub get_process_info {
215 my $self = shift;
216 my ($process) = @_;
217  
218 $self->brik_help_run_undef_arg('get_process_info', $process) or return;
219  
220 my @results = ();
221 my $list = $self->list or return;
222 for my $this (@$list) {
223 my $command = $this->{COMMAND} or next;
224 my @toks = split(/\s+/, $command);
225 $toks[0] =~ s/^.*\/(.*?)$/$1/;
226 if ($toks[0] eq $process) {
227 push @results, $this;
228 }
229 }
230  
231 return \@results;
232 }
233  
234 sub kill {
235 my $self = shift;
236 my ($process) = @_;
237  
238 $self->brik_help_run_undef_arg('kill', $process) or return;
239  
240 my $signal = $self->force_kill ? 'KILL' : 'TERM';
241  
242 if ($process =~ /^\d+$/) {
243 kill($signal, $process);
244 my $kid = waitpid(-1, POSIX::WNOHANG());
245 }
246 else {
247 my $list = $self->get_process_info($process) or return;
248 for my $this (@$list) {
249 kill($signal, $this->{PID});
250 my $kid = waitpid(-1, POSIX::WNOHANG());
251 }
252 }
253  
254 return 1;
255 }
256  
257 #
258 # start process sub from user program has to call write_pidfile Command to create the pidfile.
259 # Then parent process can use wait_for_pidfile Command with the $pidfile returned from
260 # start Command to wait for the process to really start.
261 #
262 sub start {
263 my $self = shift;
264 my ($sub) = @_;
265  
266 my %opts = (
267 close => $self->close_output_on_start,
268 );
269  
270 my $r;
271 # Daemonize the given subroutine
272 if (defined($sub)) {
273 $r = Daemon::Daemonize->daemonize(
274 %opts,
275 run => $sub,
276 );
277 }
278 # Or myself.
279 else {
280 $r = Daemon::Daemonize->daemonize(
281 %opts,
282 );
283 }
284  
285 if ($self->use_pidfile) {
286 my $pidfile = $self->get_new_pidfile or return;
287  
288 $self->log->verbose("start: new daemon started with pidfile [$pidfile]");
289  
290 return $pidfile;
291 }
292  
293 return 1;
294 }
295  
296 sub start_with_pidfile {
297 my $self = shift;
298 my ($sub) = @_;
299  
300 my %opts = (
301 close => $self->close_output_on_start,
302 );
303  
304 my $pidfile = $self->get_new_pidfile or return;
305  
306 my $r;
307 # Daemonize the given subroutine
308 if (defined($sub)) {
309 $r = Daemon::Daemonize->daemonize(
310 %opts,
311 run => sub {
312 $self->write_pidfile($pidfile);
313 &$sub();
314 },
315 );
316 }
317 # Or myself.
318 else {
319 $r = Daemon::Daemonize->daemonize(
320 %opts,
321 );
322 }
323  
324 $self->log->verbose("start: new daemon started with pidfile [$pidfile]");
325  
326 return $pidfile;
327 }
328  
329 sub list_daemons {
330 my $self = shift;
331  
332 my $datadir = $self->datadir;
333  
334 my $ff = Metabrik::File::Find->new_from_brik_init($self) or return;
335 my $list = $ff->files($datadir, 'daemonpid\.\d+');
336  
337 my %daemons = ();
338 for (@$list) {
339 my ($id) = $_ =~ m{\.(\d+)$};
340 my $pid = Daemon::Daemonize->read_pidfile($_) or next;
341 $daemons{$id} = { file => $_, pid => $pid };
342 }
343  
344 return \%daemons;
345 }
346  
347 sub get_latest_daemon_id {
348 my $self = shift;
349  
350 my $list = $self->list_daemons or return;
351  
352 my $id = 0;
353 for (keys %$list) {
354 if ($_ > $id) {
355 $id = $_;
356 }
357 }
358  
359 return $id;
360 }
361  
362 sub kill_from_pidfile {
363 my $self = shift;
364 my ($pidfile) = @_;
365  
366 $self->brik_help_run_undef_arg('kill_from_pidfile', $pidfile) or return;
367 $self->brik_help_run_file_not_found('kill_from_pidfile', $pidfile) or return;
368  
369 if (my $pid = Daemon::Daemonize->check_pidfile($pidfile)) {
370 $self->log->verbose("kill_from_pidfile: file[$pidfile] and pid[$pid]");
371 $self->kill($pid);
372 Daemon::Daemonize->delete_pidfile($pidfile);
373 }
374  
375 return 1;
376 }
377  
378 sub is_running_from_pidfile {
379 my $self = shift;
380 my ($pidfile) = @_;
381  
382 $self->brik_help_run_undef_arg('is_running_from_pidfile', $pidfile) or return;
383  
384 # Not file found, so probably not running
385 if (! -f $pidfile) {
386 return 0;
387 }
388  
389 if (my $pid = Daemon::Daemonize->check_pidfile($pidfile)) {
390 $self->log->debug("is_running_from_pidfile: yes");
391 return 1;
392 }
393  
394 $self->log->debug("is_running_from_pidfile: no");
395  
396 return 0;
397 }
398  
399 sub grep_by_name {
400 my $self = shift;
401 my ($process_name) = @_;
402  
403 $self->brik_help_run_undef_arg('grep_by_name', $process_name) or return;
404  
405 my $list = $self->list or return;
406 for my $p (@$list) {
407 if (lc($p->{COMMAND}) =~ m{$process_name}i) {
408 return $p;
409 }
410 }
411  
412 return 0;
413 }
414  
415 sub get_new_pidfile {
416 my $self = shift;
417  
418 # Use provided one.
419 my $pidfile = $self->pidfile;
420 if (defined($pidfile)) {
421 return $pidfile;
422 }
423  
424 my $id = $self->get_latest_daemon_id;
425 defined($id) ? $id++ : ($id = 1);
426 $pidfile = sprintf("%s/daemonpid.%05d", $self->datadir, $id);
427  
428 return $pidfile;
429 }
430  
431 sub get_latest_pidfile {
432 my $self = shift;
433  
434 # Return user provided one.
435 my $pidfile = $self->pidfile;
436 if (defined($pidfile)) {
437 return $pidfile;
438 }
439  
440 my $id = $self->get_latest_daemon_id;
441 if (defined($id)) {
442 $pidfile = sprintf("%s/daemonpid.%05d", $self->datadir, $id);
443 }
444 else {
445 return $self->log->error("get_latest_pidfile: no pidfile found");
446 }
447  
448 return $pidfile;
449 }
450  
451 #
452 # To be called by a sub used by start Command
453 #
454 sub write_pidfile {
455 my $self = shift;
456  
457 my $pidfile = $self->get_new_pidfile or return;
458  
459 my $pid = $$;
460  
461 my $r = Daemon::Daemonize->write_pidfile($pidfile, $pid);
462 if (! defined($r)) {
463 return $self->log->erro("write_pidfile: failed to write pidfile [$pidfile]: $!");
464 }
465  
466 return $pid;
467 }
468  
469 #
470 # To be used by parent process
471 #
472 sub delete_pidfile {
473 my $self = shift;
474 my ($pidfile) = @_;
475  
476 $self->brik_help_run_undef_arg('delete_pidfile', $pidfile) or return;
477  
478 if (! -f $pidfile) {
479 # Nothing to delete
480 return 0;
481 }
482  
483 my $r = Daemon::Daemonize->delete_pidfile($pidfile);
484 if (! defined($r)) {
485 return $self->log->erro("delete_pidfile: failed to delete pidfile [$pidfile]: $!");
486 }
487  
488 return 1;
489 }
490  
491 # XXX: Move to system::file and use a helper here
492 #
493 # To be used by parent process
494 #
495 sub wait_for_pidfile {
496 my $self = shift;
497 my ($pidfile) = @_;
498  
499 $self->brik_help_run_undef_arg('wait_pidfile', $pidfile) or return;
500  
501 my $found = 0;
502 # 50 * 100 ms = 5s
503 for (0..49) {
504 if (-e $pidfile) {
505 $found++;
506 last;
507 }
508 Time::HiRes::usleep(100_000); # 100_000us => 100ms => 0.1s
509 # 0.1s * 50 = 5s
510 }
511  
512 return $found;
513 }
514  
515 sub info_process_is_running {
516 my $self = shift;
517  
518 return $self->log->info("process is running");
519 }
520  
521 sub info_process_is_not_running {
522 my $self = shift;
523  
524 return $self->log->info("process is NOT running");
525 }
526  
527 sub verbose_process_is_running {
528 my $self = shift;
529  
530 return $self->log->verbose("process is running");
531 }
532  
533 sub verbose_process_is_not_running {
534 my $self = shift;
535  
536 return $self->log->verbose("process is NOT running");
537 }
538  
539 sub error_process_is_running {
540 my $self = shift;
541  
542 return $self->log->error("process is running");
543 }
544  
545 sub error_process_is_not_running {
546 my $self = shift;
547  
548 return $self->log->error("process is NOT running");
549 }
550  
551 # XXX: Broken.
552 sub _brik_fini {
553 my $self = shift;
554  
555 my $pidfile = $self->get_latest_pidfile;
556 if (-f $pidfile) {
557 $self->delete_pidfile($pidfile);
558 }
559  
560 return $self->SUPER::brik_fini;
561 }
562  
563 1;
564  
565 __END__
« System/Package.pm ↑ Summary System/Route.pm »