« lib/Monitoring/Spooler/Cmd/SendingCommand.pm
↑ Summary
lib/Monitoring/Spooler/Cmd/Command/text.pm »
| 1 | package Monitoring::Spooler::Cmd::Command::phone; | ||
| 2 | $Monitoring::Spooler::Cmd::Command::phone::VERSION = '0.05'; | ||
| 3 | BEGIN { | ||
| 4 | $Monitoring::Spooler::Cmd::Command::phone::AUTHORITY = 'cpan:TEX'; | ||
| 5 | } | ||
| 6 | # ABSTRACT: the voice 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 | has 'iterations' => ( | ||
| 26 | 'is' => 'rw', | ||
| 27 | 'isa' => 'Int', | ||
| 28 | 'lazy' => 1, | ||
| 29 | 'builder' => '_init_iterations', | ||
| 30 | ); | ||
| 31 | # with ... | ||
| 32 | # initializers ... | ||
| 33 | sub _init_iterations { | ||
| 34 | my $self = shift; | ||
| 35 | |||
| 36 | return $self->config()->get('Monitoring::Spooler::Phone::Iterations', { Default => 5, }); | ||
| 37 | } | ||
| 38 | |||
| 39 | # your code here ... | ||
| 40 | sub _prepare_message_and_send { | ||
| 41 | my $self = shift; | ||
| 42 | my $group_id = shift; | ||
| 43 | my $msg_ref = shift; | ||
| 44 | |||
| 45 | # get all users that ought to be notified | ||
| 46 | my $sql = 'SELECT id, number FROM notify_order WHERE group_id = ? ORDER BY id'; | ||
| 47 | my $sth = $self->dbh()->prepare($sql); | ||
| 48 | if(!$sth) { | ||
| 49 | $self->logger()->log( message => 'Failed to prepare statement from SQL: '.$sql.' w/ error: '.$self->dbh()->errstr, level => 'warning', ); | ||
| 50 | return; | ||
| 51 | } | ||
| 52 | if(!$sth->execute($group_id)) { | ||
| 53 | $self->logger()->log( message => 'Failed to execute statement w/ error: '.$sth->errstr, level => 'warning', ); | ||
| 54 | return; | ||
| 55 | } | ||
| 56 | my @numbers = (); | ||
| 57 | while(my ($id, $number) = $sth->fetchrow_array()) { | ||
| 58 | push(@numbers, { id => $id, number => $number }); | ||
| 59 | } | ||
| 60 | $sth->finish(); | ||
| 61 | |||
| 62 | if(!scalar(@numbers)) { | ||
| 63 | $self->logger()->log( message => 'Got no numbers to notify!', level => 'error', ); | ||
| 64 | return; | ||
| 65 | } | ||
| 66 | |||
| 67 | # just use the first supplied message | ||
| 68 | my $message = $msg_ref->{$group_id}->[0]->{'msg'}; | ||
| 69 | |||
| 70 | # five (default) iterations over the whole list should be enough ... | ||
| 71 | foreach my $it (1 .. $self->iterations()) { | ||
| 72 | $self->logger()->log( message => 'Starting call iteration #'.$it.' for group #'.$group_id, level => 'debug', ); | ||
| 73 | foreach my $num (@numbers) { | ||
| 74 | my $number = $num->{'number'}; | ||
| 75 | $self->logger()->log( message => 'Trying to send to '.$number, level => 'debug', ); | ||
| 76 | my $result = $self->_send_with_best_transport($number,$message); # "send" may be misleading here | ||
| 77 | # since we subclass "SendingCommand" it should at least make some sense. | ||
| 78 | # "SendingCommand" means anything the places a call or sends a text message | ||
| 79 | |||
| 80 | # this call was not successfull, try next one in queue | ||
| 81 | # defined coz' the DMTF value may be 0 ... | ||
| 82 | next unless defined($result); | ||
| 83 | |||
| 84 | # the callee pressed 3, so calls to this group will be paused for 30 minutes | ||
| 85 | if($result == 3) { # TODO should be configurable in config ... | ||
| 86 | my $sql = 'INSERT INTO paused_groups (group_id,until) VALUES(?,?)'; | ||
| 87 | my $sth = $self->dbh()->prepare($sql); | ||
| 88 | my $until = time() + (30*60); # TODO LOW should confiurable in db w/ default in config and fallback in code | ||
| 89 | $sth->execute($group_id,$until); | ||
| 90 | $sth->finish(); | ||
| 91 | return 3; | ||
| 92 | } else { | ||
| 93 | # unknown result code, better call the next one | ||
| 94 | $self->logger()->log( message => 'Unknown result code. Trying next one.', level => 'debug', ); | ||
| 95 | next; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | $self->logger()->log( message => 'All iterations failed.', level => 'debug', ); | ||
| 101 | |||
| 102 | return; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub _cleanup { | ||
| 106 | my $self = shift; | ||
| 107 | my $success = shift; | ||
| 108 | my $group_id = shift; | ||
| 109 | my $msg_ref = shift; | ||
| 110 | |||
| 111 | # this method does nothing if the call cycle failed | ||
| 112 | return unless $success; | ||
| 113 | |||
| 114 | # prepare stmt to delete processed msg from queue | ||
| 115 | # will be executed below once the message has been sent successfully | ||
| 116 | my $sql = 'DELETE FROM msg_queue WHERE group_id = ? AND type = ?'; | ||
| 117 | my $sth_del = $self->dbh()->prepare($sql); | ||
| 118 | if(!$sth_del) { | ||
| 119 | $self->logger()->log( message => 'Could not prepare SQL '.$sql.' due to error: '.$self->dbh()->errstr, level => 'warning', ); | ||
| 120 | return; | ||
| 121 | } | ||
| 122 | |||
| 123 | # if the message was sent successfully we delete all queued messages for this group | ||
| 124 | # anything that was inserted after this script has fetched it's initial workload | ||
| 125 | # will be deleted, too. Since this code shall only be executed | ||
| 126 | # on success don't want any calls during the next 30 (or so) minutes | ||
| 127 | if(!$sth_del->execute($group_id,'phone')) { | ||
| 128 | $self->logger()->log( message => 'Could not delete queued messages for group '.$group_id.' w/ error: '.$sth_del->errstr, level => 'warning', ); | ||
| 129 | } | ||
| 130 | $sth_del->finish(); | ||
| 131 | |||
| 132 | return 1; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub _media_type { | ||
| 136 | return 'phone'; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub abstract { | ||
| 140 | return "Process all messages of media-type phone in the notification queue"; | ||
| 141 | } | ||
| 142 | |||
| 143 | no Moose; | ||
| 144 | __PACKAGE__->meta->make_immutable; | ||
| 145 | |||
| 146 | 1; | ||
| 147 | |||
| 148 | __END__ |