mirror of
https://github.com/idrainformatica/PecFlow.git
synced 2026-06-16 12:45:42 +02:00
vbox funzionanti
This commit is contained in:
@@ -21,7 +21,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from app.config import get_settings
|
||||
from app.database import AsyncSessionLocal
|
||||
from app.imap.reconnect import ExponentialBackoff
|
||||
from app.imap.sync import sync_new_messages
|
||||
from app.imap.sync import sync_new_messages, sync_sent_messages
|
||||
from app.models import Mailbox
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -144,17 +144,30 @@ class IMAPConnection:
|
||||
backoff.reset()
|
||||
await self._reset_error_state(mailbox, db)
|
||||
|
||||
# Sync iniziale: porta il DB aggiornato fino all'ultimo UID disponibile
|
||||
logger.info(f"[{mailbox.email_address}] Sync iniziale...")
|
||||
# Sync iniziale INBOX: porta il DB aggiornato fino all'ultimo UID disponibile
|
||||
logger.info(f"[{mailbox.email_address}] Sync iniziale INBOX...")
|
||||
try:
|
||||
n = await sync_new_messages(self._client, mailbox, db, self.redis)
|
||||
if n > 0:
|
||||
logger.info(
|
||||
f"[{mailbox.email_address}] Sync iniziale completata: {n} messaggi nuovi"
|
||||
f"[{mailbox.email_address}] Sync iniziale INBOX completata: {n} messaggi nuovi"
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
f"[{mailbox.email_address}] Errore sync iniziale: {e}", exc_info=True
|
||||
f"[{mailbox.email_address}] Errore sync iniziale INBOX: {e}", exc_info=True
|
||||
)
|
||||
|
||||
# Sync iniziale Sent: sincronizza la posta inviata storica
|
||||
logger.info(f"[{mailbox.email_address}] Sync iniziale Sent...")
|
||||
try:
|
||||
ns = await sync_sent_messages(self._client, mailbox, db, self.redis)
|
||||
if ns > 0:
|
||||
logger.info(
|
||||
f"[{mailbox.email_address}] Sync iniziale Sent completata: {ns} messaggi nuovi"
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
f"[{mailbox.email_address}] Errore sync iniziale Sent: {e}", exc_info=True
|
||||
)
|
||||
|
||||
# Avvia IDLE o polling
|
||||
@@ -259,18 +272,32 @@ class IMAPConnection:
|
||||
if line
|
||||
)
|
||||
|
||||
# Ricarica mailbox dal DB prima delle sync
|
||||
await db.refresh(mailbox)
|
||||
|
||||
if has_new:
|
||||
logger.debug(
|
||||
f"[{mailbox.email_address}] EXISTS ricevuto, sync..."
|
||||
f"[{mailbox.email_address}] EXISTS ricevuto, sync INBOX..."
|
||||
)
|
||||
# Ricarica mailbox dal DB per avere last_sync_uid aggiornato
|
||||
await db.refresh(mailbox)
|
||||
n = await sync_new_messages(client, mailbox, db, self.redis)
|
||||
if n > 0:
|
||||
logger.info(
|
||||
f"[{mailbox.email_address}] {n} nuovi messaggi sincronizzati"
|
||||
f"[{mailbox.email_address}] {n} nuovi messaggi INBOX sincronizzati"
|
||||
)
|
||||
|
||||
# Sync Sent ad ogni ciclo IDLE (heartbeat ~28 min o su EXISTS)
|
||||
try:
|
||||
ns = await sync_sent_messages(client, mailbox, db, self.redis)
|
||||
if ns > 0:
|
||||
logger.info(
|
||||
f"[{mailbox.email_address}] {ns} nuovi messaggi Sent sincronizzati"
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning(
|
||||
f"[{mailbox.email_address}] Errore sync Sent in IDLE loop: {e}"
|
||||
)
|
||||
# sync_sent_messages garantisce il ritorno in INBOX anche in caso di errore
|
||||
|
||||
except asyncio.CancelledError:
|
||||
try:
|
||||
await client.idle_done()
|
||||
@@ -311,12 +338,24 @@ class IMAPConnection:
|
||||
except Exception:
|
||||
raise ConnectionError("Connessione IMAP persa durante NOOP")
|
||||
|
||||
# Ricarica mailbox e controlla nuovi UID
|
||||
# Ricarica mailbox e controlla nuovi UID INBOX
|
||||
await db.refresh(mailbox)
|
||||
n = await sync_new_messages(client, mailbox, db, self.redis)
|
||||
if n > 0:
|
||||
logger.info(
|
||||
f"[{mailbox.email_address}] Polling: {n} nuovi messaggi"
|
||||
f"[{mailbox.email_address}] Polling INBOX: {n} nuovi messaggi"
|
||||
)
|
||||
|
||||
# Sync Sent ad ogni ciclo di polling
|
||||
try:
|
||||
ns = await sync_sent_messages(client, mailbox, db, self.redis)
|
||||
if ns > 0:
|
||||
logger.info(
|
||||
f"[{mailbox.email_address}] Polling Sent: {ns} nuovi messaggi"
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning(
|
||||
f"[{mailbox.email_address}] Errore sync Sent in polling loop: {e}"
|
||||
)
|
||||
|
||||
except asyncio.CancelledError:
|
||||
|
||||
Reference in New Issue
Block a user