File Coverage

blib/lib/Minion/Command/minion/jobx.pm
Criterion Covered Total %
statement 12 46 26.0
branch 0 32 0.0
condition 0 4 0.0
subroutine 4 10 40.0
pod 1 1 100.0
total 17 93 18.2


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