| 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 wait_cycle | ||
| 33 | { | ||
| 34 | my ($s, $running, $processed) = @_; | ||
| 35 | |||
| 36 | $s->SUPER::wait_cycle( $running, $processed ); | ||
| 37 | |||
| 38 | # Let everyone know that we're still active...: | ||
| 39 | $s->memd->set( | ||
| 40 | # An indicator of this hostname's active status: | ||
| 41 | "Connected." . $s->config->hostname, | ||
| 42 | # True: | ||
| 43 | 1, | ||
| 44 | # Expires 60 seconds from now: | ||
| 45 | 60 | ||
| 46 | ); | ||
| 47 | }# end wait_cycle() | ||
| 48 | |||
| 49 | |||
| 50 | #============================================================================== | ||
| 51 | sub memd { shift->{memd} } | ||
| 52 | |||
| 53 | |||
| 54 | #============================================================================== | ||
| 55 | sub find_new_transmission | ||
| 56 | { | ||
| 57 | my ($s) = @_; | ||
| 58 | |||
| 59 | $s->memd->set( | ||
| 60 | # An indicator of this hostname's active status: | ||
| 61 | "Connected." . $s->config->hostname, | ||
| 62 | # True: | ||
| 63 | 1, | ||
| 64 | # Expires 60 seconds from now: | ||
| 65 | 60 | ||
| 66 | ); | ||
| 67 | my $servers = $s->memd->get("connected_servers"); | ||
| 68 | my $param = join ",", (1, | ||
| 69 | map {"'$_'"} | ||
| 70 | # Only the "Active" servers: | ||
| 71 | grep { $s->memd->get("Connected.$_") } | ||
| 72 | @$servers | ||
| 73 | ); | ||
| 74 | |||
| 75 | my $sth = Email::Blaster::Transmission->db_Main->prepare(<<"SQL"); | ||
| 76 | SELECT * | ||
| 77 | FROM transmissions | ||
| 78 | WHERE is_queued = 1 | ||
| 79 | AND ( | ||
| 80 | is_started = 0 | ||
| 81 | ) | ||
| 82 | OR ( | ||
| 83 | -- Clean up after another (possibly offline) server: | ||
| 84 | is_started = 1 | ||
| 85 | AND is_completed = 0 | ||
| 86 | AND blaster_hostname NOT IN ( $param ) | ||
| 87 | ) | ||
| 88 | OR ( | ||
| 89 | -- Clean up after ourselves, if we rebooted: | ||
| 90 | is_started = 1 | ||
| 91 | AND is_completed = 0 | ||
| 92 | AND blaster_hostname = ? | ||
| 93 | ) | ||
| 94 | ORDER BY queued_on DESC | ||
| 95 | LIMIT 0, 1 | ||
| 96 | SQL | ||
| 97 | $sth->execute( $s->config->hostname ); | ||
| 98 | return unless my ($trans) = Email::Blaster::Transmission->sth_to_objects( $sth ); | ||
| 99 | |||
| 100 | return $trans; | ||
| 101 | }# end find_new_transmission() | ||
| 102 | |||
| 103 | 1;# return true: | ||
| 104 | |||
| 105 | __END__ |