| 1 | package Ubic::Service::Skeleton; | ||||||||||||||||||||||||||||
| 2 | 150 | $Ubic::Service::Skeleton::VERSION = '1.59'; | |||||||||||||||||||||||||||
| 3 | 150 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 150 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | # ABSTRACT: skeleton of any service with common start/stop logic | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 150 | ▶ | use Ubic::Result qw(result); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 150 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 150 | ▶ | use Time::HiRes qw(sleep); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 150 | ▶ | use Ubic::Service::Utils qw(wait_for_status); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 150 | ▶ | use parent qw(Ubic::Service); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | sub status { | ||||||||||||||||||||||||||||
| 16 | 263 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
status: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 263 | my $result = $self->status_impl; | |||||||||||||||||||||||||||
| 18 | 259 | ▶ | $result ||= 'unknown'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$result ||= "unknown"
MC/DC100%1/1
$result ||= "unknown"
$result
Truth table100%2/2
A
A= $result
| |||||||||||||||||||||||||||||
| 19 | 259 | $result = result($result); | |||||||||||||||||||||||||||
| 20 | 259 | return $result; | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | sub start { | ||||||||||||||||||||||||||||
| 24 | 61 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
start: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 61 | my $status = $self->status; | |||||||||||||||||||||||||||
| 27 | 61 | ▶ | if ($status->status eq 'running') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 28 | 2 | return 'already running'; # TODO - update $status field instead? | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | elsif ($status->status eq 'not running') { | ||||||||||||||||||||||||||||
| 31 | 57 | return $self->_do_start; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | elsif ($status->status eq 'broken') { | ||||||||||||||||||||||||||||
| 34 | # checks inside _do_start and _do_stop guarantee correct status | ||||||||||||||||||||||||||||
| 35 | 2 | $self->_do_stop; | |||||||||||||||||||||||||||
| 36 | 1 | return $self->_do_start; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | else { | ||||||||||||||||||||||||||||
| 39 | 0 | die result('unknown', "wrong status '$status'"); | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | sub stop { | ||||||||||||||||||||||||||||
| 44 | 68 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
stop: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 68 | my $status = $self->status; | |||||||||||||||||||||||||||
| 47 | 64 | ▶ | if ($status->status eq 'not running') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 23 | return 'not running'; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 41 | return $self->_do_stop; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | sub status_impl { | ||||||||||||||||||||||||||||
| 55 | 0 | ▶ | die 'not implemented'; | ||||||||||||||||||||||||||
|
Subroutine
status_impl: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | sub start_impl { | ||||||||||||||||||||||||||||
| 59 | 0 | ▶ | die 'not implemented'; | ||||||||||||||||||||||||||
|
Subroutine
start_impl: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | sub stop_impl { | ||||||||||||||||||||||||||||
| 63 | 0 | ▶ | die 'not implemented'; | ||||||||||||||||||||||||||
|
Subroutine
stop_impl: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | sub timeout_options { | ||||||||||||||||||||||||||||
| 67 | 66 | ▶ | return {}; | ||||||||||||||||||||||||||
|
Subroutine
timeout_options: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | ##### internal methods ###### | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | sub _do_start { | ||||||||||||||||||||||||||||
| 74 | 58 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_do_start: called CC=9 SCAR=23.2
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 58 | my $status; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 58 | my $start_result = $self->start_impl; | |||||||||||||||||||||||||||
| 79 | 41 | ▶ | if (blessed($start_result) and $start_result->isa('Ubic::Result::Class')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($start_result) and $start_result->isa("Ubic::Result::Class")
MC/DC50%1/2
builtin::blessed($start_result) and $start_result->isa("Ubic::Result::Class")
builtin::blessed($start_result)
$start_result->isa("Ubic::Result::Class")
Truth table66%2/3
A and B
A= builtin::blessed($start_result)B= $start_result->isa("Ubic::Result::Class")
| |||||||||||||||||||||||||||||
| 80 | 2 | $status = $start_result; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 41 | ▶ | if (not $status or $status->type eq 'starting') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $status or $status->type eq "starting"
MC/DC50%1/2
not $status or $status->type eq "starting"
not $status
$status->type eq "starting"
Truth table66%2/3
not(A) or B
A= not $statusB= $status->type eq "starting"
| |||||||||||||||||||||||||||||
| 84 | $status = wait_for_status({ | ||||||||||||||||||||||||||||
| 85 | service => $self, | ||||||||||||||||||||||||||||
| 86 | expect_status => ['running', 'not running'], | ||||||||||||||||||||||||||||
| 87 | 39 | ▶ | %{ $self->timeout_options->{start} || {} }, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | }); | ||||||||||||||||||||||||||||
| 89 | 39 | ▶ | if ($status->status eq 'running') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 39 | $status->type('started'); # fake status to report correct action (hopefully) | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 41 | ▶ | if (not $status) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | die result('unknown', 'no result'); | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | 41 | ▶ | if ($status->status eq 'running') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 41 | return $status; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | else { | ||||||||||||||||||||||||||||
| 101 | 0 | die result($status, 'start failed'); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | sub _do_stop { | ||||||||||||||||||||||||||||
| 106 | 43 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_do_stop: called CC=9 SCAR=22.3
| |||||||||||||||||||||||||||||
| 107 | 43 | my $status; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 43 | my $stop_result = $self->stop_impl; | |||||||||||||||||||||||||||
| 110 | 42 | ▶ | if (blessed($stop_result) and $stop_result->isa('Ubic::Result::Class')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($stop_result) and $stop_result->isa("Ubic::Result::Class")
MC/DC50%1/2
builtin::blessed($stop_result) and $stop_result->isa("Ubic::Result::Class")
builtin::blessed($stop_result)
$stop_result->isa("Ubic::Result::Class")
Truth table66%2/3
A and B
A= builtin::blessed($stop_result)B= $stop_result->isa("Ubic::Result::Class")
| |||||||||||||||||||||||||||||
| 111 | 2 | $status = $stop_result; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 42 | ▶ | if (not $status or $status->type eq 'stopping') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $status or $status->type eq "stopping"
MC/DC50%1/2
not $status or $status->type eq "stopping"
not $status
$status->type eq "stopping"
Truth table66%2/3
not(A) or B
A= not $statusB= $status->type eq "stopping"
| |||||||||||||||||||||||||||||
| 115 | $status = wait_for_status({ | ||||||||||||||||||||||||||||
| 116 | service => $self, | ||||||||||||||||||||||||||||
| 117 | expect_status => ['not running'], | ||||||||||||||||||||||||||||
| 118 | 40 | ▶ | %{ $self->timeout_options->{stop} || {} }, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | }); | ||||||||||||||||||||||||||||
| 120 | 40 | ▶ | if ($status->status eq 'not running') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 38 | $status->type('stopped'); # fake status to report correct action (hopefully) | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 42 | ▶ | if (not $status) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 0 | die result('unknown', 'no result'); | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | 42 | ▶ | if ($status->status eq 'not running') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 40 | return $status; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | else { | ||||||||||||||||||||||||||||
| 132 | 2 | die result($status, 'stop failed'); | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 150 | 1; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | __END__ | ||||||||||||||||||||||||||||