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