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