| 1 | package Ubic::Watchdog; | ||||||||||
| 2 | 97 | $Ubic::Watchdog::VERSION = '1.59'; | |||||||||
| 3 | 97 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 97 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | # ABSTRACT: watchdog code | ||||||||||
| 7 | |||||||||||
| 8 | |||||||||||
| 9 | 97 | ▶ | use POSIX; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 97 | ▶ | use IO::Handle; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 97 | ▶ | use Params::Validate qw(:all); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 97 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 97 | ▶ | use List::MoreUtils qw(any); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 97 | ▶ | use Ubic; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 97 | ▶ | use Ubic::Logger; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | sub run { | ||||||||||
| 19 | 92 | ▶ | my $class = shift; | ||||||||
|
Subroutine
run: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 20 | 92 | my $options = validate(@_, { | |||||||||
| 21 | glob_filter => { type => ARRAYREF, default => [] }, | ||||||||||
| 22 | compile_timeout => { type => SCALAR, regex => qr/^\d+$/ }, | ||||||||||
| 23 | verbose => { type => SCALAR|UNDEF }, | ||||||||||
| 24 | }); | ||||||||||
| 25 | |||||||||||
| 26 | 92 | my @filter; | |||||||||
| 27 | { | ||||||||||
| 28 | 92 | for my $arg (@{ $options->{glob_filter} }) { | |||||||||
| 29 | 29 | ▶ | $arg =~ /^[*\w.-]+$/ or die "Invalid argument '$arg', expected service name or shell-style glob"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 28 | $arg =~ s/\./\\./g; | |||||||||
| 31 | 28 | $arg =~ s/\*/.*/g; | |||||||||
| 32 | 28 | push @filter, qr/^$arg$/; | |||||||||
| 33 | } | ||||||||||
| 34 | } | ||||||||||
| 35 | 91 | ▶ | $options->{filter} = \@filter if @filter; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 91 | delete $options->{glob_filter}; | |||||||||
| 37 | |||||||||||
| 38 | 91 | my $self = bless $options => $class; | |||||||||
| 39 | |||||||||||
| 40 | 91 | my @services = $self->load_services(Ubic->root_service); | |||||||||
| 41 | 91 | $self->check_all(@services); | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | sub match($$) { | ||||||||||
| 45 | 182 | ▶ | my ($name, $filter) = @_; | ||||||||
|
Subroutine
match: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 46 | 182 | ▶ | do { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 262 | ▶ | return 1 if $name =~ $filter; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | } while ($name =~ s/\.[^.]+$//); | ||||||||||
| 49 | 142 | return; | |||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | sub load_services { | ||||||||||
| 53 | 247 | ▶ | my $self = shift; | ||||||||
|
Subroutine
load_services: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 54 | 247 | my ($parent) = @_; | |||||||||
| 55 | 247 | alarm($self->{compile_timeout}); | |||||||||
| 56 | $SIG{ALRM} = sub { | ||||||||||
| 57 | 3 | ▶ | die "Couldn't compile $parent services in $self->{compile_timeout} seconds"; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 58 | 247 | }; | |||||||||
| 59 | 247 | my @services = $parent->services; | |||||||||
| 60 | 247 | alarm(0); | |||||||||
| 61 | 247 | return @services; | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | sub check_all { | ||||||||||
| 65 | 247 | ▶ | my $self = shift; | ||||||||
|
Subroutine
check_all: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||
| 66 | 247 | my @services = @_; | |||||||||
| 67 | 247 | for my $service (@services) { | |||||||||
| 68 | 768 | my $name = $service->full_name; | |||||||||
| 69 | 768 | ▶ | if ($service->isa('Ubic::Multiservice')) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 156 | ▶ | INFO("$name is multiservice, checking subservices") if $self->{verbose}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 156 | $self->check_all($self->load_services($service)); | |||||||||
| 72 | 110 | next; | |||||||||
| 73 | } | ||||||||||
| 74 | 612 | ▶ | if ($self->{filter}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | 134 | ▶ | next unless any { match($name, $_) } @{ $self->{filter} }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | # trying to get logs a little bit more ordered | ||||||||||
| 79 | 518 | STDOUT->flush; | |||||||||
| 80 | 518 | STDERR->flush; | |||||||||
| 81 | |||||||||||
| 82 | 518 | my $child = fork; | |||||||||
| 83 | 518 | ▶ | unless (defined $child) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 0 | die "fork failed"; | |||||||||
| 85 | } | ||||||||||
| 86 | 518 | ▶ | unless ($child) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 78 | POSIX::setsid; # so we could kill this watchdog and its children safely later | |||||||||
| 88 | 78 | $self->check($service); | |||||||||
| 89 | 77 | exit; | |||||||||
| 90 | } | ||||||||||
| 91 | } | ||||||||||
| 92 | 123 | ▶ | 1 while wait() > 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | 123 | return; | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | sub check($) { | ||||||||||
| 97 | 78 | ▶ | my $self = shift; | ||||||||
|
Subroutine
check: called CC=3 SCAR=11.6
Pod
documented
| |||||||||||
| 98 | 78 | my $service = shift; | |||||||||
| 99 | 78 | my $name = $service->full_name; | |||||||||
| 100 | 78 | ▶ | if ($self->{verbose}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | 26 | INFO("Checking $name"); | |||||||||
| 102 | } | ||||||||||
| 103 | 78 | $0 = "ubic-watchdog $name"; | |||||||||
| 104 | |||||||||||
| 105 | try { | ||||||||||
| 106 | 78 | ▶ | alarm($service->check_timeout); | ||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=23.5
| |||||||||||
| 107 | |||||||||||
| 108 | # TODO - do additional fork, so that if service code overrides SIG{ALRM} or resets alarm(), watchdog still will finish in time | ||||||||||
| 109 | $SIG{ALRM} = sub { | ||||||||||
| 110 | 0 | ▶ | ERROR("$name check_timeout exceeded"); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 111 | 0 | STDOUT->flush; | |||||||||
| 112 | 0 | STDERR->flush; | |||||||||
| 113 | 0 | kill -9 => $$; # suicide | |||||||||
| 114 | 0 | ERROR "kill sent, still alive"; # should never happen, we called setsid earlier | |||||||||
| 115 | 78 | }; | |||||||||
| 116 | |||||||||||
| 117 | # permanently use service credentials | ||||||||||
| 118 | # this line optimizes the number of fork calls - future status/restart calls would perform forked_call() otherwise | ||||||||||
| 119 | 78 | Ubic::Credentials->new( service => $service )->set; | |||||||||
| 120 | |||||||||||
| 121 | # so we don't need access guard for this lock | ||||||||||
| 122 | 78 | my $watchdog_lock = Ubic::SingletonLock->new(Ubic->get_data_dir()."/watchdog/lock/".$name, { blocking => 0 }); | |||||||||
| 123 | |||||||||||
| 124 | 78 | ▶ | unless ($watchdog_lock) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 125 | 0 | ▶ | if ($self->{verbose}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 126 | 0 | INFO "$name is locked by another watchdog process"; | |||||||||
| 127 | } | ||||||||||
| 128 | 0 | return; | |||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | 78 | my $lock = Ubic->lock($name); | |||||||||
| 132 | 78 | ▶ | unless (Ubic->is_enabled($name)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 133 | 74 | ▶ | INFO("$name disabled") if $self->{verbose}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 134 | 74 | return; | |||||||||
| 135 | } | ||||||||||
| 136 | |||||||||||
| 137 | 4 | my $cached_status = Ubic->cached_status($name); | |||||||||
| 138 | 4 | my $status = Ubic->status($name); | |||||||||
| 139 | 4 | ▶ | unless ($status->status eq 'running') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 140 | # following code can throw an exception, so we want to cache invalid status immediately | ||||||||||
| 141 | 3 | Ubic->set_cached_status($name, $status); | |||||||||
| 142 | |||||||||||
| 143 | 3 | ▶ | if ($cached_status eq "autostarting") { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 144 | 0 | INFO("$name is autostarting"); | |||||||||
| 145 | } | ||||||||||
| 146 | else { | ||||||||||
| 147 | 3 | ERROR("$name status is '$status', restarting"); | |||||||||
| 148 | } | ||||||||||
| 149 | |||||||||||
| 150 | 3 | Ubic->restart($name); | |||||||||
| 151 | |||||||||||
| 152 | # This is a precaution against services with wrong start/status logic. | ||||||||||
| 153 | 2 | $status = Ubic->status($name); | |||||||||
| 154 | 2 | ▶ | if ($status->status ne 'running') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 155 | 0 | INFO("$name started, but status is still '$status'"); | |||||||||
| 156 | } | ||||||||||
| 157 | } | ||||||||||
| 158 | |||||||||||
| 159 | 3 | alarm(0); | |||||||||
| 160 | 3 | Ubic->set_cached_status($name, $status); # if service's start implementation is invalid, ubic-watchdog will restart it every minute, so be careful | |||||||||
| 161 | } | ||||||||||
| 162 | catch { | ||||||||||
| 163 | 0 | ▶ | ERROR("Failed to revive $name: $_"); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 164 | 78 | }; | |||||||||
| 165 | |||||||||||
| 166 | 77 | ▶ | INFO("$name checked") if $self->{verbose}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 167 | } | ||||||||||
| 168 | |||||||||||
| 169 | |||||||||||
| 170 | 97 | 1; | |||||||||
| 171 | |||||||||||
| 172 | __END__ | ||||||||||