Bread-Runner

 view release on metacpan or  search on metacpan

lib/Bread/Runner.pm  view on Meta::CPAN

package Bread::Runner;
use 5.020;
use strict;
use warnings;

# ABSTRACT: run ALL the apps via Bread::Board

our $VERSION = '0.905'; # VERSION

use Bread::Board qw();
use Carp;
use Module::Runtime qw(use_module);
use Scalar::Util qw(blessed);
use Getopt::Long;
use Log::Any qw($log);
use Try::Tiny;


sub run {
    my ( $class, $bb_class, $opts ) = @_;

    my ($bb, $service) = $class->setup($bb_class, $opts);

    $class->_hook( 'pre_run', $bb, $service, $opts ) if $opts->{pre_run};

    my $run_methods = $opts->{run_method} || ['run'];
    $run_methods = [$run_methods] unless ref($run_methods) eq 'ARRAY';
    my $method;
    foreach my $m (@$run_methods) {
        next unless $service->can($m);
        $method = $m;
        last;
    }
    unless ($method) {
        my $msg = ref($service)." does not provide any run_method: "
            . join( ', ', @$run_methods );
        $log->error($msg);
        croak $msg;
    }

    my $rv = try {
        $log->infof("Running %s->%s",ref($service), $method) unless $opts->{no_startup_logmessage};
        return $service->$method;
    }
    catch {
        my $e = $_;
        my $msg;
        if ( blessed($e) && $e->can('message') ) {
            $msg = $e->message;
        }
        else {
            $msg = $e;
        }
        $log->errorf( "%s died with %s", $method, $msg );
        croak $msg;
    };

    $class->_hook( 'post_run', $bb, $service, $opts ) if $opts->{post_run};
    return $rv;
}


sub setup {
    my ( $class, $bb_class, $opts ) = @_;
    $opts ||= {};

    my $service_name = $opts->{service} || $0;
    $service_name =~ s{^(?:.*\bbin/)(.+)$}{$1};
    $service_name =~ s{/}{_}g;

    my $bb = $class->_compose_breadboard( $bb_class, $opts );

    my $bb_container = $opts->{container} || 'App';
    my $service_bb = try {
        $bb->fetch( $bb_container . '/' . $service_name );
    }



( run in 2.945 seconds using v1.01-cache-2.11-cpan-8f98c5d2c55 )