Minion/Util.pm

filter: Statementstmt 97.0% 65 / 67 Branchbran 66.6% 8 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 14 / 14 Podpod 100% 3 / 3 totaltotal 93.7% 90 / 96 SCAR 22.0
CC
9
Cov
92%
__ANON__
4.3
build_mojo_app
4.1
minion_init_args
3.0
CRAP 9.0
↑ Summary Runner/Command/minion.pm »
1 package Beam::Minion::Util;
2 9 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<Beam::Minion>.
15 #pod
16 #pod =head1 SEE ALSO
17 #pod
18 #pod L<Beam::Minion>
19 #pod
20 #pod =cut
21  
22 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 9 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 9 ▶use Minion;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 9 ▶use Beam::Runner::Util qw( find_containers );
Subroutine BEGIN: called CC=1 SCAR=0.0
27 9 ▶use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
28 9 ▶use Mojolicious;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 9 ▶use Mojo::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 9 ▶use Beam::Wire;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32  
33 9 our @EXPORT_OK = qw( minion_init_args minion build_mojo_app );
34  
35 9 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<BEAM_MINION> environment variable.
48 #pod
49 #pod This environment variable can take a few forms:
50 #pod
51 #pod =over
52 #pod
53 #pod =item <url>
54 #pod
55 #pod A simple backend URL like C<postgres://postgres@/test>,
56 #pod C<sqlite:/tmp/minion.db>, C<mongodb://127.0.0.1:27017>, or
57 #pod C<mysql://user@127.0.0.1/minion>. The following backends are supported:
58 #pod L<Minion::Backend::SQLite>, L<Minion::Backend::Pg>,
59 #pod L<Minion::Backend::MongoDB>, L<Minion::Backend::mysql>.
60 #pod
61 #pod =item <backend>+<url>
62 #pod
63 #pod A backend name and arguments, separated by C<+>, like
64 #pod C<Storable+/tmp/minion.db>. 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 ▶ unless $ENV{BEAM_MINION};
Subroutine minion_init_args: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"BEAM_MINION"} 3 10 13
80 10 my ( $backend, $url );
81 10 ▶ if ( $ENV{BEAM_MINION} =~ /^[^+:]+\+/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"BEAM_MINION"} =~ /^[^+:]+\+/) 1 9 10
82 1 my @args = split /\+/, $ENV{BEAM_MINION};
83 1 return @args;
84 }
85 9 my ( $schema ) = $ENV{BEAM_MINION} =~ /^([^:]+)/;
86 9 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<Minion> instance as configured by the C<BEAM_MINION> environment
94 #pod variable (parsed by L</minion_init_args>).
95 #pod
96 #pod =cut
97  
98 sub minion {
99 8 ▶ return Minion->new( minion_init_args );
Subroutine minion: called CC=1 SCAR=0.0
Pod documented
100 }
101  
102 #pod =sub build_mojo_app
103 #pod
104 #pod Build the L<Mojolicious> app that contains the L<Minion> plugin
105 #pod (L<Mojolicious::Plugin::Minion>) and tasks. This can then be given to
106 #pod one of the L<Minion::Command> classes to execute commands.
107 #pod
108 #pod =cut
109  
110 sub build_mojo_app {
111 5 ▶ my $app = Mojolicious->new(
Subroutine build_mojo_app: called CC=4 SCAR=14.0
Pod documented
112 log => Mojo::Log->new, # Log to STDERR
113 );
114  
115 5 push @{$app->commands->namespaces}, 'Minion::Command';
116  
117 5 my $minion = minion();
118 3 ▶ $app->helper(minion => sub {$minion});
Subroutine __ANON__: called CC=1 SCAR=0.0
119  
120 3 my %container = find_containers();
121 3 for my $container_name ( keys %container ) {
122 3 my $path = $container{ $container_name };
123 3 my $wire = Beam::Wire->new( file => $path );
124 3 my $config = $wire->config;
125 3 for my $service_name ( keys %$config ) {
126 12 ▶ next unless $wire->is_meta( $config->{ $service_name }, 1 );
Branch: 1 branch
BranchTrueFalseTotal
unless $wire->is_meta($config->{$service_name}, 1) 0 12 12
127 $minion->add_task( "$container_name:$service_name" => sub {
128 1 ▶ my ( $job, @args ) = @_;
Subroutine __ANON__: called CC=4 SCAR=14.7
129 1 my $wire = Beam::Wire->new( file => $path );
130  
131 1 my $obj = eval { $wire->get( $service_name, lifecycle => 'factory' ) };
132 1 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
133 0 return $job->fail( { error => $@ } );
134 }
135  
136 1 my $exit = eval { $obj->run( @args ) };
137 1 ▶ if ( my $err = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 1 1
138 0 return $job->fail( { error => $err } );
139 }
140  
141 1 ▶ my $method = $exit ? 'fail' : 'finish';
Branch: 1 branch
BranchTrueFalseTotal
$exit ? : 0 1 1
142 1 $job->$method( { exit => $exit } );
143 12 } );
144 }
145 3 undef $wire;
146 }
147  
148 3 return $app;
149 }
150  
151 9 1;
152  
153 __END__
↑ Summary Runner/Command/minion.pm »