blib/lib/Minion/Task.pm

filter: Statementstmt 31.8% 22 / 69 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 18.7% 3 / 16 Podpod 100% 5 / 5 totaltotal 27.7% 30 / 108 CC 10 SCAR 22.3
CC
10
Cov
26%
tagify
30.0
start
12.0
__ANON__
6.0
CRAP 9.3
↑ Summary
1 package Minion::Task;
2 2 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 ▶use Mojo::Loader qw(load_class);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Mojo::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '0.0.3';
8  
9 2 has [qw/id guard parent_id user/];
10  
11 0 ▶has 'app' => sub { Mojo::Server->new->build_app('Mojo::HelloWorld') }, weak => 1;
Subroutine __ANON__: not called CC=1 SCAR=1.2
12  
13 0 ▶has 'args' => sub { {} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
14  
15 0 ▶has 'chain' => sub { shift->args->{ chain } || [] };
Subroutine __ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->args->{"chain"} or [] 0 0 0
16  
17 0 ▶has 'children' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
18 0 my $self = shift;
19  
20 0 my @children = @{ $self->subtasks };
21 0 push(@children, @{ $self->chain });
22  
23 0 return \@children;
24 2 };
25  
26 2 has 'error' => 'Failed.';
27  
28 0 ▶has 'failed' => sub { 0 };
Subroutine __ANON__: not called CC=1 SCAR=1.2
29  
30 2 has 'finish' => 'Action complete.';
31  
32 0 ▶has 'options' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
33 0 my $self = shift;
34 0 my $options = {
35 queue => 'default',
36 };
37  
38 0 ▶ if ($self->parent_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->parent_id) 0 0 0
39 0 $options->{ parents } = [$self->parent_id];
40 }
41  
42 0 return $options;
43 2 };
44  
45 0 ▶has 'subtasks' => sub { [] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
46  
47 0 ▶has 'tags' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
48 0 my $self = shift;
49  
50 0 my @tags = $self->tagify($self->args);
51  
52 0 return \@tags;
53 2 };
54  
55  
56 =head2 dispatch
57  
58 Dispatch the task
59  
60 =cut
61  
62 0 ▶sub dispatch {
Subroutine dispatch: not called CC=1 SCAR=6.9
Pod documented
63 0 my $self = shift;
64  
65 0 return $self->start;
66 }
67  
68 =head2 processChain
69  
70 Process the chain of tasks
71  
72 =cut
73  
74 0 ▶sub processChain {
Subroutine processChain: not called CC=2 SCAR=17.9
Pod documented
75 0 my $self = shift;
76  
77 0 my @children = @{ $self->children };
78 0 my $task = shift(@children);
79  
80 0 my $e = load_class($task);
81  
82 0 ▶ $self->app->log->fatal("Loading '$task' failed: $e") if ($e);
Branch: 1 branch
BranchTrueFalseTotal
if $e 0 0 0
83  
84 0 $task->new(app => $self->app, args => $self->args, parent_id => $self->id)
85 ->withChain(\@children)
86 ->dispatch;
87 }
88  
89 =head2 start
90  
91 Start the task
92  
93 =cut
94  
95 0 ▶sub start {
Subroutine start: not called CC=3 SCAR=24.8
Pod documented
96 0 my $self = shift;
97  
98 0 my $ok = $self->run(@_);
99  
100 # When current task was successful,
101 # and there are children tasks defined,
102 # try to process them
103 0 ▶ if ($ok && scalar(@{ $self->children })) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok and scalar @{$self->children;}) 0 0 0
Condition0%0/2
$ok and scalar @{$self->children;}
!ll
00
MC/DC0%0/1
$ok and scalar @{$self->children;}
$ok
Truth table0%0/2
A
A= $ok
Aresult
00
11
104 0 $self->processChain;
105 }
106  
107 0 return $ok;
108 }
109  
110 =head2 tag
111  
112 Convert name and value to a tag
113  
114 =cut
115  
116 0 ▶sub tag {
Subroutine tag: not called CC=1 SCAR=6.9
Pod documented
117 0 my ($self, $name, $value) = @_;
118  
119 0 return sprintf("%s %s", $name, $value);
120 }
121  
122 =head2 tagify
123  
124 Convert a hash to an array of tags
125  
126 =cut
127  
128 0 ▶sub tagify {
Subroutine tagify: not called CC=5 SCAR=34.0
Pod documented
129 0 my ($self, $args, $prefix) = @_;
130 0 ▶ $prefix ||= '';
Condition0%0/2
$prefix ||= ""
l!l
00
MC/DC0%0/1
$prefix ||= ""
$prefix
Truth table0%0/2
A
A= $prefix
Aresult
11
00
131  
132 0 my @tags;
133  
134 0 for my $key (sort(keys(%{ $args }))) {
135 0 ▶ if (ref($args->{ $key }) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $args->{$key} eq 'HASH') { } 0 0 0
elsif (ref $args->{$key} eq 'ARRAY') { } 0 0 0
136 0 push(@tags, $self->tagify($args->{ $key }, $prefix . $key . ' '));
137 } elsif (ref($args->{ $key }) eq 'ARRAY') {
138 0 push(@tags, $self->tag($prefix . $key, join(', ', @{ $args->{ $key } })));
139 } else {
140 0 push(@tags, $self->tag($prefix . $key, $args->{ $key }));
141 }
142 }
143  
144 0 return @tags;
145 }
146  
147 2 1;
148  
149 =encoding utf8
150  
151 =head1 NAME
152  
153 Minion::Task - A task boilerplate for Minion
154  
155 =head1 SYNOPSIS
156  
157 package MyApp::Tasks::HelloWorld;
158 use Mojo::Base 'Minion::Task';
159  
160 ... defined your own logic here ...
161  
162 =head1 DESCRIPTION
163  
164 L<Minion::Task> is a package that provides a way of handling minion tasks
165  
166 =head1 ATTRIBUTES
167  
168 L<Minion::Task> inherits all attributes from L<Mojo::Base>.
169  
170 =head1 METHODS
171  
172 L<Minion::Task> inherits all methods from L<Mojo::Base> and implements
173 the following new ones.
174  
175 =head2 dispatch
176  
177 $task->dispatch;
178  
179 Is basically a link to start method.
180  
181 =head2 processChain
182  
183 $task->processChain;
184  
185 If current task has subtasks or if there's something defined in the args->{ chain },
186 those tasks will be dispatched.
187  
188 =head2 start
189  
190 $task->start;
191  
192 Start processing the task.
193  
194 =head1 SEE ALSO
195  
196 L<Mojolicious>, L<Mojolicious::Plugin::Minion::Overview>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
197  
198 =cut
↑ Summary