| 1 | package Ubic::Service::SimpleDaemon; | ||
| 2 | $Ubic::Service::SimpleDaemon::VERSION = '1.59'; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | # ABSTRACT: service module for daemonizing any binary | ||
| 7 | |||
| 8 | |||
| 9 | use parent qw(Ubic::Service::Skeleton); | ||
| 10 | |||
| 11 | use Ubic::Daemon qw(start_daemon stop_daemon check_daemon); | ||
| 12 | use Ubic::Result qw(result); | ||
| 13 | use Ubic::Settings; | ||
| 14 | |||
| 15 | use Params::Validate qw(:all); | ||
| 16 | |||
| 17 | # Beware - this code will ignore any overrides if you're using custom Ubic->new(...) objects | ||
| 18 | our $PID_DIR; | ||
| 19 | |||
| 20 | sub _pid_dir { | ||
| 21 | return $PID_DIR if defined $PID_DIR; | ||
| 22 | if ($ENV{UBIC_DAEMON_PID_DIR}) { | ||
| 23 | warn "UBIC_DAEMON_PID_DIR env variable is deprecated, use Ubic->set_data_dir or configs instead (see Ubic::Settings for details)"; | ||
| 24 | $PID_DIR = $ENV{UBIC_DAEMON_PID_DIR}; | ||
| 25 | } | ||
| 26 | else { | ||
| 27 | $PID_DIR = Ubic::Settings->data_dir."/simple-daemon/pid"; | ||
| 28 | } | ||
| 29 | return $PID_DIR; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub _send_signal { | ||
| 33 | my ($self, $signal) = @_; | ||
| 34 | |||
| 35 | my $daemon = check_daemon($self->pidfile); | ||
| 36 | unless ($daemon) { | ||
| 37 | return result('not running'); | ||
| 38 | } | ||
| 39 | |||
| 40 | my $pid = $daemon->pid; | ||
| 41 | kill $signal => $pid; | ||
| 42 | |||
| 43 | my $guardian_pid = $daemon->guardian_pid; | ||
| 44 | kill HUP => $guardian_pid; | ||
| 45 | |||
| 46 | return result('reloaded', "sent $self->{reload_signal} to $pid, sent HUP to $guardian_pid"); | ||
| 47 | } | ||
| 48 | |||
| 49 | sub new { | ||
| 50 | my $class = shift; | ||
| 51 | my $params = validate(@_, { | ||
| 52 | bin => { type => SCALAR | ARRAYREF }, | ||
| 53 | user => { type => SCALAR, optional => 1 }, | ||
| 54 | group => { type => SCALAR | ARRAYREF, optional => 1 }, | ||
| 55 | daemon_user => { type => SCALAR, optional => 1 }, | ||
| 56 | daemon_group => { type => SCALAR | ARRAYREF, optional => 1 }, | ||
| 57 | name => { type => SCALAR, optional => 1 }, | ||
| 58 | pidfile => { type => SCALAR, optional => 1 }, | ||
| 59 | stdout => { type => SCALAR, optional => 1 }, | ||
| 60 | stderr => { type => SCALAR, optional => 1 }, | ||
| 61 | ubic_log => { type => SCALAR, optional => 1 }, | ||
| 62 | proxy_logs => { type => BOOLEAN, optional => 1 }, | ||
| 63 | cwd => { type => SCALAR, optional => 1 }, | ||
| 64 | env => { type => HASHREF, optional => 1 }, | ||
| 65 | reload_signal => { type => SCALAR, optional => 1 }, | ||
| 66 | term_timeout => { type => SCALAR, optional => 1, regex => qr/^\d+$/ }, | ||
| 67 | stop_timeout => { type => SCALAR, optional => 1, regex => qr/^\d+$/ }, | ||
| 68 | ulimit => { type => HASHREF, optional => 1 }, | ||
| 69 | auto_start => { type => BOOLEAN, default => 0 }, | ||
| 70 | }); | ||
| 71 | |||
| 72 | if ($params->{ulimit}) { | ||
| 73 | # load BSD::Resource lazily, but fail fast if we're asked for it | ||
| 74 | eval "require BSD::Resource"; | ||
| 75 | if ($@) { | ||
| 76 | die "BSD::Resource is not installed"; | ||
| 77 | } | ||
| 78 | if (BSD::Resource->VERSION < 1.29) { | ||
| 79 | # 1.29 supports string names for resources | ||
| 80 | die "BSD::Resource >= 1.29 required"; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | return bless {%$params} => $class; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub pidfile { | ||
| 88 | my ($self) = @_; | ||
| 89 | return $self->{pidfile} if exists($self->{pidfile}); | ||
| 90 | my $name = $self->full_name or die "Can't start nameless SimpleDaemon"; | ||
| 91 | return _pid_dir."/$name"; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub start_impl { | ||
| 95 | my ($self) = @_; | ||
| 96 | |||
| 97 | my $start_params = { | ||
| 98 | pidfile => $self->pidfile, | ||
| 99 | bin => $self->{bin}, | ||
| 100 | }; | ||
| 101 | for (qw/ env cwd stdout stderr ubic_log proxy_logs term_timeout /) { | ||
| 102 | $start_params->{$_} = $self->{$_} if defined $self->{$_}; | ||
| 103 | } | ||
| 104 | if ($self->{reload_signal}) { | ||
| 105 | $start_params->{proxy_logs} = 1; | ||
| 106 | } | ||
| 107 | if (defined $self->{daemon_user}) { | ||
| 108 | $start_params->{credentials} = Ubic::Credentials->new( | ||
| 109 | user => $self->{daemon_user}, | ||
| 110 | group => $self->{daemon_group}, | ||
| 111 | ); | ||
| 112 | } | ||
| 113 | if (defined $self->{ulimit}) { | ||
| 114 | $start_params->{start_hook} = sub { | ||
| 115 | for my $name (keys %{$self->{ulimit}}) { | ||
| 116 | my $value = $self->{ulimit}{$name}; | ||
| 117 | my $result = BSD::Resource::setrlimit($name, $value, $value); | ||
| 118 | unless ($result) { | ||
| 119 | die "Failed to set $name=$value ulimit"; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | }; | ||
| 123 | } | ||
| 124 | start_daemon($start_params); | ||
| 125 | } | ||
| 126 | |||
| 127 | sub user { | ||
| 128 | my $self = shift; | ||
| 129 | return $self->{user} if defined $self->{user}; | ||
| 130 | return $self->SUPER::user(); | ||
| 131 | } | ||
| 132 | |||
| 133 | sub group { | ||
| 134 | my $self = shift; | ||
| 135 | my $groups = $self->{group}; | ||
| 136 | return $self->SUPER::group() if not defined $groups; | ||
| 137 | return @$groups if ref $groups eq 'ARRAY'; | ||
| 138 | return $groups; | ||
| 139 | } | ||
| 140 | |||
| 141 | sub stop_impl { | ||
| 142 | my ($self) = @_; | ||
| 143 | my $options = {}; | ||
| 144 | $options->{timeout} = $self->{stop_timeout} if $self->{stop_timeout}; | ||
| 145 | stop_daemon($self->pidfile,$options); | ||
| 146 | } | ||
| 147 | |||
| 148 | sub status_impl { | ||
| 149 | my ($self) = @_; | ||
| 150 | if (my $daemon = check_daemon($self->pidfile)) { | ||
| 151 | return result('running', "pid ".$daemon->pid); | ||
| 152 | } | ||
| 153 | else { | ||
| 154 | return result('not running'); | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | sub reload { | ||
| 159 | my $self = shift; | ||
| 160 | unless (defined $self->{reload_signal}) { | ||
| 161 | return result('unknown', 'not implemented'); | ||
| 162 | } | ||
| 163 | return $self->_send_signal($self->{reload_signal}); | ||
| 164 | } | ||
| 165 | |||
| 166 | sub auto_start { | ||
| 167 | my $self = shift; | ||
| 168 | return $self->{auto_start}; | ||
| 169 | } | ||
| 170 | |||
| 171 | |||
| 172 | our %SIGNALS = ( | ||
| 173 | sigusr1 => 'SIGUSR1', | ||
| 174 | sigusr2 => 'SIGUSR2', | ||
| 175 | ); | ||
| 176 | |||
| 177 | sub custom_commands { | ||
| 178 | my ($self) = @_; | ||
| 179 | return keys %SIGNALS; | ||
| 180 | } | ||
| 181 | |||
| 182 | sub do_custom_command { | ||
| 183 | my ($self, $command) = @_; | ||
| 184 | unless (defined $SIGNALS{$command}) { | ||
| 185 | return result('unknown', 'not implemented'); | ||
| 186 | } | ||
| 187 | |||
| 188 | return $self->_send_signal($SIGNALS{$command}); | ||
| 189 | } | ||
| 190 | |||
| 191 | |||
| 192 | 1; | ||
| 193 | |||
| 194 | __END__ |