ваше сообщение коммита
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
console.log('[EmailBot] emailBot.js loaded');
|
||||
const db = require('../db');
|
||||
const nodemailer = require('nodemailer');
|
||||
const Imap = require('imap');
|
||||
@@ -9,6 +10,10 @@ const identityService = require('./identity-service');
|
||||
const aiAssistant = require('./ai-assistant');
|
||||
|
||||
class EmailBotService {
|
||||
constructor() {
|
||||
console.log('[EmailBot] constructor called');
|
||||
}
|
||||
|
||||
async getSettingsFromDb() {
|
||||
const { rows } = await db.getQuery()('SELECT * FROM email_settings ORDER BY id LIMIT 1');
|
||||
if (!rows.length) throw new Error('Email settings not found in DB');
|
||||
@@ -46,6 +51,7 @@ class EmailBotService {
|
||||
idleInterval: 300000,
|
||||
forceNoop: true,
|
||||
},
|
||||
connTimeout: 30000, // 30 секунд
|
||||
};
|
||||
}
|
||||
|
||||
@@ -228,55 +234,63 @@ class EmailBotService {
|
||||
}
|
||||
|
||||
async start() {
|
||||
logger.info('[EmailBot] start() called');
|
||||
const imapConfig = await this.getImapConfig();
|
||||
// Логируем IMAP-конфиг (без пароля)
|
||||
const safeConfig = { ...imapConfig };
|
||||
if (safeConfig.password) safeConfig.password = '***';
|
||||
logger.info('[EmailBot] IMAP config:', safeConfig);
|
||||
let attempt = 0;
|
||||
const maxAttempts = 3;
|
||||
this.isChecking = false;
|
||||
const tryConnect = () => {
|
||||
attempt++;
|
||||
logger.info(`[EmailBot] IMAP connect attempt ${attempt}`);
|
||||
this.imap = new Imap(imapConfig);
|
||||
this.imap.once('ready', () => {
|
||||
logger.info('[EmailBot] IMAP connection ready');
|
||||
this.imap.openBox('INBOX', false, (err, box) => {
|
||||
if (err) {
|
||||
logger.error(`[EmailBot] Error opening INBOX: ${err.message}`);
|
||||
this.imap.end();
|
||||
return;
|
||||
}
|
||||
logger.info('[EmailBot] INBOX opened successfully');
|
||||
try {
|
||||
console.log('[EmailBot] start() called');
|
||||
logger.info('[EmailBot] start() called');
|
||||
const imapConfig = await this.getImapConfig();
|
||||
// Логируем IMAP-конфиг (без пароля)
|
||||
const safeConfig = { ...imapConfig };
|
||||
if (safeConfig.password) safeConfig.password = '***';
|
||||
logger.info('[EmailBot] IMAP config:', safeConfig);
|
||||
let attempt = 0;
|
||||
const maxAttempts = 3;
|
||||
this.isChecking = false;
|
||||
const tryConnect = () => {
|
||||
attempt++;
|
||||
logger.info(`[EmailBot] IMAP connect attempt ${attempt}`);
|
||||
this.imap = new Imap(imapConfig);
|
||||
this.imap.once('ready', () => {
|
||||
logger.info('[EmailBot] IMAP connection ready');
|
||||
this.imap.openBox('INBOX', false, (err, box) => {
|
||||
if (err) {
|
||||
logger.error(`[EmailBot] Error opening INBOX: ${err.message}`);
|
||||
this.imap.end();
|
||||
return;
|
||||
}
|
||||
logger.info('[EmailBot] INBOX opened successfully');
|
||||
});
|
||||
// После успешного подключения — обычная логика
|
||||
this.checkEmails();
|
||||
logger.info('[EmailBot] Email bot started and IMAP connection initiated');
|
||||
// Периодическая проверка почты
|
||||
setInterval(async () => {
|
||||
if (this.isChecking) return;
|
||||
this.isChecking = true;
|
||||
try {
|
||||
await this.checkEmails();
|
||||
} catch (e) {
|
||||
logger.error('[EmailBot] Error in periodic checkEmails:', e);
|
||||
}
|
||||
this.isChecking = false;
|
||||
}, 60000); // 60 секунд
|
||||
});
|
||||
// После успешного подключения — обычная логика
|
||||
this.checkEmails();
|
||||
logger.info('[EmailBot] Email bot started and IMAP connection initiated');
|
||||
// Периодическая проверка почты
|
||||
setInterval(async () => {
|
||||
if (this.isChecking) return;
|
||||
this.isChecking = true;
|
||||
try {
|
||||
await this.checkEmails();
|
||||
} catch (e) {
|
||||
logger.error('[EmailBot] Error in periodic checkEmails:', e);
|
||||
this.imap.once('error', (err) => {
|
||||
logger.error(`[EmailBot] IMAP connection error: ${err.message}`);
|
||||
if (err.message && err.message.toLowerCase().includes('timed out') && attempt < maxAttempts) {
|
||||
logger.warn(`[EmailBot] IMAP reconnecting in 10 seconds (attempt ${attempt + 1})...`);
|
||||
setTimeout(tryConnect, 10000);
|
||||
}
|
||||
this.isChecking = false;
|
||||
}, 60000); // 60 секунд
|
||||
});
|
||||
this.imap.once('error', (err) => {
|
||||
logger.error(`[EmailBot] IMAP connection error: ${err.message}`);
|
||||
if (err.message && err.message.toLowerCase().includes('timed out') && attempt < maxAttempts) {
|
||||
logger.warn(`[EmailBot] IMAP reconnecting in 10 seconds (attempt ${attempt + 1})...`);
|
||||
setTimeout(tryConnect, 10000);
|
||||
}
|
||||
});
|
||||
this.imap.connect();
|
||||
};
|
||||
tryConnect();
|
||||
});
|
||||
this.imap.connect();
|
||||
};
|
||||
tryConnect();
|
||||
} catch (err) {
|
||||
console.error('[EmailBot] Ошибка при старте:', err);
|
||||
logger.error('[EmailBot] Ошибка при старте:', err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log('[EmailBot] module.exports = EmailBotService');
|
||||
module.exports = EmailBotService;
|
||||
|
||||
Reference in New Issue
Block a user