File Coverage

blib/lib/Minion/Command/minion/jobx.pm
Criterion Covered Total %
statement 12 47 25.5
branch 0 34 0.0
condition 0 4 0.0
subroutine 4 10 40.0
pod 1 1 100.0
total 17 96 17.7


line stmt bran cond sub pod time code
1             package Minion::Command::minion::jobx;
2              
3 2     2   13954 use Mojo::Base 'Mojolicious::Command';
  2         6689  
  2         9  
4              
5 2     2   162718 use Getopt::Long qw/GetOptionsFromArray :config no_auto_abbrev no_ignore_case/;
  2         14376  
  2         9  
6 2     2   1276 use Mojo::JSON qw/decode_json/;
  2         14156  
  2         126  
7 2     2   12 use Mojo::Util qw/dumper tablify/;
  2         2  
  2         1472  
8              
9             our $VERSION = '0.05';
10              
11             has description => 'Manage Minion jobs';
12             has usage => sub { shift->extract_usage };
13              
14             sub run {
15 0     0 1   my ($self, @args) = @_;
16              
17 0           my ($args, $options) = ([], {});
18             GetOptionsFromArray \@args,
19             'A|attempts=i' => \$options->{attempts},
20 0     0     'a|args=s' => sub { $args = decode_json($_[1]) },
21             'b|broadcast=s' => (\my $command),
22             'd|delay=i' => \$options->{delay},
23             'e|enqueue=s' => \my $enqueue,
24             'l|limit=i' => \(my $limit = 100),
25             'o|offset=i' => \(my $offset = 0),
26             'P|parent=s' => ($options->{parents} = []),
27             'p|priority=i' => \$options->{priority},
28             'q|queue=s' => \$options->{queue},
29             'R|retry' => \my $retry,
30             'r|remove' => \my $remove,
31             'S|state=s' => \$options->{state},
32             's|stats' => \my $stats,
33             't|task=s' => \$options->{task},
34 0           'w|workers' => \my $workers;
35              
36             # Worker remote control command
37 0 0         return $self->app->minion->backend->broadcast($command, $args, \@args)
38             if $command;
39              
40             # Enqueue
41 0 0         return say $self->app->minion->enqueue($enqueue, $args, $options) if $enqueue;
42              
43             # Show stats
44 0 0         return $self->_stats if $stats;
45              
46             # List jobs/workers
47 0 0         my $id = @args ? shift @args : undef;
48              
49 0 0         return $id ? $self->_worker($id) : $self->_list_workers($offset, $limit) if $workers;
    0          
50 0 0         return $self->_list_jobs($offset, $limit, $options) unless defined $id;
51 0 0         die "Job does not exist.\n" unless my $job = $self->app->minion->job($id);
52              
53             # Remove job
54 0 0 0       return $job->remove || die "Job is active.\n" if $remove;
55              
56             # Retry job
57 0 0 0       return $job->retry($options) || die "Job is active.\n" if $retry;
58              
59             # Job info
60 0           my $job_info = $job->info;
61              
62 0 0         $job_info->{created} = localtime($job_info->{created}) if $job_info->{created};
63 0 0         $job_info->{started} = localtime($job_info->{started}) if $job_info->{started};
64 0 0         $job_info->{delayed} = localtime($job_info->{delayed}) if $job_info->{delayed};
65 0 0         $job_info->{finished} = localtime($job_info->{finished}) if $job_info->{finished};
66              
67 0           print dumper($job_info);
68             }
69              
70             sub _list_jobs {
71 0     0     my $jobs = shift->app->minion->backend->list_jobs(@_);
72 0           my @job_rows;
73 0           push @job_rows, ['id','state','queue','created','started','finished','task'];
74 0           foreach my $job (@$jobs) {
75 0           foreach my $key (qw/created started finished/) {
76 0 0         $job->{$key} = '[' . localtime($job->{$key}) . ']' if defined($job->{$key});
77 0 0         $job->{$key} = 'N/A' unless defined($job->{$key});
78             }
79 0           push @job_rows, [$job->{id}, $job->{state}, $job->{queue}, $job->{created}, $job->{started}, $job->{finished}, $job->{task}];
80             }
81 0           print tablify \@job_rows;
82             }
83              
84             sub _list_workers {
85 0     0     my $workers = shift->app->minion->backend->list_workers(@_);
86 0           my @workers = map { [$_->{id}, $_->{host} . ':' . $_->{pid}] } @$workers;
  0            
87 0           print tablify \@workers;
88             }
89              
90 0     0     sub _stats { print dumper shift->app->minion->stats }
91              
92             sub _worker {
93 0 0   0     die "Worker does not exist.\n"
94             unless my $worker = shift->app->minion->backend->worker_info(@_);
95 0           print dumper $worker;
96             }
97              
98             =encoding utf8
99              
100             =head1 NAME
101              
102             Minion::Command::minion::jobx - The clone of Minion::Command::minion::job but with some output changes.
103              
104             =head1 VERSION
105              
106             Version 0.01
107              
108             =head1 SYNOPSIS
109              
110             This module will work the same as Minion::Command::minion::job but with some differences.
111              
112             1) Display timestamps instead of epoch times.
113              
114             {
115             "args" => [
116             "/some/path/to/some/file",
117             "/some/other/path/to/some/file"
118             ],
119             "attempts" => 1,
120             "children" => [],
121             "created" => "Wed Aug 3 15:05:00 2016",
122             "delayed" => "Wed Aug 3 15:05:00 2016",
123             "finished" => "Wed Aug 3 15:05:26 2016",
124             "id" => 1853,
125             "parents" => [
126             1852
127             ],
128             "priority" => 0,
129             "queue" => "default",
130             "result" => {
131             "output" => "done"
132             },
133             "retried" => undef,
134             "retries" => 0,
135             "started" => "Wed Aug 3 15:05:05 2016",
136             "state" => "finished",
137             "task" => "task_a",
138             "worker" => 108
139             }
140              
141             2) Add "created", "started" and "finished" times to the list of jobs. Column headers included.
142              
143             $./script/app minion jobx -l 5
144             id state queue created started finished task
145             2507 finished default [Thu Aug 18 16:23:25 2016] [Thu Aug 18 16:23:32 2016] [Thu Aug 18 16:23:38 2016] some_task
146             2506 finished default [Thu Aug 18 16:23:25 2016] [Thu Aug 18 16:23:31 2016] [Thu Aug 18 16:23:34 2016] some_task
147             2505 finished default [Thu Aug 18 16:23:25 2016] [Thu Aug 18 16:23:30 2016] [Thu Aug 18 16:23:41 2016] some_task
148             2504 finished default [Thu Aug 18 16:23:25 2016] [Thu Aug 18 16:23:30 2016] [Thu Aug 18 16:23:36 2016] some_task
149             2503 finished default [Thu Aug 18 16:23:25 2016] [Thu Aug 18 16:23:25 2016] [Thu Aug 18 16:23:33 2016] some_task
150              
151             =head1 USAGE
152              
153             Usage: APPLICATION minion jobx [OPTIONS] [ID]
154              
155             ./myapp.pl minion jobx
156             ./myapp.pl minion jobx 10023
157             ./myapp.pl minion jobx -w
158             ./myapp.pl minion jobx -w 23
159             ./myapp.pl minion jobx -s
160             ./myapp.pl minion jobx -q important -t foo -S inactive
161             ./myapp.pl minion jobx -e foo -a '[23, "bar"]'
162             ./myapp.pl minion jobx -e foo -P 10023 -P 10024 -p 5 -q important
163             ./myapp.pl minion jobx -R -d 10 10023
164             ./myapp.pl minion jobx -r 10023
165              
166             Options:
167             -A, --attempts Number of times performing this new job will be
168             attempted, defaults to 1
169             -a, --args Arguments for new job in JSON format
170             -d, --delay Delay new job for this many seconds
171             -e, --enqueue New job to be enqueued
172             -h, --help Show this summary of available options
173             --home Path to home directory of your application,
174             defaults to the value of MOJO_HOME or
175             auto-detection
176             -l, --limit Number of jobs/workers to show when listing them,
177             defaults to 100
178             -m, --mode Operating mode for your application, defaults to
179             the value of MOJO_MODE/PLACK_ENV or "development"
180             -o, --offset Number of jobs/workers to skip when listing them,
181             defaults to 0
182             -P, --parent One or more jobs the new job depends on
183             -p, --priority Priority of new job, defaults to 0
184             -q, --queue Queue to put new job in, defaults to "default", or
185             list only jobs in this queue
186             -R, --retry Retry job
187             -r, --remove Remove job
188             -S, --state List only jobs in this state
189             -s, --stats Show queue statistics
190             -t, --task List only jobs for this task
191             -w, --workers List workers instead of jobs, or show information
192             for a specific worker
193              
194             =head1 ATTRIBUTES
195            
196             L inherits all attributes from
197             L and implements the following new ones.
198            
199             =head2 description
200            
201             my $description = $job->description;
202             $job = $job->description('Foo');
203            
204             Short description of this command, used for the command list.
205            
206             =head2 usage
207            
208             my $usage = $job->usage;
209             $job = $job->usage('Foo');
210            
211             Usage information for this command, used for the help screen.
212            
213             =head1 METHODS
214            
215             L inherits all methods from
216             L and implements the following new ones.
217            
218             =head2 run
219            
220             $job->run(@ARGV);
221            
222             Run this command.
223              
224             =head1 TO DO
225              
226             - Allow command line option to let user pick which timestamps are included in the list of jobs
227            
228             =head1 SEE ALSO
229            
230             L, L, L.
231              
232             =head1 ACKNOWLEDGEMENTS
233              
234             Most of the code comes from L written by Sebastian Riedel (SRI).
235              
236             =cut
237              
238             1; # End of Minion::Command::minion::jobx