File Coverage

blib/lib/Beam/Minion/Util.pm
Criterion Covered Total %
statement 61 63 96.8
branch 8 12 66.6
condition n/a
subroutine 14 14 100.0
pod 3 3 100.0
total 86 92 93.4


line stmt bran cond sub pod time code
1             package Beam::Minion::Util;
2             our $VERSION = '0.016';
3             # ABSTRACT: Utility functions for Beam::Minion
4              
5             #pod =head1 SYNOPSIS
6             #pod
7             #pod use Beam::Minion::Util qw( minion );
8             #pod
9             #pod my $minion = minion();
10             #pod my %attrs = minion_attrs();
11             #pod
12             #pod =head1 DESCRIPTION
13             #pod
14             #pod This module contains helper routines for L.
15             #pod
16             #pod =head1 SEE ALSO
17             #pod
18             #pod L
19             #pod
20             #pod =cut
21              
22 4     4   68474 use strict;
  4         15  
  4         129  
23 4     4   20 use warnings;
  4         16  
  4         180  
24 4     4   25 use Exporter qw( import );
  4         8  
  4         134  
25 4     4   1834 use Minion;
  4         808704  
  4         34  
26 4     4   1968 use Beam::Runner::Util qw( find_containers );
  4         48798  
  4         256  
27 4     4   37 use Scalar::Util qw( weaken );
  4         7  
  4         173  
28 4     4   2506 use Mojolicious;
  4         608533  
  4         44  
29 4     4   175 use Mojo::Log;
  4         10  
  4         46  
30 4     4   2552 use Beam::Wire;
  4         1872198  
  4         2649  
31              
32              
33             our @EXPORT_OK = qw( minion_init_args minion build_mojo_app );
34              
35             our %BACKEND = (
36             sqlite => 'SQLite',
37             postgres => 'Pg',
38             mongodb => 'MongoDB',
39             mysql => 'mysql',
40             );
41              
42             #pod =sub minion_init_args
43             #pod
44             #pod my %args = minion_init_args();
45             #pod
46             #pod Get the arguments needed to initialize a new Minion instance by parsing
47             #pod the C environment variable.
48             #pod
49             #pod This environment variable can take a few forms:
50             #pod
51             #pod =over
52             #pod
53             #pod =item
54             #pod
55             #pod A simple backend URL like C,
56             #pod C, C, or
57             #pod C. The following backends are supported:
58             #pod L, L,
59             #pod L, L.
60             #pod
61             #pod =item +
62             #pod
63             #pod A backend name and arguments, separated by C<+>, like
64             #pod C. Any backend may be used this way.
65             #pod
66             #pod If your backend requires more arguments, you can separate them with
67             #pod C<+>:
68             #pod
69             #pod # Configure the MySQL backend with a DBI DSN
70             #pod BEAM_MINION=mysql+dsn+dbi:mysql:minion
71             #pod
72             #pod =back
73             #pod
74             #pod =cut
75              
76             sub minion_init_args {
77             die "You must set the BEAM_MINION environment variable to the Minion database URL.\n"
78             . "See `perldoc Beam::Minion` for getting started instructions.\n"
79 13 100   13 1 2713 unless $ENV{BEAM_MINION};
80 10         34 my ( $backend, $url );
81 10 100       58 if ( $ENV{BEAM_MINION} =~ /^[^+:]+\+/ ) {
82 1         8 my @args = split /\+/, $ENV{BEAM_MINION};
83 1         16 return @args;
84             }
85 9         68 my ( $schema ) = $ENV{BEAM_MINION} =~ /^([^:]+)/;
86 9         90 return $BACKEND{ $schema }, $ENV{BEAM_MINION};
87             }
88              
89             #pod =sub minion
90             #pod
91             #pod my $minion = minion();
92             #pod
93             #pod Get a L instance as configured by the C environment
94             #pod variable (parsed by L).
95             #pod
96             #pod =cut
97              
98             sub minion {
99 8     8 1 41 return Minion->new( minion_init_args );
100             }
101              
102             #pod =sub build_mojo_app
103             #pod
104             #pod Build the L app that contains the L plugin
105             #pod (L) and tasks. This can then be given to
106             #pod one of the L classes to execute commands.
107             #pod
108             #pod =cut
109              
110             sub build_mojo_app {
111 5     5 1 59 my $app = Mojolicious->new(
112             log => Mojo::Log->new, # Log to STDERR
113             );
114              
115 5         62127 push @{$app->commands->namespaces}, 'Minion::Command';
  5         26  
116              
117 5         368 my $minion = minion();
118 3     2   161509 $app->helper(minion => sub {$minion});
  2         2710  
119              
120 3         353 my %container = find_containers();
121 3         2286 for my $container_name ( keys %container ) {
122 3         10 my $path = $container{ $container_name };
123 3         61 my $wire = Beam::Wire->new( file => $path );
124 3         98270 my $config = $wire->config;
125 3         31 for my $service_name ( keys %$config ) {
126 12 50       140 next unless $wire->is_meta( $config->{ $service_name }, 1 );
127             $minion->add_task( "$container_name:$service_name" => sub {
128 1     1   5660 my ( $job, @args ) = @_;
129 1         73 my $wire = Beam::Wire->new( file => $path );
130              
131 1         14230 my $obj = eval { $wire->get( $service_name, lifecycle => 'factory' ) };
  1         20  
132 1 50       5241 if ( $@ ) {
133 0         0 return $job->fail( { error => $@ } );
134             }
135              
136 1         4 my $exit = eval { $obj->run( @args ) };
  1         12  
137 1 50       13 if ( my $err = $@ ) {
138 0         0 return $job->fail( { error => $err } );
139             }
140              
141 1 50       9 my $method = $exit ? 'fail' : 'finish';
142 1         40 $job->$method( { exit => $exit } );
143 12         1042 } );
144             }
145 3         73 undef $wire;
146             }
147              
148 3         65 return $app;
149             }
150              
151             1;
152              
153             __END__