File Coverage

blib/lib/Dancer2/Plugin/Minion.pm
Criterion Covered Total %
statement 18 19 94.7
branch n/a
condition 1 2 50.0
subroutine 5 6 83.3
pod 3 3 100.0
total 27 30 90.0


line stmt bran cond sub pod time code
1             package Dancer2::Plugin::Minion;
2              
3             # ABSTRACT: Use the Minion job queue in your Dancer2 apps.
4              
5 2     2   1816505 use Dancer2::Plugin;
  2         28275  
  2         18  
6 2     2   8226 use Minion;
  2         669437  
  2         20  
7              
8             our $VERSION = '0.3.1';
9              
10             plugin_keywords qw(
11             minion
12             add_task
13             enqueue
14             minion_app
15             );
16              
17             has _backend => (
18             is => 'ro',
19             from_config => 'backend',
20             default => sub{ '' },
21             );
22              
23             has _dsn => (
24             is => 'ro',
25             from_config => 'dsn',
26             default => sub{ '' },
27             );
28              
29             has 'minion' => (
30             is => 'ro',
31             lazy => 1,
32             default => sub {
33             Minion->new( $_[0]->_backend => $_[0]->_dsn );
34             },
35             );
36              
37             sub add_task {
38 2     2 1 22656 return shift->minion->add_task( @_ );
39             }
40              
41             sub enqueue {
42 2     2 1 246707 return shift->minion->enqueue( @_ );
43             }
44              
45             sub minion_app {
46 1     1 1 171158 my $plugin = shift;
47 1   50     9 my $return_to = shift || '/';
48              
49 1         725 require Mojolicious;
50 1         150860 my $app = Mojolicious->new;
51 1         24603 $app->log->level('warn');
52              
53             # emulate minion plugin, needed for admin panel
54 1     0   159 $app->helper(minion => sub { $plugin->minion });
  0         0  
55             # enable the minion command system
56 1         106 push @{ $app->commands->namespaces }, 'Minion::Command';
  1         4  
57              
58 1         79 $app->plugin('Minion::Admin' => {
59             route => $app->routes->any('/'),
60             return_to => $return_to,
61             });
62              
63 1         4952 return $app;
64             }
65              
66             1;
67              
68             __END__
69              
70             =pod
71              
72             =encoding UTF-8
73              
74             =head1 NAME
75              
76             Dancer2::Plugin::Minion - Use the Minion job queue in your Dancer2 apps.
77              
78             =head1 VERSION
79              
80             version 0.3.1
81              
82             =head1 SYNOPSIS
83              
84             package MyApp;
85             use Dancer2;
86             use Dancer2::Plugin::Minion;
87             use Plack::Builder;
88              
89             get '/' => sub {
90             add_task( add => sub {
91             my ($job, $first, $second) = @_;
92             $job->finish($first + $second);
93             });
94             };
95              
96             get '/another-route' => sub {
97             my $id = enqueue(add => [1, 1]);
98             # Do something with $id
99             };
100              
101             get '/yet-another-route' => sub {
102             # Get a job ID, then...
103             my $result = minion->job($id)->info->{result};
104             };
105              
106             build {
107             mount '/dashboard/' => minion_app->start;
108             mount '/' => start;
109             }
110              
111             # In config.yml
112             plugins:
113             Minion:
114             dsn: sqlite:test.db
115             backend: SQLite
116              
117             =head1 DESCRIPTION
118              
119             C<Dancer2::Plugin::Minion> makes it easy to add a job queue to any of your
120             L<Dancer2> applications. The queue is powered by L<Minion> and uses a
121             backend of your choosing, such as PostgreSQL or SQLite.
122              
123             The plugin lazily instantiates a Minion object, which is accessible via the
124             C<minion> keyword. Any method, attribute, or event you need in Minion is
125             available via this keyword. Additionally, C<add_task> and C<enqueue> keywords
126             are available to make it convenient to add and start new queued jobs.
127              
128             See the L<Minion> documentation for more complete documentation on the methods
129             and functionality available.
130              
131             =head1 NAME
132              
133             Dancer2::Plugin::Minion - Easy access to Minion job queue in your Dancer2
134             applications
135              
136             =head1 ATTRIBUTES
137              
138             =head2 minion
139              
140             The L<Minion>-based object. See the L<Minion> documentation for a list of
141             additional methods provided.
142              
143             =head1 METHODS
144              
145             =head2 add_task()
146              
147             Keyword/shortcut for C<< minion->add_task() >>. See
148             L<Minion's add_task() documentation|Minion/add_task> for
149             more information.
150              
151             =head2 enqueue()
152              
153             Keyword/shortcut for C<< minion->enqueue() >>.
154             See L<Minion's enqueue() documentation|Minion/enqueue1>
155             for more information.
156              
157             =head2 minion_app()
158              
159             Build a L<Mojolicious> application with the
160             L<Mojolicious::Plugin::Minion::Admin> application running. This application can
161             then be started and mounted inside your own but be sure to leave a trailing
162             slash in your mount path!
163              
164             You can optionally pass in an absolute URL to act as the "return to" link. The url must
165             be absolute or else it will be made relative to the admin UI, which is probably
166             not what you want. For example:
167             C<< mount '/dashboard/' => minion_app( '/foo/bar' )->start; >>
168              
169             =head1 RUNNING JOBS
170              
171             You will need to create a Minion worker if you want to be able to run your
172             queued jobs. Thankfully, you can write a minimal worker with just a few
173             lines of code:
174              
175             #!/usr/bin/env perl
176              
177             use Dancer2;
178             use Dancer2::Plugin::Minion;
179             use MyJobLib;
180              
181             minion->add_task( my_job_1 => MyJobLib::job1());
182              
183             my $worker = minion->worker;
184             $worker->run;
185              
186             By using C<Dancer2::Plugin::Minion>, your worker will be configured with
187             the settings provided in your F<config.yml> file. See L<Minion::Worker>
188             for more information.
189              
190             =head1 SEE ALSO
191              
192             =over 4
193              
194             =item * L<Dancer2>
195              
196             =item * L<Minion>
197              
198             =back
199              
200             =head1 AUTHOR
201              
202             Jason A. Crome C< cromedome AT cpan DOT org >
203              
204             =head1 ACKNOWLEDGEMENTS
205              
206             I'd like to extend a hearty thanks to my employer, Clearbuilt Technologies,
207             for giving me the necessary time and support for this module to come to
208             life.
209              
210             The following contributors have sent patches, suggestions, or bug reports that
211             led to the improvement of this plugin:
212              
213             =over 4
214              
215             =item * Gabor Szabo
216             =item * Joel Berger
217             =item * Slaven Rezić
218              
219             =back
220              
221             =head1 COPYRIGHT AND LICENSE
222              
223             Copyright (c) 2020, Clearbuilt Technologies.
224              
225             This is free software; you can redistribute it and/or modify it under
226             the same terms as the Perl 5 programming language system itself.
227              
228             =head1 AUTHOR
229              
230             Jason A. Crome <cromedome@cpan.org>
231              
232             =head1 COPYRIGHT AND LICENSE
233              
234             This software is copyright (c) 2020 by Clearbuilt Technologies.
235              
236             This is free software; you can redistribute it and/or modify it under
237             the same terms as the Perl 5 programming language system itself.
238              
239             =cut