Change mialing flow and defere queueing of emails until schedule date.

This commit is contained in:
Martin Kamerbeek 2010-06-02 11:31:10 +02:00
parent e7b9a4480a
commit ee4c7549f2
2 changed files with 112 additions and 17 deletions

View file

@ -18,7 +18,17 @@ sub canCancel {
#----------------------------------------------------------------------------
sub cancel {
my $self = shift;
my $self = shift;
my $state = $self->get('state');
# Better bail out hard, then send wrong emails!
if ( $state ne 'scheduled' && $state ne 'queued' ) {
confess sprintf(
"Only scheduled and queued mailings can be cancelled, but mailing %s has state %s",
$self->getId,
$state,
);
};
$self->update( {
state => 'idle',
@ -117,8 +127,7 @@ sub getNextInSendQueue {
my $it = $class->getAllIterator( $session, {
constraints => [
{ 'state=? or state=?' => [ 'sending', 'scheduled' ] },
{ 'sendDate <= ?' => [ time() ] },
{ 'state=? or state=?' => [ 'sending', 'queued' ] },
],
orderBy => "state = 'sending' desc, sendDate asc",
limit => 1,
@ -178,6 +187,29 @@ sub queueTestEmails {
return;
}
#----------------------------------------------------------------------------
sub queue {
my $self = shift;
my $state = $self->get('state');
# Better bail out hard, then send wrong emails!
if ( $state ne 'scheduled' ) {
confess sprintf(
"Only schduled mailings can be queued, but mailing %s has state %s",
$self->getId,
$state,
);
};
$self->queueEmails( $self->getAsset->getRecipients );
$self->update( {
state => 'queued',
} );
return;
}
#----------------------------------------------------------------------------
sub queueEmails {
my $self = shift;
@ -198,15 +230,60 @@ sub queueEmails {
return;
}
#----------------------------------------------------------------------------
sub queueScheduled {
my $class = shift;
my $session = shift;
my $it = $class->getAllIterator( $session, {
constraints => [
{ 'state=?' => [ 'scheduled' ] },
{ 'sendDate <= ?' => [ time() ] },
],
limit => 1,
} );
while ( my $mailing = $it->() ) {
$mailing->queue;
}
return;
}
#----------------------------------------------------------------------------
sub send {
my $self = shift;
my $timeLimit = shift;
my $state = $self->get('state');
# Better bail out hard, then send wrong emails!
if ( $state ne 'queued' && $state ne 'sending' ) {
confess sprintf(
"Only queued and sending mailings can be sent, but mailing %s has state %s",
$self->getId,
$state,
);
};
$self->update( { state => 'sending' } );
my $complete = $self->sendQueuedEmails( $timeLimit );
if ( $complete ) {
$self->update( { state => 'sent' } );
return 1;
}
return;
}
#----------------------------------------------------------------------------
sub sendQueuedEmails {
my $self = shift;
my $timeLimit = shift;
my $session = $self->session;
return if $self->get('state') eq 'sent';
$self->update( { state => 'sending' } );
return unless $self->get('state') eq 'sending';
my $it = $self->getQueuedEmailIterator;
while ( my $email = $it->() ) {
@ -215,8 +292,6 @@ sub sendQueuedEmails {
$email->send;
}
$self->update( { state => 'sent' } );
return 1;
}
@ -388,6 +463,29 @@ sub www_sendBatch {
return WebGUI::Mailing::Admin->new($session)->getAdminConsole->render( $f->print, $i18n->get('schedule mailing') );
}
#----------------------------------------------------------------------------
sub schedule {
my $self = shift;
my $date = shift || croak 'schedule requires a sendDate';
my $state = $self->get('state');
# Better bail out hard, then send wrong emails!
if ( $state ne 'idle' ) {
confess sprintf(
"Only idle mailings can be scheduled, but mailing %s has state %s",
$self->getId,
$state,
);
};
$self->update( {
sendDate => $date,
state => 'scheduled',
} );
return;
}
#----------------------------------------------------------------------------
sub www_sendBatchConfirm {
my $self = shift;
@ -401,13 +499,7 @@ sub www_sendBatchConfirm {
my $asset = WebGUI::Asset->newByDynamicClass( $session, $self->get('assetId') );
croak "Cannot instaciate asset " . $self->get('assetId') unless $asset;
my $userIds = $asset->getRecipients;
$self->queueEmails( $userIds );
$self->update( {
sendDate => $scheduled,
state => 'scheduled',
} );
$self->schedule( $scheduled );
return WebGUI::Mailing::Admin->new( $session )->www_view;
}

View file

@ -55,11 +55,14 @@ sub execute {
$email->send;
}
# Fetch mailings that are scheduled to be sent one by one, and send them.
# Queue all mailings that are ready to send.
WebGUI::Mailing->queueScheduled( $session );
# Fetch mailings that are queued to be sent one by one, and send them.
while ( my $mailing = WebGUI::Mailing->getNextInSendQueue( $session ) ) {
return $self->WAITING(1) if time >= $maxTime;
$mailing->sendQueuedEmails( $maxTime ) || return $self->WAITING(1);
$mailing->send( $maxTime ) || return $self->WAITING(1);
}
return $self->COMPLETE;