//! Sorsfonalak worker: background jobs that read and write the shared //! PostgreSQL database. //! //! * e-mail delivery from `email_outbox` over SMTP (Mailpit in development) //! * hourly cleanup of expired sessions, tokens, invites and old e-mails //! * writing the requested text analyses with an LLM (OpenRouter) //! //! Configuration: see [`config::Config`]; plus `BIND_ADDR` (default //! `0.0.0.0:8082`, only `/healthz`), `LOG_FORMAT` and `RUST_LOG`. //! `worker healthcheck` performs a local health check (for Docker); //! `worker compare-models ...` writes one analysis with several models (see [`compare`]). mod analysis; mod cleanup; mod compare; mod config; mod email; mod ses; mod templates { pub use email_templates::*; } use std::sync::Arc; use std::time::Duration; use anyhow::Context; use axum::Router; use axum::extract::State; use axum::http::StatusCode; use axum::routing::get; use sqlx::PgPool; use sqlx::postgres::PgListener; use tokio::sync::watch; use crate::config::Config; const DEFAULT_BIND_ADDR: &str = "0.0.0.0:8082"; /// Resolves once shutdown has been requested. pub async fn stopped(mut stop: watch::Receiver) { let _ = stop.wait_for(|s| *s).await; } /// Subscribes to a NOTIFY channel; `None` (polling only) if that fails. pub async fn listen(db: &PgPool, channel: &str) -> Option { let result = async { let mut listener = PgListener::connect_with(db).await?; listener.listen(channel).await?; Ok::<_, sqlx::Error>(listener) } .await; result.map_err(|e| tracing::warn!(channel, error = %e, "LISTEN failed; polling only")).ok() } /// Resolves on the next notification; never without a listener. pub async fn notified(listener: Option<&mut PgListener>) { match listener { // Reconnects by itself; an error means the connection is down for now. Some(l) => { if let Err(e) = l.recv().await { tracing::warn!(error = %e, "LISTEN connection lost"); tokio::time::sleep(Duration::from_secs(5)).await; } } None => std::future::pending().await, } } async fn healthz(State(db): State) -> StatusCode { match sqlx::query("SELECT 1").execute(&db).await { Ok(_) => StatusCode::OK, Err(_) => StatusCode::SERVICE_UNAVAILABLE, } } #[tokio::main] async fn main() -> anyhow::Result<()> { let addr = service_kit::bind_addr(DEFAULT_BIND_ADDR)?; match std::env::args().nth(1).as_deref() { Some("healthcheck") => return service_kit::healthcheck(addr), Some("compare-models") => { service_kit::init_logging(); return compare::main(std::env::args().skip(2).collect()).await; } _ => {} } service_kit::init_logging(); let config = Config::from_env()?; let pool = db::connect(&config.database_url).await.context("connecting to the database")?; db::migrate(&pool).await.context("running database migrations")?; let (stop_tx, stop) = watch::channel(false); let mut jobs = tokio::task::JoinSet::new(); match &config.smtp_url { Some(url) => { let delivery = email::Delivery::new(pool.clone(), email::Mailer::smtp(url)?, &config); let via = config.smtp_about.clone().unwrap_or_else(|| "SMTP_URL".into()); tracing::info!(host = %email::smtp_host(url), via = %via, from = %config.mail_from, "e-mail delivery enabled"); jobs.spawn(email::run(delivery, config.poll_interval, stop.clone())); } None => tracing::warn!("neither SMTP_URL nor SES credentials are set: e-mails stay in the outbox"), } match &config.openrouter_api_key { Some(key) => { let llm = analysis::openrouter::OpenRouter::new(&config.openrouter_base_url, key, config.public_base_url.clone()); let analyzer = analysis::Analyzer::new(pool.clone(), llm, config.max_analysis_attempts, config.analysis_concurrency); tracing::info!(concurrency = analyzer.concurrency, "analyses enabled"); jobs.spawn(analysis::run(Arc::new(analyzer), config.poll_interval, stop.clone())); } None => tracing::warn!("OPENROUTER_API_KEY is not set: analyses stay pending"), } jobs.spawn(cleanup::run(pool.clone(), stop.clone())); let app = Router::new().route("/healthz", get(healthz)).with_state(pool); let listener = tokio::net::TcpListener::bind(addr).await.with_context(|| format!("binding {addr}"))?; tracing::info!(%addr, "worker running"); let server = axum::serve(listener, app).with_graceful_shutdown(stopped(stop.clone())); tokio::select! { r = server => r?, () = service_kit::shutdown_signal() => {} } let _ = stop_tx.send(true); // Let jobs finish the batch they are working on. let finished = tokio::time::timeout(Duration::from_secs(30), async { while jobs.join_next().await.is_some() {} }); if finished.await.is_err() { tracing::warn!("jobs did not stop in time"); } Ok(()) } #[cfg(test)] mod tests;