Change mialing flow and defere queueing of emails until schedule date.
This commit is contained in:
parent
e7b9a4480a
commit
ee4c7549f2
2 changed files with 112 additions and 17 deletions
|
|
@ -18,7 +18,17 @@ sub canCancel {
|
||||||
|
|
||||||
#----------------------------------------------------------------------------
|
#----------------------------------------------------------------------------
|
||||||
sub cancel {
|
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( {
|
$self->update( {
|
||||||
state => 'idle',
|
state => 'idle',
|
||||||
|
|
@ -117,8 +127,7 @@ sub getNextInSendQueue {
|
||||||
|
|
||||||
my $it = $class->getAllIterator( $session, {
|
my $it = $class->getAllIterator( $session, {
|
||||||
constraints => [
|
constraints => [
|
||||||
{ 'state=? or state=?' => [ 'sending', 'scheduled' ] },
|
{ 'state=? or state=?' => [ 'sending', 'queued' ] },
|
||||||
{ 'sendDate <= ?' => [ time() ] },
|
|
||||||
],
|
],
|
||||||
orderBy => "state = 'sending' desc, sendDate asc",
|
orderBy => "state = 'sending' desc, sendDate asc",
|
||||||
limit => 1,
|
limit => 1,
|
||||||
|
|
@ -178,6 +187,29 @@ sub queueTestEmails {
|
||||||
return;
|
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 {
|
sub queueEmails {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
|
|
@ -198,15 +230,60 @@ sub queueEmails {
|
||||||
return;
|
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 {
|
sub sendQueuedEmails {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my $timeLimit = shift;
|
my $timeLimit = shift;
|
||||||
my $session = $self->session;
|
my $session = $self->session;
|
||||||
|
|
||||||
return if $self->get('state') eq 'sent';
|
return unless $self->get('state') eq 'sending';
|
||||||
|
|
||||||
$self->update( { state => 'sending' } );
|
|
||||||
|
|
||||||
my $it = $self->getQueuedEmailIterator;
|
my $it = $self->getQueuedEmailIterator;
|
||||||
while ( my $email = $it->() ) {
|
while ( my $email = $it->() ) {
|
||||||
|
|
@ -215,8 +292,6 @@ sub sendQueuedEmails {
|
||||||
$email->send;
|
$email->send;
|
||||||
}
|
}
|
||||||
|
|
||||||
$self->update( { state => 'sent' } );
|
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -388,6 +463,29 @@ sub www_sendBatch {
|
||||||
return WebGUI::Mailing::Admin->new($session)->getAdminConsole->render( $f->print, $i18n->get('schedule mailing') );
|
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 {
|
sub www_sendBatchConfirm {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
|
|
@ -401,13 +499,7 @@ sub www_sendBatchConfirm {
|
||||||
my $asset = WebGUI::Asset->newByDynamicClass( $session, $self->get('assetId') );
|
my $asset = WebGUI::Asset->newByDynamicClass( $session, $self->get('assetId') );
|
||||||
croak "Cannot instaciate asset " . $self->get('assetId') unless $asset;
|
croak "Cannot instaciate asset " . $self->get('assetId') unless $asset;
|
||||||
|
|
||||||
my $userIds = $asset->getRecipients;
|
$self->schedule( $scheduled );
|
||||||
|
|
||||||
$self->queueEmails( $userIds );
|
|
||||||
$self->update( {
|
|
||||||
sendDate => $scheduled,
|
|
||||||
state => 'scheduled',
|
|
||||||
} );
|
|
||||||
|
|
||||||
return WebGUI::Mailing::Admin->new( $session )->www_view;
|
return WebGUI::Mailing::Admin->new( $session )->www_view;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -55,11 +55,14 @@ sub execute {
|
||||||
$email->send;
|
$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 ) ) {
|
while ( my $mailing = WebGUI::Mailing->getNextInSendQueue( $session ) ) {
|
||||||
return $self->WAITING(1) if time >= $maxTime;
|
return $self->WAITING(1) if time >= $maxTime;
|
||||||
|
|
||||||
$mailing->sendQueuedEmails( $maxTime ) || return $self->WAITING(1);
|
$mailing->send( $maxTime ) || return $self->WAITING(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $self->COMPLETE;
|
return $self->COMPLETE;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue