Hopkins/Store/Backend.pm untested

filter: Statementstmt 0.0% 0 / 125 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 199 CC 17 SCAR 23.7
CC
17
Cov
0%
process_event_task_completed
20.0
process_event_task_orphaned
20.0
connect
12.0
CRAP 10.8
1 package Hopkins::Store::Backend;
2  
3 use strict;
4 use warnings;
5  
6 =head1 NAME
7  
8 Hopkins::Store::Backend - synchronous database services
9  
10 =head1 DESCRIPTION
11  
12 Hopkins::Store::Backend encapsulates database functionality
13 in a simple event loop. no POE services are utilized in the
14 event processing -- the loop is spawned off in a separate
15 process via POE::Child::Run in order to provide asynchronous
16 operation. hence, database queries may block in the backend
17 without affecting the rest of hopkins.
18  
19 Store::Backend communicates with the store session in the
20 parent process via message passing on stdin/stdout. these
21 messages are YAML encoded via POE::Filter::Reference.
22  
23 =cut
24  
25 use Class::Accessor::Fast;
26 use POE::Filter::Reference;
27  
28 use Hopkins::Store::Schema;
29  
30 use base 'Class::Accessor::Fast';
31  
32 __PACKAGE__->mk_accessors(qw(config filter schema));
33  
34 =head1 METHODS
35  
36 =over 4
37  
38 =item new
39  
40 create a new Hopkins::Store::Backend object for processing
41 requests received on STDIN. under normal operations, this
42 constructor will return. it will, however, return if there
43 are any errors processing events.
44  
45 =cut
46  
47 sub new
48 {
49 my $self = shift->SUPER::new(@_);
50  
51 open STATUS, '>&STDOUT';
52 open STDOUT, '>&STDERR';
53  
54 autoflush STATUS 1;
55  
56 $self->filter(new POE::Filter::Reference 'Storable');
57  
58 $self->connect and $self->loop;
59 }
60  
61 =item connect
62  
63 connects the schema's storage backend to the data source
64 described by the config object passed to the constructor.
65 this method returns the resulting schema object or undef
66 if there was an error.
67  
68 =cut
69  
70 sub connect
71 {
72 my $self = shift;
73  
74 $self->schema(undef);
75  
76 my $config = $self->config->fetch('database');
77 my $dsn = $config->{dsn};
78 my $user = $config->{user};
79 my $pass = $config->{pass};
80 my $opts = $config->{options};
81  
82 if (not defined $dsn) {
83 Hopkins->log_error('database/dsn not specified');
84 return undef;
85 }
86  
87 Hopkins->log_debug("attempting to connect to $dsn as $user");
88  
89 # attempt to connect to the schema. gracefully handle
90 # any exceptions that may occur.
91  
92 my $schema;
93  
94 eval {
95 # DBIx::Class is lazy. it will wait until the last
96 # possible moment to connect to the database. this
97 # prevents unnecessary database connections, but we
98 # but we want to immediately and gracefully handle
99 # any errors, so we force the connection now with
100 # the storage object's ensure_connected method.
101  
102 $schema = Hopkins::Store::Schema->connect($dsn, $user, $pass, $opts);
103 $schema->storage->ensure_connected;
104 };
105  
106 # if the connection was successful, supply the schema
107 # object to the Store::Backend object.
108  
109 if (my $err = $@) {
110 Hopkins->log_error("failed to connect to schema: $err");
111 } else {
112 Hopkins->log_debug('successfully connected to schema');
113 $self->schema($schema);
114 }
115  
116 return $self->schema;
117 }
118  
119 =item connected
120  
121 checks to make sure that the schema's storage backend is
122 connected to the data source. returns a truth value
123 indicating whether or not the schema is connected.
124  
125 =cut
126  
127 sub connected
128 {
129 my $self = shift;
130  
131 my $ok = $self->schema->storage->ensure_connected ? 1 : 0;
132  
133 Hopkins->log_error('lost connection to database') if not $ok;
134  
135 return $ok;
136 }
137  
138 =item loop
139  
140 main event processing loop. reads lines from STDIN and
141 passes them to POE::Filter for decoding. each decoded
142 item is passed to the process method individually for
143 further processing.
144  
145 under normal operating conditions, this method will never
146 return. however, if processing of an individual item fails,
147 this method will return to its caller, resulting in the
148 destruction of the Store::Backend object.
149  
150 =cut
151  
152 sub loop
153 {
154 my $self = shift;
155  
156 while (my $line = <STDIN>) {
157 my $aref = $self->filter->get([ $line ]);
158  
159 foreach my $href (@$aref) {
160 $self->process($href) or return;
161 }
162 }
163 }
164  
165 =item process
166  
167 process an action or event received from STDIN by the loop
168 method. process will return a boolean value indicating the
169 success or failure of the processing.
170  
171 =cut
172  
173 sub process
174 {
175 my $self = shift;
176 my $href = shift;
177  
178 $self->connected or return 0;
179  
180 if ($href->{event}) {
181 return $self->process_event($href->{event});
182 }
183  
184 return 1;
185 }
186  
187 =item process_event
188  
189 process an event received from STDIN. this method will
190 dispatch the event to an appropriate method as indicated
191 by the event name. if Store::Backend does not have an
192 appropriate method to handle the event, it will be ignored.
193 if the event is successfully processed, the Store session
194 will be notified by sending a message on STDOUT.
195  
196 =cut
197  
198 sub process_event
199 {
200 my $self = shift;
201 my $href = shift;
202  
203 my $id = $href->{id};
204 my $aref = $href->{contents};
205 my $event = $aref->[0];
206 my $method = "process_event_$event";
207  
208 if ($self->can($method) && $self->$method(@$aref[1..$#{$aref}])) {
209 print STATUS $_
210 foreach @{ $self->filter->put([ { eventproc => { id => $id } } ]) };
211 }
212  
213 return 1;
214 }
215  
216 =item process_event_task_enqueued
217  
218 a task was enqueued: record it in the database
219  
220 =cut
221  
222 sub process_event_task_enqueued
223 {
224 my $self = shift;
225 my $href = shift;
226  
227 my $rsTask = $self->schema->resultset('Task');
228 my $task = $rsTask->find_or_create({ id => $href->{id} });
229  
230 $task->name($href->{task}) if not defined $task->name;
231 $task->queue($href->{queue}) if not defined $task->queue;
232 $task->date_enqueued($href->{date_enqueued});
233 $task->date_to_execute($href->{date_to_execute});
234  
235 $task->update;
236 }
237  
238 =item process_event_task_started
239  
240 a task was started: record it in the database
241  
242 =cut
243  
244 sub process_event_task_started
245 {
246 my $self = shift;
247 my $href = shift;
248  
249 my $rsTask = $self->schema->resultset('Task');
250 my $task = $rsTask->find_or_create({ id => $href->{id} });
251  
252 $task->name($href->{task}) if not defined $task->name;
253 $task->queue($href->{queue}) if not defined $task->queue;
254 $task->started(1);
255 $task->date_started($href->{date_started});
256  
257 $task->update;
258 }
259  
260 =item process_event_task_completed
261  
262 a task was completed: record it in the database
263  
264 =cut
265  
266 sub process_event_task_completed
267 {
268 my $self = shift;
269 my $href = shift;
270  
271 my $rsTask = $self->schema->resultset('Task');
272 my $task = $rsTask->find_or_create({ id => $href->{id} });
273  
274 $task->name($href->{task}) if not defined $task->name;
275 $task->queue($href->{queue}) if not defined $task->queue;
276 $task->completed(1);
277 $task->succeeded($href->{succeeded});
278 $task->failed($href->{failed});
279 $task->aborted($href->{aborted});
280 $task->date_completed($href->{date_completed});
281 $task->create_related(output => { text => $href->{output} })
282 if defined $href->{output};
283  
284 $task->update;
285 }
286  
287 =item process_event_task_orphaned
288  
289 a task was lost: record it in the database
290  
291 =cut
292  
293 sub process_event_task_orphaned
294 {
295 my $self = shift;
296 my $href = shift;
297  
298 my $rsTask = $self->schema->resultset('Task');
299 my $task = $rsTask->find_or_create({ id => $href->{id} });
300  
301 $task->name($href->{task}) if not defined $task->name;
302 $task->queue($href->{queue}) if not defined $task->queue;
303 $task->completed(1);
304 $task->succeeded(0);
305 $task->failed(0);
306 $task->aborted(0);
307 $task->date_completed($href->{date_completed});
308 $task->create_related(output => { text => $href->{output} })
309 if defined $href->{output};
310  
311 $task->update;
312 }
313  
314 =back
315  
316 =head1 AUTHOR
317  
318 Mike Eldridge <diz@cpan.org>
319  
320 =head1 LICENSE
321  
322 =cut
323  
324 1;
325