CI / test (push) Failing after 3m23s
PUT /api/accounts/:id replaces an account's settings; the password is kept unless a new one is given. Accounts now have an updated_at, and the sync manager restarts a task whose account changed since it started — the task holds its own copy of the settings. Its status is reset to pending so the old error doesn't linger. Mail already synced stays when it's still the same mailbox (new password, renamed host); a genuinely different mailbox shows up as a UIDVALIDITY change on connect and re-syncs, as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
82 lines
3.1 KiB
Rust
82 lines
3.1 KiB
Rust
//! Spawns and tracks one sync task per account; stops the task of an
|
|
//! account that was removed, and restarts the task of one that was edited
|
|
//! (it holds its own copy of the settings). Woken by the API when accounts
|
|
//! change, and polls as a fallback.
|
|
|
|
use crate::crypto::CredentialCipher;
|
|
use crate::events::EventHub;
|
|
use crate::imap_sync::connection::AccountSyncer;
|
|
use crate::models::account::Account;
|
|
use crate::storage::BlobStore;
|
|
use chrono::{DateTime, Utc};
|
|
use sqlx::PgPool;
|
|
use std::collections::HashMap;
|
|
use std::sync::Arc;
|
|
use std::time::Duration;
|
|
use tokio::sync::Notify;
|
|
use uuid::Uuid;
|
|
|
|
const ACCOUNT_POLL_INTERVAL: Duration = Duration::from_secs(30);
|
|
|
|
pub async fn run(
|
|
pool: PgPool,
|
|
cipher: CredentialCipher,
|
|
blobs: BlobStore,
|
|
events: EventHub,
|
|
wake: Arc<Notify>,
|
|
) {
|
|
// Each task, with the account version (updated_at) it was started from.
|
|
let mut running: HashMap<Uuid, (tokio::task::JoinHandle<()>, DateTime<Utc>)> = HashMap::new();
|
|
|
|
loop {
|
|
running.retain(|_, (handle, _)| !handle.is_finished());
|
|
|
|
match sqlx::query_as::<_, Account>("SELECT * FROM accounts")
|
|
.fetch_all(&pool)
|
|
.await
|
|
{
|
|
Ok(accounts) => {
|
|
// A removed account's task would otherwise retry forever
|
|
// against rows that no longer exist.
|
|
let present: HashMap<Uuid, DateTime<Utc>> =
|
|
accounts.iter().map(|a| (a.id, a.updated_at)).collect();
|
|
running.retain(|id, (handle, version)| match present.get(id) {
|
|
None => {
|
|
tracing::info!(account_id = %id, "account removed; stopping sync task");
|
|
handle.abort();
|
|
false
|
|
}
|
|
Some(current) if current != version => {
|
|
tracing::info!(account_id = %id, "account edited; restarting sync task");
|
|
handle.abort();
|
|
false
|
|
}
|
|
Some(_) => true,
|
|
});
|
|
for account in accounts {
|
|
if running.contains_key(&account.id) {
|
|
continue;
|
|
}
|
|
tracing::info!(account_id = %account.id, name = %account.name, "starting sync task");
|
|
let syncer = AccountSyncer {
|
|
account,
|
|
pool: pool.clone(),
|
|
cipher: cipher.clone(),
|
|
blobs: blobs.clone(),
|
|
events: events.clone(),
|
|
};
|
|
let (id, version) = (syncer.account.id, syncer.account.updated_at);
|
|
let handle = tokio::spawn(syncer.run());
|
|
running.insert(id, (handle, version));
|
|
}
|
|
}
|
|
Err(e) => tracing::error!(error = %e, "failed to list accounts for sync manager"),
|
|
}
|
|
|
|
tokio::select! {
|
|
_ = wake.notified() => {}
|
|
_ = tokio::time::sleep(ACCOUNT_POLL_INTERVAL) => {}
|
|
}
|
|
}
|
|
}
|