File Coverage

blib/lib/Minion/Command/minion/jobx.pm
Criterion Covered Total %
statement 12 45 26.6
branch 0 32 0.0
condition 0 4 0.0
subroutine 4 10 40.0
pod 1 1 100.0
total 17 92 18.4


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