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 => {
fieldType => 'textarea',
serialize => 1,
defaultValue => {},
},
sendDate => {
fieldType => 'dateTime',
@ -56,6 +57,22 @@ sub getAsset {
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 {
my $self = shift;
my $db = $self->session->db;
@ -124,6 +141,27 @@ sub queueEmails {
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 {
my $self = shift;
my $session = $self->session;

View file

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

View file

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