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.60';
3 23     23   6471 use strict;
  23         26  
  23         525  
4 23     23   71 use warnings;
  23         20  
  23         533  
5              
6             # ABSTRACT: skeleton of any service with common start/stop logic
7              
8 23     23   320 use Ubic::Result qw(result);
  23         21  
  23         1191  
9 23     23   91 use Scalar::Util qw(blessed);
  23         38  
  23         913  
10 23     23   5843 use Time::HiRes qw(sleep);
  23         14151  
  23         106  
11 23     23   8713 use Ubic::Service::Utils qw(wait_for_status);
  23         28  
  23         1133  
12              
13 23     23   104 use parent qw(Ubic::Service);
  23         25  
  23         78  
14              
15             sub status {
16 131     131 1 308 my ($self) = @_;
17 131         527 my $result = $self->status_impl;
18 131   100     1805 $result ||= 'unknown';
19 131         457 $result = result($result);
20 131         658 return $result;
21             }
22              
23             sub start {
24 32     32 1 888 my ($self) = @_;
25              
26 32         126 my $status = $self->status;
27 32 100       98 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         158 return $self->_do_start;
32             }
33             elsif ($status->status eq 'broken') {
34             # checks inside _do_start and _do_stop guarantee correct status
35 2         18 $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 2000339 my ($self) = @_;
45              
46 23         88 my $status = $self->status;
47 23 100       82 if ($status->status eq 'not running') {
48 2         9 return 'not running';
49             }
50              
51 21         188 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 749 return {};
68             }
69              
70              
71             ##### internal methods ######
72              
73             sub _do_start {
74 29     29   52 my ($self) = @_;
75              
76 29         33 my $status;
77              
78 29         112 my $start_result = $self->start_impl;
79 23 100 66     390 if (blessed($start_result) and $start_result->isa('Ubic::Result::Class')) {
80 2         3 $status = $start_result;
81             }
82              
83 23 100 66     109 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       202 %{ $self->timeout_options->{start} || {} },
  21         543  
88             });
89 21 50       82 if ($status->status eq 'running') {
90 21         84 $status->type('started'); # fake status to report correct action (hopefully)
91             }
92             }
93              
94 23 50       214 if (not $status) {
95 0         0 die result('unknown', 'no result');
96             }
97 23 50       102 if ($status->status eq 'running') {
98 23         343 return $status;
99             }
100             else {
101 0         0 die result($status, 'start failed');
102             }
103             }
104              
105             sub _do_stop {
106 23     23   40 my ($self) = @_;
107 23         26 my $status;
108              
109 23         254 my $stop_result = $self->stop_impl;
110 22 100 66     642 if (blessed($stop_result) and $stop_result->isa('Ubic::Result::Class')) {
111 2         3 $status = $stop_result;
112             }
113              
114 22 100 66     88 if (not $status or $status->type eq 'stopping') {
115             $status = wait_for_status({
116             service => $self,
117             expect_status => ['not running'],
118 20 50       129 %{ $self->timeout_options->{stop} || {} },
  20         88  
119             });
120 20 50       74 if ($status->status eq 'not running') {
121 20         64 $status->type('stopped'); # fake status to report correct action (hopefully)
122             }
123             }
124              
125 22 50       180 if (not $status) {
126 0         0 die result('unknown', 'no result');
127             }
128 22 50       96 if ($status->status eq 'not running') {
129 22         111 return $status;
130             }
131             else {
132 0           die result($status, 'stop failed');
133             }
134             }
135              
136             1;
137              
138             __END__