This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(metrics): add code and config for email service notification queue
Fixes #2633. Once the email service starts handling bounce and complaint events we will need a way for it to tell the auth server to emit metrics. This change adds a handler + config for a new SQS queue to that end. It duplicates some of the code from other handlers but that's intentional, we plan to remove those queues eventually.
- Loading branch information
Showing
4 changed files
with
586 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
'use strict' | ||
|
||
const P = require('../promise') | ||
const utils = require('./utils/helpers') | ||
|
||
// Account deletion threshold for new unverified accounts that receive | ||
// a bounce or complaint notification. Unverified accounts younger than | ||
// 6 hours old will be deleted if a bounce or complaint occurs. | ||
const SIX_HOURS = 1000 * 60 * 60 * 6 | ||
|
||
module.exports = (log, error) => { | ||
return (queue, db) => { | ||
queue.start() | ||
|
||
queue.on('data', async message => { | ||
try { | ||
utils.logErrorIfHeadersAreWeirdOrMissing(log, message, 'notification') | ||
|
||
let addresses = [], eventType = 'bounced', isDeletionCandidate = false | ||
if (message.bounce) { | ||
addresses = mapBounceComplaintRecipients(message.bounce.bouncedRecipients) | ||
isDeletionCandidate = true | ||
} else if (message.complaint) { | ||
addresses = mapBounceComplaintRecipients(message.complaint.complainedRecipients) | ||
isDeletionCandidate = true | ||
} else if (message.delivery) { | ||
addresses = message.delivery.recipients | ||
eventType = 'delivered' | ||
} | ||
|
||
await P.all(addresses.map(async address => { | ||
const domain = utils.getAnonymizedEmailDomain(address) | ||
|
||
utils.logFlowEventFromMessage(log, message, eventType) | ||
utils.logEmailEventFromMessage(log, message, eventType, domain) | ||
|
||
if (isDeletionCandidate) { | ||
const emailRecord = await db.accountRecord(address) | ||
|
||
if (! emailRecord.emailVerified && emailRecord.createdAt >= Date.now() - SIX_HOURS) { | ||
// A bounce or complaint on a new unverified account is grounds for deletion | ||
await db.deleteAccount(emailRecord) | ||
|
||
log.info({ op: 'accountDeleted', ...emailRecord }) | ||
} | ||
} | ||
})) | ||
} catch (err) { | ||
log.error({ op: 'email.notification.error', err }) | ||
} | ||
|
||
message.del() | ||
}) | ||
} | ||
} | ||
|
||
function mapBounceComplaintRecipients (recipients) { | ||
return recipients.map(recipient => recipient.emailAddress) | ||
} |
Oops, something went wrong.