Beekeeper

 view release on metacpan or  search on metacpan

lib/Beekeeper/WorkerPool.pm  view on Meta::CPAN

package Beekeeper::WorkerPool;

use strict;
use warnings;

our $VERSION = '0.10';

use base 'Beekeeper::WorkerPool::Daemon';
use POSIX ":sys_wait_h";
use Time::HiRes 'sleep';
use Beekeeper::Config;

use constant COMPILE_ERROR_EXIT_CODE => 99;


sub new {
    my ($class, %args) = @_;

    my $self = $class->SUPER::new(
        daemon_name  => "beekeeper",
        description  => "worker pool",
        get_options  => [ "pool=s", "config-dir=s", "debug" ],
        %args,
    );

    $self->parse_options;

    my $pool_id = $self->{options}->{'pool'} || '';
    ($pool_id) = ($pool_id =~ m/^([\w-]+)$/); # untaint

    unless ($pool_id) {
        print "Mandatory parameter --pool was not specified.\n\n";
        #ENHACEMENT: list available pools
        $self->cmd_help;
        CORE::exit(1);
    }

    $self->{config}->{'pool_id'}   = $pool_id;
    $self->{config}->{daemon_name} = "beekeeper-$pool_id";
    $self->{config}->{description} = "worker pool $pool_id";

    # Pool cannot be started without a proper config file
    $self->load_config || CORE::exit(1);

    unless ($self->{config}->{log_file}) {
        my $file = "$pool_id-pool.log";
        my $dir  = '/var/log';
        my $user = $self->{options}->{'user'} || getpwuid($>);
        ($user) = ($user =~ m/^(\w+)$/); # untaint
        $self->{config}->{log_file} = (-d "$dir/$user") ? "$dir/$user/$file" : "$dir/$file";
    }

    return $self;
}

sub cmd_help {
    my $self = shift;

    my $progname = $0;
    $progname =~ s|.*/||;

    print "Usage: $progname [options] {start|stop|restart|reload|check}\n";
    print " --foreground      Run in foreground (do not daemonize)\n";
    print " --pool       str  Worker pool name (mandatory)\n";
    print " --user       str  Run as specified user\n";
    print " --group      str  Run as specified group\n";
    print " --config-dir str  Path to directory containing config files\n";
    print " --debug           Turn on workers debug flag\n";
    print " --help            Display this help and exit\n";
}

sub load_config {
    my $self = shift;

    my $pool_id  = $self->{config}->{'pool_id'};
    my $conf_dir = $self->{options}->{'config-dir'};

    Beekeeper::Config->set_config_dir($conf_dir) if ($conf_dir);

    my $pool_cfg = Beekeeper::Config->get_pool_config( pool_id => $pool_id );
    my $bus_cfg  = Beekeeper::Config->get_bus_config(  bus_id  => '*' );

    unless ($pool_cfg) {
        die "Worker pool '$pool_id' is not defined into config file pool.config.json\n";
    }

    # Ensure that local bus is defined
    my $bus_id = $pool_cfg->{'bus_id'};

    unless ($bus_cfg->{$bus_id}) {
        die "Bus '$bus_id' is not defined into config file bus.config.json\n";
    }

    # Merge pool.config.json contents
    $self->{config}->{$_} = $pool_cfg->{$_} foreach (keys %$pool_cfg);

    # Keep bus.config.json
    $self->{bus_config} = $bus_cfg;

    # Remove unused inherited entry
    delete $self->{config}->{get_options};

    return 1;
}

sub main {
    my $self = shift;



( run in 0.858 second using v1.01-cache-2.11-cpan-39bf76dae61 )