| 1 | package Module::Build::Service::Fork; | ||||||||||
| 2 | { | ||||||||||
| 3 | 1 | $Module::Build::Service::Fork::VERSION = '0.91'; | |||||||||
| 4 | } | ||||||||||
| 5 | # ABSTRACT: Role for process handling in service implementations | ||||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use File::Spec qw{}; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use IPC::Run qw{start}; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use Log::Any qw{$log}; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use Moo::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 1 | requires '_build_command'; | |||||||||
| 14 | |||||||||||
| 15 | |||||||||||
| 16 | 1 | has 'bin' => (is => 'lazy'); | |||||||||
| 17 | |||||||||||
| 18 | # Search the list of directories specified in ->path for the | ||||||||||
| 19 | # executable specified in ->executable. | ||||||||||
| 20 | 0 | ▶ | sub _build_bin { | ||||||||
|
Subroutine
_build_bin: not called CC=3 SCAR=24.8
| |||||||||||
| 21 | 0 | my ($self) = @_; | |||||||||
| 22 | 0 | for my $location (@{$self->path}) { | |||||||||
| 23 | 0 | my $executable = File::Spec->catfile ($location, $self->executable); | |||||||||
| 24 | 0 | ▶ | -x $executable and return $executable; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | } | ||||||||||
| 26 | 0 | die "Couldn't locate " . $self->executable . " binary"; | |||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | |||||||||||
| 30 | has 'command' => (is => 'lazy', | ||||||||||
| 31 | 0 | ▶ | isa => sub {ref $_[0] eq "ARRAY"}); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 32 | |||||||||||
| 33 | |||||||||||
| 34 | 1 | has 'executable' => (is => 'lazy'); | |||||||||
| 35 | |||||||||||
| 36 | 0 | ▶ | sub _build_executable { | ||||||||
|
Subroutine
_build_executable: not called CC=1 SCAR=6.9
| |||||||||||
| 37 | 0 | my ($self) = @_; | |||||||||
| 38 | 0 | $self->service_name | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | |||||||||||
| 42 | has 'path' => (is => 'lazy', | ||||||||||
| 43 | 0 | ▶ | isa => sub {ref $_[0] eq "ARRAY"}); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 44 | |||||||||||
| 45 | # Specify our default list of directories to search | ||||||||||
| 46 | 0 | ▶ | sub _build_path { | ||||||||
|
Subroutine
_build_path: not called CC=1 SCAR=6.9
| |||||||||||
| 47 | 0 | ['/usr/sbin', '/usr/local/sbin']; | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 1 | has 'handle' => (is => 'rwp'); | |||||||||
| 51 | |||||||||||
| 52 | |||||||||||
| 53 | 0 | ▶ | sub start_service { | ||||||||
|
Subroutine
start_service: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 54 | 0 | my ($self) = @_; | |||||||||
| 55 | 0 | $log->tracef ("%s service starting", $self->service_name); | |||||||||
| 56 | 0 | ▶ | try { | ||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||
| 57 | 0 | my @output = (\undef); | |||||||||
| 58 | 0 | ▶ | if (my $logfile = $self->log) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 0 | $log->tracef ("%s base logfile is %s", $self->service_name, $logfile); | |||||||||
| 60 | 0 | ▶ | $logfile = File::Spec->catfile ($self->_builder->mbs_log_dir, $self->log) unless substr $logfile, 0, 1 eq '/'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 0 | push @output, '>>', $logfile, '2>', $logfile; | |||||||||
| 62 | } | ||||||||||
| 63 | 0 | $log->tracef ("%s output is %s", $self->service_name, \@output); | |||||||||
| 64 | 0 | ▶ | my $handle = start $self->command, @output or die $?; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 0 | $self->_set_handle ($handle); | |||||||||
| 66 | 0 | $log->tracef ("%s service started", $self->service_name); | |||||||||
| 67 | 0 | ▶ | } catch { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 68 | 0 | $log->criticalf ("%s service failed: %s", $self->service_name, $_); | |||||||||
| 69 | 0 | die $_; | |||||||||
| 70 | 0 | }; | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | |||||||||||
| 74 | 0 | ▶ | sub stop_service { | ||||||||
|
Subroutine
stop_service: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 75 | 0 | my ($self) = @_; | |||||||||
| 76 | 0 | $log->tracef ("%s service stopping", $self->service_name); | |||||||||
| 77 | 0 | ▶ | try { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 78 | 0 | $self->handle->kill_kill; | |||||||||
| 79 | 0 | $log->tracef ("%s service stopped", $self->service_name); | |||||||||
| 80 | 0 | 1; | |||||||||
| 81 | 0 | ▶ | } catch { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 82 | 0 | 0; | |||||||||
| 83 | 0 | }; | |||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | 1 | 1; | |||||||||
| 87 | |||||||||||
| 88 | __END__ | ||||||||||