File Coverage

lib/Ubic/Service/Skeleton.pm
Criterion Covered Total %
statement 65 73 89.0
branch 23 32 71.8
condition 10 14 71.4
subroutine 13 16 81.2
pod 7 7 100.0
total 118 142 83.1


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