File Coverage

blib/lib/Bot/ChatBots/Minion.pm
Criterion Covered Total %
statement 58 69 84.0
branch 14 26 53.8
condition 16 37 43.2
subroutine 14 16 87.5
pod 8 8 100.0
total 110 156 70.5


line stmt bran cond sub pod time code
1             package Bot::ChatBots::Minion;
2 2     2   87335 use strict;
  2         6  
  2         171  
3             { our $VERSION = '0.002'; }
4              
5 2     2   1583 use Ouch;
  2         8089  
  2         244  
6 2     2   1833 use Log::Any qw< $log >;
  2         19205  
  2         12  
7 2     2   6685 use Bot::ChatBots::Utils qw< pipeline >;
  2         6953  
  2         194  
8 2     2   1630 use Mojo::Base 'Mojolicious::Plugin';
  2         27806  
  2         14  
9              
10             has _minion => sub { ouch 500, 'no minion set' };
11             has name => sub { return ref($_[0]) || $_[0] };
12              
13             sub dequeuer {
14 2     2 1 32 my $self = shift;
15 2 50 33     21 my $args = (@_ && ref($_[0])) ? $_[0] : {@_};
16              
17 2   33     11 my $name = $args->{name} // $self->name // '<unknown dequeuer>';
      50        
18              
19             my $ds = $args->{downstream} // $args->{processor}
20 2 100 66     26 or ouch 500, 'no processor/downstream provided for dequeuer';
21 1 50       13 $ds = pipeline((ref($ds) eq 'ARRAY') ? @$ds : $ds);
22              
23             return sub {
24 0     0   0 my ($job, $record) = @_;
25 0         0 $log->info("dequeuing for $name");
26 0         0 my @retval = $ds->($record);
27 0         0 $job->finish('All went well... hopefully');
28 0         0 return @retval;
29 1         28 };
30             }
31              
32             sub enqueue {
33 0     0 1 0 my ($self, $record, $name) = @_;
34 0         0 return $self->enqueuer($name)->($record);
35             } ## end sub enqueue
36              
37             sub enqueuer {
38 1     1 1 20 my ($self, $name)= @_;
39              
40 1 50 33     12 $name //= $self->name
41             or ouch 500, 'no task name provided for enqueuer';
42              
43 1         3 state $cache = {};
44             return $cache->{$name} //= sub {
45 1     1   24 my $record = shift;
46 1         9 $log->info("enqueueing for $name");
47 1         4 $self->minion->enqueue($name, [$record]);
48 1         96 return $record;
49 1   50     15 };
50             }
51              
52             sub helper_name {
53 2     2 1 4 my $self = shift;
54 2   33     19 (my $name = lc(ref $self || $self)) =~ s{.*::}{}mxs;
55 2         10 return "chatbots.$name";
56             }
57              
58             sub install_dequeuer {
59 2     2 1 4 my $self = shift;
60 2 50 33     15 my $args = (@_ && ref($_[0])) ? $_[0] : {@_};
61 2 50 33     12 my $name = $args->{name} // $self->name
62             or ouch 500, 'no task name available for installing dequeuer';
63 2         13 $self->minion->add_task($name => $self->dequeuer($args));
64 1         156 return $self;
65             }
66              
67             sub minion {
68 5     5 1 8 my $self = shift;
69 5 100       14 if (@_) {
70 2         3 my $minion = shift;
71 2 50       7 if (ref($minion) eq 'ARRAY') {
72 0         0 require Minion;
73 0         0 $minion = Minion->new(@$minion);
74             }
75 2         6 return $self->_minion($minion);
76             }
77 3         8 return $self->_minion;
78             }
79              
80             sub register {
81 2     2 1 751 my ($self, $app, $conf) = @_;
82              
83 2         4 my $minion;
84 2 50       9 if (my $pc = $conf->{Minion}) {
85 0 0       0 $app->plugin(Minion => ((ref($pc) eq 'ARRAY') ? (@$pc) : ($pc)));
86 0         0 $minion = $app->minion;
87             }
88             else {
89 2   33     9 $minion = $conf->{minion} // eval { $app->minion };
  2         15  
90             }
91 2 50       204 $self->minion($minion) if defined $minion;
92              
93 2 50       19 $self->name($conf->{name}) if exists $conf->{name};
94              
95 2   66     9 my $helper_name = $conf->{helper_name} // $self->helper_name;
96 2     2   31 $app->helper($helper_name => sub { return $self });
  2         1893  
97              
98 2         157 return $self;
99             } ## end sub register
100              
101             sub wrapper {
102 2     2 1 78 my $self = shift;
103 2 50 66     22 my $args = (@_ && ref($_[0])) ? $_[0] : {@_};
104 2         8 $self->install_dequeuer($args);
105 1   33     10 return $self->enqueuer($args->{name} // $self->name);
106             }
107              
108             42;