blib/lib/Daemonise/Plugin/Event.pm untested

filter: Statementstmt 0.0% 0 / 125 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 240 CC 20 SCAR 51.2
CC
20
Cov
0%
create_event
272.0
stop_event
30.0
BEGIN
2.0
CRAP 167.2
1 package Daemonise::Plugin::Event;
2  
3 use 5.010;
4 use Mouse::Role;
5 use experimental 'smartmatch';
6 use Scalar::Util qw(looks_like_number);
7  
8 # ABSTRACT: Daemonise Event plugin
9  
10 use Carp;
11  
12 BEGIN {
13 with("Daemonise::Plugin::CouchDB");
14 with("Daemonise::Plugin::RabbitMQ");
15 }
16  
17  
18 has 'event_db' => (
19 is => 'rw',
20 isa => 'Str',
21 lazy => 1,
22 default => sub { 'events' },
23 );
24  
25  
26 has 'event_queue' => (
27 is => 'rw',
28 isa => 'Str',
29 lazy => 1,
30 default => sub { 'event' },
31 );
32  
33  
34 after 'configure' => sub {
35 my ($self, $reconfig) = @_;
36  
37 $self->log("configuring Events plugin") if $self->debug;
38  
39 if (ref($self->config->{event}) eq 'HASH') {
40 foreach my $conf_key ('db', 'queue') {
41 my $attr = "event_" . $conf_key;
42 $self->$attr($self->config->{event}->{$conf_key})
43 if defined $self->config->{event}->{$conf_key};
44 }
45 }
46  
47 $self->couchdb->db($self->event_db);
48  
49 return;
50 };
51  
52  
53 sub create_event {
54 my ($self, $type, $offset, $data) = @_;
55  
56 unless (ref \$type eq 'SCALAR') {
57 $self->log("event type must be a string!");
58 return;
59 }
60  
61 unless (ref \$offset eq 'SCALAR' and looks_like_number($offset)) {
62 $self->log("offset must be a number!");
63 return;
64 }
65  
66 # data is optional but has to be a hash ref if exists
67 if ($data) {
68 unless (ref $data eq 'HASH') {
69 $self->log("data must be a hash ref!");
70 return;
71 }
72 }
73  
74 # base event keys/values
75 my $now = time;
76 my $event = {
77 type => 'event',
78 timestamp => $now,
79 };
80  
81 # add mapped stuff
82 given ($type) {
83 when ('restart_billing') {
84 unless (exists $data->{key}
85 and ref \$data->{key} eq 'SCALAR'
86 and $data->{key})
87 {
88 $self->log('data->key missing or empty');
89 return;
90 }
91  
92 $event = {
93 %$event,
94 backend => 'internal',
95 object => 'billing',
96 action => 'restart',
97 status => 'none',
98 job_id => $data->{key},
99 };
100 }
101 when ('start_billing') {
102 unless (exists $data->{key}
103 and ref \$data->{key} eq 'SCALAR'
104 and $data->{key})
105 {
106 $self->log('data->key missing or empty');
107 return;
108 }
109  
110 $event = {
111 %$event,
112 backend => 'internal',
113 object => 'billing',
114 action => 'start',
115 status => 'none',
116 job_id => $data->{key},
117 };
118 }
119 when ('continue') {
120 unless (exists $data->{key}
121 and ref \$data->{key} eq 'SCALAR'
122 and $data->{key})
123 {
124 $self->log('data->key missing or empty');
125 return;
126 }
127  
128 $event = {
129 %$event,
130 backend => 'internal',
131 object => 'none',
132 action => 'continue',
133 status => 'none',
134 job_id => $data->{key},
135 };
136 }
137 when ('restart') {
138 unless (exists $data->{key}
139 and ref \$data->{key} eq 'SCALAR'
140 and $data->{key})
141 {
142 $self->log('data->key missing or empty');
143 return;
144 }
145  
146 $event = {
147 %$event,
148 backend => 'internal',
149 object => 'none',
150 action => 'restart',
151 status => 'none',
152 job_id => $data->{key},
153 };
154 }
155 when ('backend_call') {
156 unless (exists $data->{queue}
157 and exists $data->{command}
158 and exists $data->{options})
159 {
160 $self->log(
161 '"queue", "command" and "options" keys must be present');
162 return;
163 }
164  
165 # include all keys from $data hash first here to have "command",
166 # "queue" and "options" available later
167 $event = {
168 %$data,
169 %$event,
170 backend => 'internal',
171 object => 'none',
172 action => 'call',
173 status => 'none',
174 };
175 }
176 when (/^[a-z0-9]+_[a-z0-9]+_[a-z0-9]+_[a-z0-9]+$/) {
177 my ($backend, $object, $action, $status) = split(/_/, $type);
178 $event = {
179 %$data,
180 %$event,
181 backend => $backend,
182 object => $object,
183 action => $action,
184 status => $status,
185 };
186 }
187 default {
188 $self->log("unsupported event type: $type");
189 return;
190 }
191 }
192  
193 if ($data) {
194 $event->{parsed} = $data->{parsed};
195 $event->{raw} = $data->{raw};
196 }
197  
198 # offset is number of hours or unix timestamp
199 if ($offset) {
200 $event->{when} =
201 length($offset) >= 10 ? int $offset : $now + ($offset * 60 * 60);
202  
203 my $old_db = $self->couchdb->db;
204 $self->couchdb->db($self->event_db);
205 my ($id, $rev) = $self->couchdb->put_doc({ doc => $event });
206 $self->couchdb->db($old_db);
207  
208 if ($self->couchdb->has_error) {
209 $self->log("failed creating $type event: " . $self->couchdb->error);
210 return;
211 }
212  
213 # graph new event
214 if ($self->can('graph')) {
215 my $service = join('.',
216 $event->{backend}, $event->{object},
217 $event->{action}, $event->{status});
218 $self->graph("event.$service", 'new', 1);
219 }
220  
221 return $id;
222 }
223 else {
224 my $frame = {
225 data => {
226 command => "event_add",
227 options => $event,
228 },
229 };
230 my $res = $self->queue($self->event_queue, $frame);
231 if ($res->{error}) {
232 $self->log(
233 "failed to create $type event via queue: " . $res->{error});
234 return;
235 }
236  
237 return $res->{response}->{event_id};
238 }
239 }
240  
241  
242 sub stop_event {
243 my ($self, $id, $reason) = @_;
244  
245 return unless $id;
246  
247 my $old_db = $self->couchdb->db;
248 $self->couchdb->db($self->event_db);
249 my $event = $self->couchdb->get_doc({ id => $id });
250  
251 if ($event) {
252 unless (exists $event->{processed} and $event->{processed}) {
253 $event->{processed} = time;
254 $event->{error} = $reason || ('stopped by ' . $self->name);
255  
256 $self->couchdb->put_doc({ doc => $event });
257 $self->couchdb->db($old_db);
258  
259 $self->log("stopped event $id");
260  
261 # graph stopped event
262 if ($self->can('graph')) {
263 my $service = join('.',
264 $event->{backend}, $event->{object},
265 $event->{action}, $event->{status});
266 $self->graph("event.$service", 'stopped', 1);
267 }
268 }
269  
270 return 1;
271 }
272  
273 $self->couchdb->db($old_db);
274  
275 return;
276 }
277  
278 1;
279  
280 __END__