[local-sync] use async await in processNewMessage and fix logging

This commit is contained in:
Evan Morikawa 2016-12-20 17:09:17 -08:00
parent ab5a66d4e7
commit f0436e17d4
2 changed files with 28 additions and 33 deletions

View file

@ -264,11 +264,12 @@ class FetchMessagesInFolder {
// TODO investigate batching processing new messages
// We could measure load of actual sync vs load of just message processing
// to determine how meaningful it is
processNewMessage(messageValues, imapMessage)
await processNewMessage(messageValues, imapMessage)
// this._logger.info({
// message: messageValues,
// }, `FetchMessagesInFolder: Queued new message for processing`)
}
console.log(`🔃 ✉️ "${messageValues.subject}" - ${messageValues.date}`)
} catch (err) {
this._logger.error(err, {
imapMessage,

View file

@ -3,40 +3,34 @@ const extractFiles = require('./extract-files');
const extractContacts = require('./extract-contacts');
const LocalDatabaseConnector = require('../shared/local-database-connector');
const Queue = require('promise-queue');
const queue = new Queue(1, Infinity);
async function processNewMessage(message, imapMessage) {
const {accountId} = message;
const logger = global.Logger.forAccount({id: accountId}).child({message})
const db = await LocalDatabaseConnector.forAccount(accountId);
const {Message} = db
function processNewMessage(message, imapMessage) {
queue.add(async () => {
const {accountId} = message;
const logger = global.Logger.forAccount({id: accountId}).child({message})
const db = await LocalDatabaseConnector.forAccount(accountId);
const {Message} = db
try {
const existingMessage = await Message.findById(message.id)
if (existingMessage) {
// This is an extremely rare case when 2 or more /new/ messages with
// the exact same headers were queued for creation (same subject,
// participants, timestamp, and message-id header). In this case, we
// will ignore it and report the error
logger.warn({message}, 'MessageProcessor: Encountered 2 new messages with the same id')
return
}
const thread = await detectThread({db, message});
message.threadId = thread.id;
await Message.create(message);
await extractFiles({db, message, imapMessage});
await extractContacts({db, message});
console.log(`🔃 ✉️ "${message.subject}"`)
// logger.info({
// message_id: message.id,
// uid: message.folderImapUID,
// }, `MessageProcessor: Created and processed message`);
} catch (err) {
logger.error(err, `MessageProcessor: Failed`);
try {
const existingMessage = await Message.findById(message.id)
if (existingMessage) {
// This is an extremely rare case when 2 or more /new/ messages with
// the exact same headers were queued for creation (same subject,
// participants, timestamp, and message-id header). In this case, we
// will ignore it and report the error
logger.warn({message}, 'MessageProcessor: Encountered 2 new messages with the same id')
return
}
});
const thread = await detectThread({db, message});
message.threadId = thread.id;
await Message.create(message);
await extractFiles({db, message, imapMessage});
await extractContacts({db, message});
// logger.info({
// message_id: message.id,
// uid: message.folderImapUID,
// }, `MessageProcessor: Created and processed message`);
} catch (err) {
logger.error(err, `MessageProcessor: Failed`);
}
}
module.exports = {processNewMessage}