Files
sorsfonalak/services/worker/src/main.rs
T

182 lines
6.8 KiB
Rust
Raw Normal View History

//! 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)
//! * issuing the invoices of the payments in Billingo
//!
//! `WORKER_TASKS` (e.g. `email,cleanup` or `analyses`) picks the jobs of one
//! process, so they can run in separate containers; all of them by default.
//!
//! 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 invoices;
2026-11-17 18:43:39 +00:00
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<bool>) {
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<PgListener> {
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<PgPool>) -> 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_console_logging();
return compare::main(std::env::args().skip(2).collect()).await;
}
_ => {}
}
let _log = service_kit::init_logging("worker");
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();
let tasks = config.tasks;
tracing::info!(
email = tasks.email,
cleanup = tasks.cleanup,
analyses = tasks.analyses,
invoices = tasks.invoices,
"worker tasks"
);
match &config.smtp_url {
_ if !tasks.email => {}
Some(url) => {
let delivery = email::Delivery::new(pool.clone(), email::Mailer::smtp(url)?, &config);
2026-11-17 18:43:39 +00:00
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()));
}
2026-11-17 18:43:39 +00:00
None => tracing::warn!("neither SMTP_URL nor SES credentials are set: e-mails stay in the outbox"),
}
match &config.openrouter_api_key {
_ if !tasks.analyses => {}
Some(key) => {
let llm =
analysis::openrouter::OpenRouter::new(&config.openrouter_base_url, key, config.public_base_url.clone());
let lanes = analysis::Lanes { quick: config.quick_concurrency, slow: config.analysis_concurrency };
if lanes.quick + lanes.slow == 0 {
tracing::warn!("QUICK_CONCURRENCY and ANALYSIS_CONCURRENCY are both 0: this worker writes no analyses");
} else {
let analyzer = analysis::Analyzer::new(pool.clone(), llm, config.max_analysis_attempts, lanes);
tracing::info!(quick = lanes.quick, slow = lanes.slow, "analyses enabled");
jobs.spawn(analysis::run(
Arc::new(analyzer),
config.poll_interval,
config.shutdown_drain,
stop.clone(),
));
}
}
None => tracing::warn!("OPENROUTER_API_KEY is not set: analyses stay pending"),
}
if tasks.cleanup {
jobs.spawn(cleanup::run(pool.clone(), stop.clone()));
}
match &config.billingo_api_key {
_ if !tasks.invoices => {}
Some(key) => {
let billingo = invoices::Billingo::new(&config.billingo_api_url, key, config.billingo_block_id);
tracing::info!("invoicing enabled (Billingo)");
jobs.spawn(invoices::run(
pool.clone(),
billingo,
config.max_invoice_attempts,
config.poll_interval,
stop.clone(),
));
}
None => tracing::warn!("BILLINGO_API_KEY is not set: invoices stay pending"),
}
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);
// Nothing new is taken; running work finishes (analyses hand back what is
// not done within the drain time, a few seconds before this gives up).
tracing::info!(drain_secs = config.shutdown_drain.as_secs(), "stopping: letting running work finish");
let finished = tokio::time::timeout(config.shutdown_drain + Duration::from_secs(5), async {
while jobs.join_next().await.is_some() {}
});
if finished.await.is_err() {
tracing::warn!("jobs did not stop in time");
} else {
tracing::info!("all work finished");
}
Ok(())
}
#[cfg(test)]
mod tests;