line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::EventStreamr::Roles::ProcessControl; |
2
|
|
|
|
|
|
|
|
3
|
10
|
|
|
10
|
|
91520
|
use v5.010; |
|
10
|
|
|
|
|
26
|
|
|
10
|
|
|
|
|
361
|
|
4
|
10
|
|
|
10
|
|
32
|
use strict; |
|
10
|
|
|
|
|
13
|
|
|
10
|
|
|
|
|
205
|
|
5
|
10
|
|
|
10
|
|
32
|
use warnings; |
|
10
|
|
|
|
|
10
|
|
|
10
|
|
|
|
|
232
|
|
6
|
10
|
|
|
10
|
|
38
|
use Method::Signatures 20140224; # libmethod-signatures-perl |
|
10
|
|
|
|
|
259
|
|
|
10
|
|
|
|
|
58
|
|
7
|
10
|
|
|
10
|
|
4397
|
use Proc::Daemon; # libproc-daemon-perl |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
use Proc::ProcessTable; # libproc-processtable-perl |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use Moo::Role; # libmoo-perl |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# ABSTRACT: A process role |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = '0.4'; # VERSION: Generated by DZP::OurPkg:Version |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
requires 'cmd','id'; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
has 'pid' => ( is => 'rw', lazy => 1, builder => 1, clearer => 'clear_pid' ); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
method _build_pid() { |
22
|
|
|
|
|
|
|
my $pt = Proc::ProcessTable->new; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# Sometimes our processes report a different command than |
25
|
|
|
|
|
|
|
# how they were started. Also they often spawn a new detached |
26
|
|
|
|
|
|
|
# process, so we can't grab the pid from Proc::Daemon::Init. |
27
|
|
|
|
|
|
|
my $regex = $self->cmd_regex ? $self->cmd_regex : $self->cmd; |
28
|
|
|
|
|
|
|
my @procs = grep { $_->cmndline =~ /$regex/ } @{ $pt->table }; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
if (@procs) { |
31
|
|
|
|
|
|
|
$self->info("Process ".$self->id." found with pid=".$procs[0]->pid); |
32
|
|
|
|
|
|
|
return $procs[0]->pid; |
33
|
|
|
|
|
|
|
} else { |
34
|
|
|
|
|
|
|
return 0; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
method start() { |
40
|
|
|
|
|
|
|
my %proc_opts = ( exec_command => $self->cmd ); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# Proc::Daemon will exit parent when started in Void |
43
|
|
|
|
|
|
|
my $state = Proc::Daemon::Init( \%proc_opts ); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
method stop() { |
48
|
|
|
|
|
|
|
if ($self->pid) { |
49
|
|
|
|
|
|
|
kill 9, $self->pid; |
50
|
|
|
|
|
|
|
$self->clear_pid; |
51
|
|
|
|
|
|
|
return 1; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
return 0; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
method running() { |
58
|
|
|
|
|
|
|
#TODO: pid 0 returns true. Should rework the logic here. |
59
|
|
|
|
|
|
|
if ( ! $self->pid ) { |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
} elsif ( kill 0, $self->pid ) { |
62
|
|
|
|
|
|
|
return 1; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
$self->clear_pid; |
65
|
|
|
|
|
|
|
return 0; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
with('App::EventStreamr::Roles::Logger'); |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
1; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
__END__ |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=pod |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=encoding UTF-8 |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head1 NAME |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
App::EventStreamr::Roles::ProcessControl - A process role |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head1 VERSION |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
version 0.4 |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head1 SYNOPSIS |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
This is a role that provides 'start', 'stop' and 'running' methods. |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head1 DESCRIPTION |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
This is a Role that can be consumed to provide process operations |
93
|
|
|
|
|
|
|
used throughout EventStreamr. |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
It requires a 'cmd' attribute and will utilise an optional |
96
|
|
|
|
|
|
|
'cmd_regex' if one exists. |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head1 METHODS |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head2 start |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
$proc->start; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Forks the command and returns. |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head2 stop |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
$proc->stop; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
Stops the running process if it is found. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head2 running |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
$proc->running; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
Returns 1 if running, 0 if not. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head1 AUTHOR |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Leon Wright < techman@cpan.org > |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
This software is Copyright (c) 2014 by Leon Wright. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
This is free software, licensed under: |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
The GNU Affero General Public License, Version 3, November 2007 |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=cut |