blib/lib/Ubic/Service/Skeleton.pm untested

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