Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch mail exceptions and still remove sent emails from queue #574

Merged
merged 1 commit into from
Jul 4, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions lib/BackgroundJob/EmailNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,35 @@ protected function runStep($limit, $sendTime) {
// Send Email
$default_lang = $this->config->getSystemValue('default_language', 'en');
$defaultTimeZone = date_default_timezone_get();

$sentMailForUsers = [];

foreach ($affectedUsers as $user) {
$uid = $user['uid'];
if (empty($user['email'])) {
// The user did not setup an email address
// So we will not send an email but still discard the queue entries
$this->logger->debug("Couldn't send notification email to user '$uid' (email address isn't set for that user)", ['app' => 'activity']);
$sentMailForUsers[] = $uid;
continue;
}

$language = (!empty($userLanguages[$uid])) ? $userLanguages[$uid] : $default_lang;
$timezone = (!empty($userTimezones[$uid])) ? $userTimezones[$uid] : $defaultTimeZone;
$this->mqHandler->sendEmailToUser($uid, $user['email'], $language, $timezone, $sendTime);

try {
$this->mqHandler->sendEmailToUser($uid, $user['email'], $language, $timezone, $sendTime);
$sentMailForUsers[] = $uid;
} catch (\Exception $e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't that too tight an exception ? we likely only want to catch and skip validation exceptions.

If the mail server (SMTP) isn't available, we should directly abort instead of continuing the queue.

This was a bit the idea in my PR, except that instead of inventing a new exception in the mailer I just did the validation directly in activity. Probably not the best idea. Should we invent a new exception in our mailer code then ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well we have to catch everything here so that we can later delete the items that were sent. I was seeing an error where the SMTP server was going away which was uncaught. Maybe we can catch here, exit the loop, run a cleanup method then rethrow?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good

// Run cleanup before we die
$this->mqHandler->deleteSentItems($sentMailForUsers, $sendTime);
// Throw the exception again - which gets logged by core and the job is handled appropriately
throw $e;
}
}

// Delete all entries we dealt with
$this->mqHandler->deleteSentItems($affectedUIDs, $sendTime);
$this->mqHandler->deleteSentItems($sentMailForUsers, $sendTime);

return sizeof($affectedUsers);
}
Expand Down