File Coverage

blib/lib/Bot/ChatBots/Minion.pm
Criterion Covered Total %
statement 61 72 84.7
branch 14 26 53.8
condition 16 37 43.2
subroutine 15 17 88.2
pod 8 8 100.0
total 114 160 71.2


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