lib/Monitoring/Spooler/Cmd/Command/text.pm untested

filter: Statementstmt 0.0% 0 / 64 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 94 CC 8 SCAR 26.2
CC
8
Cov
0%
_prepare_message_and_send
30.0
_cleanup
20.0
BEGIN
2.0
CRAP 13.8
1 package Monitoring::Spooler::Cmd::Command::text;
2 $Monitoring::Spooler::Cmd::Command::text::VERSION = '0.05';
3 BEGIN {
4 $Monitoring::Spooler::Cmd::Command::text::AUTHORITY = 'cpan:TEX';
5 }
6 # ABSTRACT: the text transport
7  
8 use 5.010_000;
9 use mro 'c3';
10 use feature ':5.10';
11  
12 use Moose;
13 use namespace::autoclean;
14  
15 # use IO::Handle;
16 # use autodie;
17 # use MooseX::Params::Validate;
18 # use Carp;
19 # use English qw( -no_match_vars );
20 # use Try::Tiny;
21  
22 # extends ...
23 extends 'Monitoring::Spooler::Cmd::SendingCommand';
24 # has ...
25 # with ...
26 # initializers ...
27  
28 # your code here ...
29 sub _prepare_message_and_send {
30 my $self = shift;
31 my $group_id = shift;
32 my $msg_ref = shift;
33  
34 # the message starts with the number of aggregated text messages
35 my $message = scalar(@{$msg_ref->{$group_id}}).' Alarms. ';
36 # aggregate messages into one string
37 # TODO LOW this could probably be improved, like:
38 # - aggregate repetitive messages
39 # - aggregate substrings (i.e. xyz is down, wvu is down => xyz, wvu are down)
40 # - ...
41 # But I admit that the added complexity _could_ very well hurt more
42 # than it benefits ;)
43 foreach my $msg (@{$msg_ref->{$group_id}}) {
44 $message .= $msg->{'msg'}.', ';
45 }
46  
47 # prepare stmt to fetch callee number from db
48 # until further notice we just grab the first one from the apt group
49 my $sql = 'SELECT number FROM notify_order WHERE group_id = ? ORDER BY id LIMIT 1';
50 my $sth_num = $self->dbh()->prepare($sql);
51 if(!$sth_num) {
52 $self->logger()->log( message => 'Could not prepare SQL '.$sql.' due to error: '.$self->dbh()->errstr, level => 'warning', );
53 return;
54 }
55 if(!$sth_num->execute($group_id)) {
56 $self->logger()->log( message => 'Could not execute statement: '.$sth_num->errstr, level => 'warning', );
57 next;
58 }
59 my $number = $sth_num->fetchrow_array();
60 # skip this group if there is nobody we can contact
61 # Could very well happen if an update fails or someone messes w/ the DB
62 if(!$number) {
63 $self->logger()->log( message => 'No contact number for group #'.$group_id, level => 'warning', );
64 return;
65 }
66  
67 if($self->_send_with_best_transport($number,$message)) {
68 return 1;
69 } else {
70 return;
71 }
72 }
73  
74 sub _cleanup {
75 my $self = shift;
76 my $success = shift;
77 my $group_id = shift;
78 my $msg_ref = shift;
79  
80 # this method does nothing if the call cycle failed
81 return unless $success;
82  
83 # prepare stmt to delete processed msg from queue
84 # will be executed below once the message has been sent successfully
85 my $sql = 'DELETE FROM msg_queue WHERE id = ?';
86 my $sth_del = $self->dbh()->prepare($sql);
87 if(!$sth_del) {
88 $self->logger()->log( message => 'Could not prepare SQL '.$sql.' due to error: '.$self->dbh()->errstr, level => 'warning', );
89 return;
90 }
91  
92 # if the message was sent successfully we delete all sent messages
93 # anything that got inserted into the queue after this script fetched
94 # its workload from the DB will NOT be deleted and sent in the
95 # next run
96 foreach my $msg (@{$msg_ref->{$group_id}}) {
97 if(!$sth_del->execute($msg->{'id'})) {
98 $self->logger()->log( message => "Failed to delete message from DB w/ error: ".$sth_del->errstr, level => 'error', );
99 }
100 }
101  
102 $sth_del->finish();
103  
104 return 1;
105 }
106  
107 sub _media_type {
108 return 'text';
109 }
110  
111 sub abstract {
112 return "Process all messages of media-type text in the notification queue";
113 }
114  
115 no Moose;
116 __PACKAGE__->meta->make_immutable;
117  
118 1;
119  
120 __END__