| 1 | |||
| 2 | package Email::Blaster::Clustered; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings 'all'; | ||
| 6 | use base 'Email::Blaster'; | ||
| 7 | use Cache::Memcached; | ||
| 8 | |||
| 9 | local $Email::Blaster::InstanceClass = __PACKAGE__; | ||
| 10 | |||
| 11 | |||
| 12 | #============================================================================== | ||
| 13 | sub new | ||
| 14 | { | ||
| 15 | my ($class) = shift; | ||
| 16 | |||
| 17 | my $s = $class->SUPER::new( @_ ); | ||
| 18 | |||
| 19 | # Just set up our memcached connection and defer to our super: | ||
| 20 | $s->{memd} = Cache::Memcached->new({ | ||
| 21 | 'servers' => [ $s->config->cluster->servers ], | ||
| 22 | 'debug' => $s->config->is_testing, | ||
| 23 | 'compress_threshold' => 10_000, | ||
| 24 | 'namespace' => 'EmailBlaster', | ||
| 25 | }); | ||
| 26 | |||
| 27 | return $s; | ||
| 28 | }# end run() | ||
| 29 | |||
| 30 | |||
| 31 | #============================================================================== | ||
| 32 | sub memd { shift->{memd} } | ||
| 33 | |||
| 34 | |||
| 35 | #============================================================================== | ||
| 36 | sub find_new_transmission | ||
| 37 | { | ||
| 38 | my ($s) = @_; | ||
| 39 | |||
| 40 | my $servers = $s->memd->get("connected_servers"); | ||
| 41 | my $param = join ",", ( map {"'$_'"} @$servers, '' ); | ||
| 42 | |||
| 43 | my $sth = Email::Blaster::Transmission->db_Main->prepare(<<"SQL"); | ||
| 44 | SELECT * | ||
| 45 | FROM transmissions | ||
| 46 | WHERE is_queued = 1 | ||
| 47 | AND ( | ||
| 48 | is_started = 0 | ||
| 49 | ) | ||
| 50 | OR ( | ||
| 51 | -- Clean up after another (possibly offline) server: | ||
| 52 | is_started = 1 | ||
| 53 | AND is_completed = 0 | ||
| 54 | AND blaster_hostname NOT IN ( $param ) | ||
| 55 | ) | ||
| 56 | OR ( | ||
| 57 | -- Clean up after ourselves, if we rebooted: | ||
| 58 | is_started = 1 | ||
| 59 | AND is_completed = 0 | ||
| 60 | AND blaster_hostname = ? | ||
| 61 | ) | ||
| 62 | ORDER BY queued_on DESC | ||
| 63 | LIMIT 0, 1 | ||
| 64 | SQL | ||
| 65 | $sth->execute( $s->config->hostname ); | ||
| 66 | return unless my ($trans) = Email::Blaster::Transmission->sth_to_objects( $sth ); | ||
| 67 | |||
| 68 | return $trans; | ||
| 69 | }# end find_new_transmission() | ||
| 70 | |||
| 71 | 1;# return true: | ||
| 72 | |||
| 73 | __END__ |