File Coverage

blib/lib/Beam/Minion/Util.pm
Criterion Covered Total %
statement 62 64 96.8
branch 8 12 66.6
condition n/a
subroutine 14 14 100.0
pod 3 3 100.0
total 87 93 93.5


line stmt bran cond sub pod time code
1             package Beam::Minion::Util;
2             our $VERSION = '0.014';
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   57937 use strict;
  4         15  
  4         85  
23 4     4   15 use warnings;
  4         6  
  4         76  
24 4     4   15 use Exporter qw( import );
  4         6  
  4         87  
25 4     4   1552 use Minion;
  4         940425  
  4         30  
26 4     4   1665 use Beam::Runner::Util qw( find_containers );
  4         40135  
  4         187  
27 4     4   24 use Scalar::Util qw( weaken );
  4         5  
  4         127  
28 4     4   1608 use Mojolicious;
  4         493252  
  4         31  
29 4     4   137 use Mojo::Log;
  4         7  
  4         30  
30 4     4   2119 use Beam::Wire;
  4         1437953  
  4         1948  
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 12 100   12 1 2005 unless $ENV{BEAM_MINION};
80 9         20 my ( $backend, $url );
81 9 100       45 if ( $ENV{BEAM_MINION} =~ /^[^+:]+\+/ ) {
82 1         4 my @args = split /\+/, $ENV{BEAM_MINION};
83 1         12 return @args;
84             }
85 8         64 my ( $schema ) = $ENV{BEAM_MINION} =~ /^([^:]+)/;
86 8         59 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 7     7 1 42 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 4     4 1 61 my $app = Mojolicious->new(
112             log => Mojo::Log->new, # Log to STDERR
113             );
114              
115 4         24733 push @{$app->commands->namespaces}, 'Minion::Command';
  4         17  
116              
117 4         178 my $minion = minion();
118 2         117758 weaken $minion->app($app)->{app};
119 2     2   71 $app->helper(minion => sub {$minion});
  2         2316  
120              
121 2         66 my %container = find_containers();
122 2         1173 for my $container_name ( keys %container ) {
123 2         5 my $path = $container{ $container_name };
124 2         21 my $wire = Beam::Wire->new( file => $path );
125 2         66185 my $config = $wire->config;
126 2         16 for my $service_name ( keys %$config ) {
127 8 50       72 next unless $wire->is_meta( $config->{ $service_name }, 1 );
128             $minion->add_task( "$container_name:$service_name" => sub {
129 1     1   4843 my ( $job, @args ) = @_;
130 1         66 my $wire = Beam::Wire->new( file => $path );
131              
132 1         13945 my $obj = eval { $wire->get( $service_name, lifecycle => 'factory' ) };
  1         20  
133 1 50       5061 if ( $@ ) {
134 0         0 return $job->fail( { error => $@ } );
135             }
136              
137 1         3 my $exit = eval { $obj->run( @args ) };
  1         15  
138 1 50       17 if ( my $err = $@ ) {
139 0         0 return $job->fail( { error => $err } );
140             }
141              
142 1 50       10 my $method = $exit ? 'fail' : 'finish';
143 1         33 $job->$method( { exit => $exit } );
144 8         530 } );
145             }
146 2         36 undef $wire;
147             }
148              
149 2         11 return $app;
150             }
151              
152             1;
153              
154             __END__