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
|
// TODO investigate batching processing new messages
|
||||||
// We could measure load of actual sync vs load of just message processing
|
// We could measure load of actual sync vs load of just message processing
|
||||||
// to determine how meaningful it is
|
// to determine how meaningful it is
|
||||||
processNewMessage(messageValues, imapMessage)
|
await processNewMessage(messageValues, imapMessage)
|
||||||
// this._logger.info({
|
// this._logger.info({
|
||||||
// message: messageValues,
|
// message: messageValues,
|
||||||
// }, `FetchMessagesInFolder: Queued new message for processing`)
|
// }, `FetchMessagesInFolder: Queued new message for processing`)
|
||||||
}
|
}
|
||||||
|
console.log(`🔃 ✉️ "${messageValues.subject}" - ${messageValues.date}`)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this._logger.error(err, {
|
this._logger.error(err, {
|
||||||
imapMessage,
|
imapMessage,
|
||||||
|
|
|
@ -3,40 +3,34 @@ const extractFiles = require('./extract-files');
|
||||||
const extractContacts = require('./extract-contacts');
|
const extractContacts = require('./extract-contacts');
|
||||||
const LocalDatabaseConnector = require('../shared/local-database-connector');
|
const LocalDatabaseConnector = require('../shared/local-database-connector');
|
||||||
|
|
||||||
const Queue = require('promise-queue');
|
async function processNewMessage(message, imapMessage) {
|
||||||
const queue = new Queue(1, Infinity);
|
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) {
|
try {
|
||||||
queue.add(async () => {
|
const existingMessage = await Message.findById(message.id)
|
||||||
const {accountId} = message;
|
if (existingMessage) {
|
||||||
const logger = global.Logger.forAccount({id: accountId}).child({message})
|
// This is an extremely rare case when 2 or more /new/ messages with
|
||||||
const db = await LocalDatabaseConnector.forAccount(accountId);
|
// the exact same headers were queued for creation (same subject,
|
||||||
const {Message} = db
|
// participants, timestamp, and message-id header). In this case, we
|
||||||
|
// will ignore it and report the error
|
||||||
try {
|
logger.warn({message}, 'MessageProcessor: Encountered 2 new messages with the same id')
|
||||||
const existingMessage = await Message.findById(message.id)
|
return
|
||||||
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`);
|
|
||||||
}
|
}
|
||||||
});
|
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}
|
module.exports = {processNewMessage}
|
||||||
|
|
Loading…
Reference in a new issue