mirror of
https://github.com/Foundry376/Mailspring.git
synced 2025-01-11 18:47:51 +08:00
[local-sync] use async await in processNewMessage and fix logging
This commit is contained in:
parent
ab5a66d4e7
commit
f0436e17d4
2 changed files with 28 additions and 33 deletions
|
@ -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,
|
||||
|
|
|
@ -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}
|
||||
|
|
Loading…
Reference in a new issue