Skip to content

Commit

Permalink
Kernel::System::MailQueue::Send now ensures number of attempts gettin…
Browse files Browse the repository at this point in the history
…g increased before sending an email in case the process dies.
  • Loading branch information
jepf committed Dec 14, 2023
1 parent 9ce64ce commit 832e49b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 6.5.6 2024-??-??
- 2023-10-24 Kernel::System::MailQueue::Send now increases number of attempts before sending an email. This prevents the attempts not being increased if the process dies while trying to send an email.

# 6.5.5 2023-12-13
- 2023-12-11 Increased size of user_id column in table customer_user_customer.
- 2023-12-07 Customer detail search cache for dynamic field values will now be cleared if a customer will be added or updated.
Expand Down
62 changes: 57 additions & 5 deletions Kernel/System/MailQueue.pm
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,58 @@ sub Send {
$Param{CommunicationLogObject} = $Self->_GetCommunicationLog( %Param, );
$Param{CommunicationID} = $Param{CommunicationLogObject}->CommunicationIDGet();

# Increase number of attempts and delete item if limit has been reached.
$Param{Attempts} //= 0;
$Param{Attempts}++;
my %UpdateData = (
Attempts => $Param{Attempts},
);
my $ItemUpdateOK = $Self->Update(
Filters => {
ID => $Param{ID},
},
Data => \%UpdateData,
);

if ( !$ItemUpdateOK ) {
my $LogMessage = sprintf(
'Error while updating mail queue element "%s" with "%s"!',
$Param{ID},
join( ', ', map { $_ . '=' . $UpdateData{$_} } sort keys %UpdateData ),
);

$LogObject->Log(
Priority => 'error',
Message => $LogMessage,
);

$Param{CommunicationLogObject}->ObjectLog(
ObjectLogType => 'Message',
Priority => 'Error',
Key => 'Kernel::System::MailQueue',
Value => $LogMessage,
);

return;
}

my $Config = $Kernel::OM->Get('Kernel::Config')->Get('MailQueue');
my $MaxAttempts = $Config->{ItemMaxAttempts};
if ( $Param{Attempts} > $MaxAttempts ) {

# This will lead to _SendError executing the code for reaching limit of attempts
# and hence deleting the item from the queue.
$Self->_SendError(
Item => \%Param,
SendResult => {},
);

return {
Status => 'Failed',
Message => 'Sending has failed.',
};
}

# If DueTime is bigger than current time, skip, it is not time to run yet.
my $CurrentSysDTObject = $Kernel::OM->Create('Kernel::System::DateTime');
my $DueTime = $Param{DueTime};
Expand Down Expand Up @@ -694,7 +746,7 @@ sub Send {

return {
Status => 'Failed',
Message => 'Sending has Failed.'
Message => 'Sending has failed.'
};
}

Expand Down Expand Up @@ -829,7 +881,7 @@ sub _SendError {
my $SendResult = $Param{SendResult};

my $Item = $Param{Item};
my $ItemAttempts = $Item->{Attempts} + 1;
my $ItemAttempts = $Item->{Attempts};
my $ItemMaxAttempts = $Config->{ItemMaxAttempts};

$Item->{CommunicationLogObject}->ObjectLog(
Expand Down Expand Up @@ -910,10 +962,10 @@ sub _SendError {
my $NextAttempt = $CurrentSysDTObject->Clone();
$NextAttempt->Add( Minutes => $ItemAttempts * $MinutesToIncrement );

# Update mail-queue with attempt and smtp code and message.
# Update mail-queue with smtp code and message.
# Note: Attempts already have been incremented in Send().
my %UpdateData = (
Attempts => $ItemAttempts,
DueTime => $NextAttempt->ToString(),
DueTime => $NextAttempt->ToString(),
);

if ( $SendResult->{SMTPError} ) {
Expand Down

0 comments on commit 832e49b

Please sign in to comment.