lib/Email/Blaster/Throttled.pm untested

filter: Statementstmt 0.0% 0 / 68 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 95 CC 4 SCAR 14.4
CC
4
Cov
0%
run
12.0
watch_maillog
6.0
new
2.0
CRAP 4.2
1  
2 package Email::Blaster::Throttled;
3  
4 use strict;
5 use warnings 'all';
6 use base 'Email::Blaster';
7 use Email::Blaster::Sendlog;
8 use POSIX 'ceil';
9 use Time::HiRes qw( usleep gettimeofday );
10 use HTTP::Date 'time2iso';
11  
12 local $Email::Blaster::InstanceClass = __PACKAGE__;
13  
14  
15 #==============================================================================
16 sub new
17 {
18 my $class = shift;
19
20 my $s = $class->SUPER::new( @_ );
21
22 $s->_load_class( $s->config->maillog_watcher );
23 $s->{maillog_watcher} = $s->config->maillog_watcher->new( );
24
25 return $s;
26 }# end new()
27  
28  
29 #==============================================================================
30 sub maillog_watcher { shift->{maillog_watcher} }
31  
32  
33 #==============================================================================
34 sub run
35 {
36 my ($s, $domain, $hourly_limit) = @_;
37
38 # For quick reference later:
39 $s->{domain} = $domain;
40 $s->{hourly_limit} = $hourly_limit;
41
42 # Wait until we have some sendlogs for our domain:
43 while( $s->continue_running )
44 {
45 warn "Looking for sendlogs to '@{[ $s->domain ]}'...\n";
46 my @sendlogs = $s->find_sendlogs();
47 unless( @sendlogs )
48 {
49 sleep(5);
50 next;
51 }# end unless()
52
53 # We found some sendlogs - send them...slowly:
54 my $delay = 3600 / $s->hourly_limit * 1000000;
55 warn "Delay for $domain: $delay\n";
56
57 my $last_time = gettimeofday();
58 foreach my $sendlog ( @sendlogs )
59 {
60 my $queued_as = $s->send_message(
61 $sendlog,
62 $sendlog->transmission
63 );
64 $sendlog->queued_as( $queued_as );
65 $sendlog->is_sent( 1 );
66 $sendlog->sent_on( time2iso() );
67 $sendlog->update;
68
69 # Wait until the message has been processed by our MTA:
70 my $is_successful = $s->maillog_watcher->watch_maillog(
71 $s->config->maillog_path,
72 $queued_as
73 );
74 $sendlog->is_successful( $is_successful );
75 $sendlog->update();
76
77 # How much time has passed since our last message was sent?:
78 my $diff = gettimeofday() - $last_time;
79  
80 # We might have some time left to sleep through:
81 if( $diff < $delay )
82 {
83 usleep( $delay - $diff );
84 }# end if()
85
86 # Mark the time that we finished with this sendlog:
87 $last_time = gettimeofday();
88 }# end foreach()
89 }# end while()
90 }# end run()
91  
92  
93 #==============================================================================
94 sub watch_maillog
95 {
96 my ($s, $queued_as) = @_;
97
98 my $path = $s->config->maillog_path;
99 warn "Waiting for the message ($queued_as) to go out...\n";
100 while( 1 )
101 {
102 my $wanted = `grep $queued_as $path*`;
103 unless( $wanted =~ m/\sstatus\=sent\s/i )
104 {
105 usleep(500000);
106 next;
107 }# end until()
108
109 # We have sent it:
110 last;
111 }# end while()
112 }# end watch_maillog()
113  
114  
115 #==============================================================================
116 sub domain { shift->{domain} }
117 sub hourly_limit { shift->{hourly_limit} }
118  
119  
120 #==============================================================================
121 sub find_sendlogs
122 {
123 my ($s) = @_;
124
125 my $sth = Email::Blaster::Sendlog->db_Main->prepare(<<"SQL");
126 SELECT sendlogs.*
127 FROM sendlogs
128 INNER JOIN transmissions
129 ON transmissions.transmission_id = sendlogs.transmission_id
130 WHERE sendlogs.throttled_domain = ?
131 AND sendlogs.is_sent = 0
132 ORDER BY transmissions.queued_on ASC
133 SQL
134 $sth->execute( $s->domain );
135
136 return Email::Blaster::Sendlog->sth_to_objects( $sth );
137 }# end find_sendlogs()
138  
139 1;# return true:
140