RWDE/Scheduler/Pending_action.pm untested

filter: Statementstmt 0.0% 0 / 115 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 157 CC 8 SCAR 18.6
CC
8
Cov
0%
create
12.0
process
12.0
reschedule
12.0
CRAP 6.5
1 # Object to handle pending system actions
2  
3 package RWDE::Scheduler::Pending_action;
4  
5 use strict;
6 use warnings;
7  
8 use Error qw(:try);
9  
10 use RWDE::Exceptions;
11  
12 use RWDE::Time;
13  
14 use base qw(RWDE::DB::Record);
15  
16 our ($db, $table, $index, $id, @fieldnames, @modifiable_fieldnames, %fields, %static_fields, %modifiable_fields, @static_fieldnames);
17  
18 use vars qw($VERSION);
19 $VERSION = sprintf "%d", q$Revision: 509 $ =~ /(\d+)/;
20  
21 BEGIN {
22 $table = 'pending_actions';
23 $id = 'action_id';
24 $index = 'pending_actions_action_id_seq';
25  
26 #all of the static fields present in the login table
27 %static_fields = (
28  
29 # Field => [Type, Descr]
30 action_id => [ 'int', 'Action ID' ],
31 action_created => [ 'timestamp', 'Date the action was created' ],
32 );
33  
34 #all of the fields allowed to be modified in the login table
35 %modifiable_fields = (
36  
37 # Field => [Type, Descr]
38 action_due => [ 'timestamp', 'Action due date' ],
39 action_started => [ 'timestamp', 'Action start time' ],
40 action_completed => [ 'timestamp', 'Action completed time' ],
41 action_type => [ 'char', 'Class namespace' ],
42 action_function => [ 'char', 'Action function' ],
43 action_scheduler_name => [ 'char', 'Preferred Scheduler' ],
44 action_exit_status => [ 'char', 'Exit Info' ],
45 action_params => [ 'hash', 'Action parameters' ],
46 action_repeat_frequency => [ 'char', 'Repeat frequency' ],
47 action_priority => [ 'int', 'Action priority' ],
48 action_parent_id => [ 'int', 'Group actions for identification purpose' ],
49 scheduler_id => [ 'int', 'Taken by scheduler' ],
50 );
51  
52 %fields = (%static_fields, %modifiable_fields);
53  
54 @static_fieldnames = sort keys %static_fields;
55 @modifiable_fieldnames = sort keys %modifiable_fields;
56 @fieldnames = sort keys %fields;
57 }
58  
59 ## @method object get_db()
60 # (Enter get_db info here)
61 # @return
62 sub get_db {
63 my ($self, $params) = @_;
64  
65 return 'default';
66 }
67  
68 sub create {
69 my ($self, $params) = @_;
70  
71 my @required = qw( action_type action_function action_params);
72 $self->check_params({ required => \@required, supplied => $params });
73  
74 my $action = $self->new();
75  
76 $action->action_type($$params{action_type});
77 $action->action_function($$params{action_function});
78 $action->action_params($$params{action_params});
79 $action->action_scheduler_name($$params{action_scheduler_name});
80 $action->action_due($$params{action_due});
81  
82 $action->action_priority($$params{action_priority}) if defined $$params{action_priority};
83 $action->action_parent_id($$params{action_parent_id}) if defined $$params{action_parent_id};
84  
85 $action->create_record();
86  
87 return $action;
88 }
89  
90 sub process {
91 my ($self, $params) = @_;
92  
93 $self->action_started(RWDE::Time->now());
94 $self->update_record;
95  
96 my $type = $self->action_type;
97 my $function = $self->action_function;
98  
99 #build the expression we are going to make
100 try {
101 my $string = $self->dehashify({ hash => $self->action_params });
102 $self->debug_info('devel', "Pending_action::process $type -> $function ($string) ");
103  
104 #make the call
105 $type->$function($self->action_params);
106  
107 $self->action_completed(RWDE::Time->now());
108 $self->scheduler_id(0); #indicates completed action
109 $self->action_exit_status('Successful');
110 $self->update_record;
111  
112 if ($self->is_recurring()) {
113 $self->reschedule();
114 }
115  
116 }
117  
118 catch Error with {
119 my $ex = shift;
120  
121 $self->syslog_msg('warn', $ex);
122  
123 RWDE::PostMaster->send_report_message(
124 {
125 info => $ex,
126 formdata => $self->action_params,
127 uri => 'Pending action doing: ' . $type . '->' . $function,
128 }
129 );
130  
131 #enable re-scheduling for the tasks that explicitly require it
132 if ($ex->is_retry() and !$self->is_recurring()) {
133 $self->reschedule();
134 }
135  
136 $self->action_exit_status($ex);
137 $self->update_record;
138 };
139  
140 return ();
141 }
142  
143 sub cleanup {
144 my ($self, $params) = @_;
145  
146 if (defined $self->action_completed) {
147  
148 # The action was completed, but the record wasn't released
149 # Mark as completed
150 $self->scheduler_id(0);
151 }
152  
153 else {
154  
155 # The action has not completed
156 # return the action to the pool to be picked up by a new scheduler
157 $self->scheduler_id('NULL');
158 }
159  
160 $self->update_record;
161  
162 return ();
163 }
164  
165 sub is_recurring {
166 my ($self, $params) = @_;
167  
168 return (defined $self->action_repeat_frequency);
169 }
170  
171 sub reschedule {
172 my ($self, $params) = @_;
173  
174 my $interval;
175  
176 if (defined $self->action_repeat_frequency) {
177 $interval = $self->action_repeat_frequency;
178 }
179  
180 elsif (defined $$params{interval}) {
181 $interval = $$params{interval};
182 }
183  
184 else {
185 $interval = '15 minutes';
186 }
187  
188 my $new_action = $self->new;
189  
190 $new_action->action_type($self->action_type);
191 $new_action->action_function($self->action_function);
192 $new_action->action_params($self->action_params);
193 $new_action->action_scheduler_name($self->action_scheduler_name);
194 $new_action->action_repeat_frequency($self->action_repeat_frequency);
195  
196 my $new_runtime = RWDE::Time->fetch_time({ timestamp => 'NOW()', interval => $interval });
197 $new_action->action_due($new_runtime);
198  
199 $new_action->create_record();
200  
201 return $new_action;
202 }
203  
204 sub count_outstanding {
205 my ($self, $params) = @_;
206  
207 return $self->count_query({ query => "action_due < NOW() AND scheduler_id IS NULL AND action_repeat_frequency IS NULL", });
208 }
209  
210 1;