File Coverage

blib/lib/cPanel/TaskQueue/Processor.pm
Criterion Covered Total %
statement 58 61 95.0
branch 28 32 87.5
condition 11 14 78.5
subroutine 13 13 100.0
pod 9 9 100.0
total 119 129 92.2


line stmt bran cond sub pod time code
1             package cPanel::TaskQueue::Processor;
2             {
3             $cPanel::TaskQueue::Processor::VERSION = '0.606';
4             }
5              
6             # cpanel - cPanel/TaskQueue/Processor.pm Copyright(c) 2014 cPanel, Inc.
7             # All rights Reserved.
8             # copyright@cpanel.net http://cpanel.net
9             #
10             # Redistribution and use in source and binary forms, with or without
11             # modification, are permitted provided that the following conditions are met:
12             # * Redistributions of source code must retain the above copyright
13             # notice, this list of conditions and the following disclaimer.
14             # * Redistributions in binary form must reproduce the above copyright
15             # notice, this list of conditions and the following disclaimer in the
16             # documentation and/or other materials provided with the distribution.
17             # * Neither the name of the owner nor the names of its contributors may
18             # be used to endorse or promote products derived from this software
19             # without specific prior written permission.
20             #
21             # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22             # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23             # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24             # DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY
25             # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26             # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27             # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28             # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29             # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30             # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31              
32 37     37   66652 use strict;
  37         71  
  37         40566  
33              
34             #use warnings;
35              
36             {
37              
38             sub new {
39 17     17 1 2939 my ($class) = @_;
40 17         158 return bless {}, $class;
41             }
42              
43             sub get_timeout {
44 23     23 1 675 my ($self) = @_;
45 23         828 return;
46             }
47              
48             sub is_dupe {
49 150     150 1 3917 my ( $self, $a, $b ) = @_;
50              
51 150 100       482 return unless $a->command() eq $b->command();
52 131         481 my @a_args = $a->args();
53 131         394 my @b_args = $b->args();
54 131 100       612 return unless @a_args == @b_args;
55              
56 91         321 foreach my $i ( 0 .. $#a_args ) {
57 116 100       649 return unless $a_args[$i] eq $b_args[$i];
58             }
59              
60 9         118 return 1;
61             }
62              
63             sub overrides {
64 137     137 1 206 my ( $self, $new, $old ) = @_;
65              
66 137         583 return;
67             }
68              
69             sub is_valid_args {
70 93     93 1 147 my ( $self, $task ) = @_;
71              
72 93         332 return 1;
73             }
74              
75             sub process_task {
76 1     1 1 529 my ( $self, $task, $logger ) = @_;
77              
78 1 50       5 if ($logger) {
79 0         0 $logger->throw("No processing has been specified for this task.\n");
80             }
81             else {
82 1         7 die "No processing has been specified for this task.\n";
83             }
84             }
85              
86             sub deferral_tags {
87 8     8 1 74 my ( $self, $task ) = @_;
88 8         30 return;
89             }
90              
91             sub is_task_deferred {
92 83     83 1 134 my ( $self, $task, $defer_hash ) = @_;
93 83 100 100     430 return unless $defer_hash && keys %{$defer_hash};
  68         326  
94              
95 61         170 foreach my $tag ( $self->deferral_tags($task) ) {
96 95 100       351 return 1 if exists $defer_hash->{$tag};
97             }
98              
99 8         95 return;
100             }
101              
102             # Utility functions
103             sub checked_system {
104 7     7 1 13428 my ( $self, $args ) = @_;
105 7 100       52 die "Argument must be a hashref." unless ref $args eq 'HASH';
106 6 100       35 die "Missing required 'logger' argument." unless $args->{'logger'};
107 5 100 66     51 $args->{'logger'}->throw("Missing required 'cmd' argument.")
108             unless defined $args->{'cmd'} && length $args->{'cmd'};
109 4 100 66     31 $args->{'logger'}->throw("Missing required 'name' argument.")
110             unless defined $args->{'name'} && length $args->{'name'};
111 3   50     26 $args->{'args'} ||= [];
112              
113 3         7 my $rc = system $args->{'cmd'}, @{ $args->{'args'} };
  3         11921  
114 3 100       139 return 0 unless $rc;
115              
116 2         7 my $message;
117 2 100       61 if ( $rc == -1 ) {
    50          
118 1         13 $message = "Failed to run $args->{'name'}";
119             }
120             elsif ( $rc & 127 ) {
121 0         0 $message = "$args->{'name'} dies with signal: " . ( $rc & 127 );
122             }
123             else {
124 1         35 $message = "$args->{'name'} exited with value " . ( $rc >> 8 );
125             }
126 2         66 $args->{'logger'}->warn($message);
127              
128 2         100 return $rc;
129             }
130             }
131              
132             # To simplify use, here is a simple module that turns a code ref into a valid
133             # TaskQueue::Processor.
134             {
135              
136             package cPanel::TaskQueue::Processor::CodeRef;
137             {
138             $cPanel::TaskQueue::Processor::CodeRef::VERSION = '0.606';
139             }
140 37     37   292 use base 'cPanel::TaskQueue::Processor';
  37         85  
  37         15247  
141              
142             {
143              
144             sub new {
145 53     53   1398 my ( $class, $args_ref ) = @_;
146 53 50       300 die "Args must be a hash ref.\n" unless 'HASH' eq ref $args_ref;
147              
148 53 100 100     5778 unless ( exists $args_ref->{code} and 'CODE' eq ref $args_ref->{code} ) {
149 3         17 die "Missing required code parameter.\n";
150             }
151 50         450 return bless { proc => $args_ref->{code} }, $class;
152             }
153              
154             # Override the default behavior to call the stored coderef with the
155             # arguments supplied in the task. Return whatever the coderef returns.
156             sub process_task {
157 2     2   5 my ( $self, $task, $logger ) = @_;
158              
159             eval {
160 2         12 $self->{proc}->( $task->args() );
161 2         11 1;
162 2 50       4 } or do {
163 0         0 $logger->throw($@);
164             };
165              
166 2         39 return 0;
167             }
168             }
169             }
170              
171             1;
172              
173             __END__