App-aep

 view release on metacpan or  search on metacpan

lib/App/aep.pm  view on Meta::CPAN

package App::aep;

# ABSTRACT: Allows you to run a command within a container and control its start up

# Core
use warnings;
use strict;
use utf8;
use v5.28;

# Core - Modules
use Socket qw(AF_INET PF_UNIX SOCK_STREAM);
use IO::Socket::INET;

# Core - Experimental (stable)
use experimental 'signatures';

# Debug
use Data::Dumper;

# External
use POE qw(
    Session::PlainCall
    Wheel::SocketFactory
    Wheel::ReadWrite
    Wheel::Run
    Filter::Stackable
    Filter::Line
    Filter::JSONMaybeXS
);
use Try::Tiny;

# Ensure unbuffered output for container environments
STDOUT->autoflush(1);
STDERR->autoflush(1);

# Version of this software
our $VERSION = '0.013';

# create a new blessed object, we will carry any passed arguments forward.
sub new ( $class, @args )
{
    my $self = bless { '_passed_args' => $args[ 0 ]->{ '_passed_args' }, }, $class;
    return $self;
}

# POE::Kernel's _start, in this case it also tells the kernel to capture signals
sub _start ( $self, @args )
{
    poe->kernel->sig( INT  => 'sig_int' );
    poe->kernel->sig( TERM => 'sig_term' );
    poe->kernel->sig( CHLD => 'sig_chld' );
    poe->kernel->sig( USR  => 'sig_usr' );

    # Store the main session ID so sub-sessions can post events back to us
    poe->heap->{ '_' }->{ 'main_session' } = poe->session->ID;

    my $debug = poe->heap->{ '_' }->{ 'debug' };
    $debug->( 'STDERR', __LINE__, 'Signals(INT,TERM,CHLD,USR) trapped.' );

    # What command are we meant to be running?
    my $opt = poe->heap->{ '_' }->{ 'opt' };

    # Initialize lock server order tracking
    if ( $opt->lock_server )
    {
        my $order_str = $opt->lock_server_order || '';
        my @raw_steps = grep { $_ ne '' } split( /,/, $order_str );

        # Each step may contain || for parallel groups: "redis1||redis2" becomes ['redis1', 'redis2']
        my @order;
        for my $step_str ( @raw_steps )
        {
            my @ids = split( /\|\|/, $step_str );
            push @order, \@ids;
        }

        poe->heap->{ 'lock' }->{ 'order' }          = \@order;
        poe->heap->{ 'lock' }->{ 'order_idx' }      = 0;
        poe->heap->{ 'lock' }->{ 'order_orig' }     = [ map { [ @{ $_ } ] } @order ];
        poe->heap->{ 'lock' }->{ 'waiting' }        = {};
        poe->heap->{ 'lock' }->{ 'unknown_queue' }  = [];
        poe->heap->{ 'lock' }->{ 'step_completed' } = 0;
    }

    # Initialize command state
    poe->heap->{ 'command' }                    = {};
    poe->heap->{ 'command' }->{ 'restart_count' } = 0;
    poe->heap->{ 'command' }->{ 'running' }     = 0;
    poe->heap->{ 'command' }->{ 'trigger_ok' }  = 0;



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