Ubic

 view release on metacpan or  search on metacpan

lib/Ubic/Service/SimpleDaemon.pm  view on Meta::CPAN

    }
    else {
        $PID_DIR = Ubic::Settings->data_dir."/simple-daemon/pid";
    }
    return $PID_DIR;
}

sub _send_signal {
    my ($self, $signal) = @_;

    my $daemon = check_daemon($self->pidfile);
    unless ($daemon) {
        return result('not running');
    }

    my $pid = $daemon->pid;
    kill $signal => $pid;

    my $guardian_pid = $daemon->guardian_pid;
    kill HUP => $guardian_pid;

    return result('reloaded', "sent $self->{reload_signal} to $pid, sent HUP to $guardian_pid");
}

sub new {
    my $class = shift;
    my $params = validate(@_, {
        bin => { type => SCALAR | ARRAYREF },
        user => { type => SCALAR, optional => 1 },
        group => { type => SCALAR | ARRAYREF, optional => 1 },
        daemon_user => { type => SCALAR, optional => 1 },
        daemon_group => { type => SCALAR | ARRAYREF, optional => 1 },
        name => { type => SCALAR, optional => 1 },
        pidfile => { type => SCALAR, optional => 1 },
        stdout => { type => SCALAR, optional => 1 },
        stderr => { type => SCALAR, optional => 1 },
        ubic_log => { type => SCALAR, optional => 1 },
        proxy_logs => { type => BOOLEAN, optional => 1 },
        cwd => { type => SCALAR, optional => 1 },
        env => { type => HASHREF, optional => 1 },
        reload_signal => { type => SCALAR, optional => 1 },
        term_timeout => { type => SCALAR, optional => 1, regex => qr/^\d+$/ },
        stop_timeout => { type => SCALAR, optional => 1, regex => qr/^\d+$/ },
        ulimit => { type => HASHREF, optional => 1 },
        auto_start => { type => BOOLEAN, default => 0 },
        kill_child_signal => { type => SCALAR, default => -15, optional => 1 },
    });

    if ($params->{ulimit}) {
        # load BSD::Resource lazily, but fail fast if we're asked for it
        eval "require BSD::Resource";
        if ($@) {
            die "BSD::Resource is not installed";
        }
        if (BSD::Resource->VERSION < 1.29) {
            # 1.29 supports string names for resources
            die "BSD::Resource >= 1.29 required";
        }
    }

    return bless {%$params} => $class;
}

sub pidfile {
    my ($self) = @_;
    return $self->{pidfile} if exists($self->{pidfile});
    my $name = $self->full_name or die "Can't start nameless SimpleDaemon";
    return _pid_dir."/$name";
}

sub start_impl {
    my ($self) = @_;

    my $start_params = {
        pidfile => $self->pidfile,
        bin => $self->{bin},
    };
    for (qw/ env cwd stdout stderr ubic_log proxy_logs term_timeout /) {
        $start_params->{$_} = $self->{$_} if defined $self->{$_};
    }
    if ($self->{reload_signal}) {
        $start_params->{proxy_logs} = 1;
    }
    if ($self->{kill_child_signal}) {
	    $start_params->{kill_child_signal} = $self->{kill_child_signal};
	}	
    if (defined $self->{daemon_user}) {
        $start_params->{credentials} = Ubic::Credentials->new(
            user => $self->{daemon_user},
            group => $self->{daemon_group},
        );
    }
    if (defined $self->{ulimit}) {
        $start_params->{start_hook} = sub {
            for my $name (keys %{$self->{ulimit}}) {
                my $value = $self->{ulimit}{$name};
                my $result = BSD::Resource::setrlimit($name, $value, $value);
                unless ($result) {
                    die "Failed to set $name=$value ulimit";
                }
            }
        };
    }
    start_daemon($start_params);
}

sub user {
    my $self = shift;
    return $self->{user} if defined $self->{user};
    return $self->SUPER::user();
}

sub group {
    my $self = shift;
    my $groups = $self->{group};
    return $self->SUPER::group() if not defined $groups;
    return @$groups if ref $groups eq 'ARRAY';
    return $groups;
}

sub stop_impl {

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.537 second using v1.00-cache-2.02-grep-82fe00e-cpan-2c419f77a38b )