Improve sending of mailings.

This commit is contained in:
Martin Kamerbeek 2010-05-20 11:59:07 +02:00
parent e7dd066630
commit b7b1811387
3 changed files with 60 additions and 16 deletions

View file

@ -29,6 +29,7 @@ sub crud_definition {
configuration => { configuration => {
fieldType => 'textarea', fieldType => 'textarea',
serialize => 1, serialize => 1,
defaultValue => {},
}, },
sendDate => { sendDate => {
fieldType => 'dateTime', fieldType => 'dateTime',
@ -56,6 +57,22 @@ sub getAsset {
return $asset; return $asset;
} }
sub getNextInSendQueue {
my $class = shift;
my $session = shift;
my $it = $class->getAllIterator( $session, {
constraints => [
{ 'state=? or state=?' => [ 'sending', 'scheduled' ] },
{ 'sendDate <= ?' => [ time() ] },
],
orderBy => "state = 'sending' desc, sendDate asc",
limit => 1,
} );
return $it->();
}
sub getStatusLine { sub getStatusLine {
my $self = shift; my $self = shift;
my $db = $self->session->db; my $db = $self->session->db;
@ -124,6 +141,27 @@ sub queueEmails {
return; return;
} }
sub sendQueuedEmails {
my $self = shift;
my $timeLimit = shift;
my $session = $self->session;
return if $self->get('state') eq 'sent';
$self->update( { state => 'sending' } );
my $it = WebGUI::Mailing::Email->getQueued( $session, $self->getId );
while ( my $email = $it->() ) {
return if $timeLimit && time >= $timeLimit;
$email->send;
}
$self->update( { state => 'sent' } );
return 1;
}
sub www_delete { sub www_delete {
my $self = shift; my $self = shift;
my $session = $self->session; my $session = $self->session;

View file

@ -30,7 +30,7 @@ sub crud_definition {
}, },
status => { status => {
fieldType => 'text', fieldType => 'text',
defaultValue => 'queued', # Allowed are: 'queued', 'sent', 'error' defaultValue => 'queued', # Allowed are: 'queued', 'sent', 'error', 'bounced'
}, },
errorMessage => { errorMessage => {
fieldType => 'text', fieldType => 'text',
@ -40,6 +40,12 @@ sub crud_definition {
fieldType => 'yesNo', fieldType => 'yesNo',
defaultValue => 1, # For safety: explicitly turn this off to send real emails defaultValue => 1, # For safety: explicitly turn this off to send real emails
}, },
sendDate => {
fieldType => 'dateTime',
},
sentTo => {
fieldType => 'text',
},
); );
$definition->{ properties } = { $definition->{ properties } = {
@ -73,19 +79,16 @@ sub getQueuedTestEmails {
return $it; return $it;
} }
sub getSendableEmails { sub getQueued {
my $class = shift; my $class = shift;
my $session = shift; my $session = shift;
my $mailingId = shift;
my $it = $class->getAllIterator( $session, { my $it = $class->getAllIterator( $session, {
joinUsing => [
{ 'WGMailing' => 'mailingId' },
],
constraints => [ constraints => [
{ 'mailingId=?' => [ $mailingId ] },
{ 'isTest=?' => [ 0 ] }, { 'isTest=?' => [ 0 ] },
{ 'status=?' => [ 'queued' ] }, { 'status=?' => [ 'queued' ] },
{ 'state=?' => [ 'scheduled' ] },
{ 'sendDate <= ?' => [ time ] },
], ],
} ); } );
@ -180,7 +183,11 @@ sub send {
$self->error( "Mail couldn't be sent by WebGUI::Mail::Send" ); $self->error( "Mail couldn't be sent by WebGUI::Mail::Send" );
} }
else { else {
$self->update( { status => 'sent' } ); $self->update( {
status => 'sent',
sendDate => time,
sentTo => $to,
} );
} }
return; return;

View file

@ -47,7 +47,7 @@ sub execute {
my $maxTime = time + $self->getTTL; my $maxTime = time + $self->getTTL;
# For now only process test mails. # First send all queued test emails.
my $it = WebGUI::Mailing::Email->getQueuedTestEmails( $session ); my $it = WebGUI::Mailing::Email->getQueuedTestEmails( $session );
while ( my $email = $it->() ) { while ( my $email = $it->() ) {
return $self->WAITING(1) if time >= $maxTime; return $self->WAITING(1) if time >= $maxTime;
@ -55,14 +55,13 @@ sub execute {
$email->send; $email->send;
} }
my $it = WebGUI::Mailing::Email->getSendableEmails( $session ); # Fetch mailings that are scheduled to be sent one by one, and send them.
while ( my $email = $it->() ) { while ( my $mailing = WebGUI::Mailing->getNextInSendQueue( $session ) ) {
$session->log->warn( "processing" . $email->getId );
return $self->WAITING(1) if time >= $maxTime; return $self->WAITING(1) if time >= $maxTime;
$email->send; $mailing->sendQueuedEmails( $maxTime ) || return $self->WAITING(1);
} }
return $self->COMPLETE; return $self->COMPLETE;
} }