HADaemon-Control

 view release on metacpan or  search on metacpan

lib/HADaemon/Control.pm  view on Meta::CPAN

        if (exists $args->{$accessor}) {
            $self->{$accessor} = delete $args->{$accessor};
        }
    }

    $self->user(delete $args->{user}) if exists $args->{user};
    $self->group(delete $args->{group}) if exists $args->{group};

    die "Unknown arguments to the constructor: " . join(' ' , keys %$args)
        if keys %$args;

    return $self;
}

sub run {
    my ($self) = @_;
    return $self->run_command(@ARGV);
}

sub run_command {
    my ($self, $arg) = @_;

    # Error Checking.
    $self->program && ref $self->program eq 'CODE'
        or die "Error: program must be defined and must be coderef\n";
    $self->name
        or die "Error: name must be defined\n";
    $self->pid_dir
        or die "Error: pid_dir must be defined\n";
    $self->log_file
        or die "Error: log_file must be defined\n";

    defined($self->kill_timeout)
        or $self->kill_timeout(1);

    defined($self->close_fds_on_start)
        or $self->close_fds_on_start(1);

    $self->standby_stop_file
        or $self->standby_stop_file($self->pid_dir . '/standby-stop-file');

    # ipc_cl_options default settings
    $self->{ipc_cl_options} //= {};
    $self->{ipc_cl_options}->{type}              //= 'Flock';
    $self->{ipc_cl_options}->{max_procs}         //= 1;
    $self->{ipc_cl_options}->{standby_max_procs} //= 1;
    $self->{ipc_cl_options}->{interval}          //= 1;
    $self->{ipc_cl_options}->{retries}           //= sub { 1 };
    $self->{ipc_cl_options}->{path}              //= $self->pid_dir . '/lock/';
    $self->{ipc_cl_options}->{standby_path}      //= $self->pid_dir . '/lock-standby/';

    # ipc_cl_options error checking
    $self->{ipc_cl_options}->{type} ne 'Flock'
        and die "can work only with Flock backend\n";
    $self->{ipc_cl_options}->{max_procs} < 1
        and die "ipc_cl_options: 'max_procs' should be at least 1\n";
    $self->{process_name_change}
        and $self->{ipc_cl_options}->{process_name_change} = 1;

    if ($self->uid) {
        my @uiddata = getpwuid($self->uid);
        @uiddata or die "failed to get info about " . $self->uid . "\n";

        if (!$self->gid) {
            $self->gid($uiddata[3]);
            $self->trace("Implicit GID => " . $uiddata[3]);
        }

        $self->user
            or $self->{user} = $uiddata[0];

        $self->{user_home_dir} = $uiddata[7];
    }

    my $called_with = $arg // '';
    $called_with =~ s/^[-]+//g;

    if (!$called_with) {
        return $self->do_help();
    }

    # $self->can() create new record in class hash
    # so $self->_all_actions includes it as well
    if (not grep { $_ eq $called_with  } $self->_all_actions) {
        warn "Error: unknown action $called_with\n";
        return $self->do_help();
    }

    my $action = "do_$called_with";
    return $self->$action() // 0;
}

#####################################
# commands
#####################################
sub do_start {
    my ($self) = @_;
    $self->info('do_start()');

    my $expected_main = $self->_expected_main_processes();
    my $expected_standby = $self->_expected_standby_processes();
    if (   $self->_main_running() == $expected_main
        && $self->_standby_running() == $expected_standby)
    {
        $self->pretty_print('starting main + standby processes', 'Already Running');
        $self->trace("do_start(): all processes are already running");
        return 0;
    }

    $self->_precreate_directories();
    $self->_unlink_file($self->standby_stop_file);

    if ($self->_fork_mains() && $self->_fork_standbys()) {
        $self->pretty_print('starting main + standby processes', 'OK');
        return 0;
    }

    $self->pretty_print('starting main + standby processes', 'Failed', 'red');
    $self->do_status();
    $self->detect_stolen_lock();
    $self->_print_check_log_file_for_details();



( run in 0.511 second using v1.01-cache-2.11-cpan-df04353d9ac )