use std::collections::HashMap;
use std::io;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Duration;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::server_store::ServerStore;
use crate::tasks::TaskRecord;
pub(crate) const DEFAULT_RELAY_INTERVAL: Duration = Duration::from_millis(250);
pub(crate) const RELAY_BATCH_LIMIT: usize = 100;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct OutboxRecord {
pub outbox_id: String,
pub tenant: String,
pub task: TaskRecord,
#[serde(default)]
pub published_at: Option<DateTime<Utc>>,
pub created_at: DateTime<Utc>,
}
impl OutboxRecord {
pub(crate) fn new(task: TaskRecord, now: DateTime<Utc>) -> Self {
Self {
outbox_id: task.task_id.clone(),
tenant: task.tenant.clone(),
task,
published_at: None,
created_at: now,
}
}
}
pub(crate) fn dir(root: &Path) -> PathBuf {
root.join("outbox")
}
pub(crate) async fn persist(root: &Path, record: &OutboxRecord) -> io::Result<()> {
let dir = dir(root);
tokio::fs::create_dir_all(&dir).await?;
let bytes = serde_json::to_vec_pretty(record)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
let path = dir.join(format!("{}.json", record.outbox_id));
let tmp = dir.join(format!("{}.tmp", record.outbox_id));
tokio::fs::write(&tmp, bytes).await?;
tokio::fs::rename(&tmp, path).await
}
pub(crate) fn load(root: &Path) -> HashMap<String, OutboxRecord> {
let mut out = HashMap::new();
let Ok(entries) = std::fs::read_dir(dir(root)) else {
return out;
};
for entry in entries.flatten() {
let path = entry.path();
if path.extension().and_then(|e| e.to_str()) != Some("json") {
continue;
}
let parsed = std::fs::read_to_string(&path)
.ok()
.and_then(|raw| serde_json::from_str::<OutboxRecord>(&raw).ok());
match parsed {
Some(record) => {
out.insert(record.outbox_id.clone(), record);
}
None => {
tracing::warn!(path = %path.display(), "skipping unreadable outbox file")
}
}
}
out
}
pub(crate) fn spawn_relay(
store: Arc<dyn ServerStore>,
interval: Duration,
shutdown: tokio_util::sync::CancellationToken,
) {
tokio::spawn(async move {
let mut ticker = tokio::time::interval(interval);
ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
loop {
tokio::select! {
_ = ticker.tick() => {}
_ = shutdown.cancelled() => {
tracing::info!("outbox relay shutting down; pending rows publish on the next process");
break;
}
}
match store
.outbox_publish_pending(RELAY_BATCH_LIMIT, Utc::now())
.await
{
Ok(published) if !published.is_empty() => {
tracing::info!(count = published.len(), "outbox relay published tasks");
}
Ok(_) => {}
Err(error) => {
tracing::warn!(%error, "outbox relay poll failed; will retry");
}
}
}
});
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tasks::{NewTask, DEFAULT_MAX_ATTEMPTS, DEFAULT_POOL};
fn task(id: &str) -> TaskRecord {
TaskRecord::new(
NewTask {
task_id: id.to_string(),
tenant: "acme".to_string(),
kind: "send_email".to_string(),
payload: serde_json::json!({"to": "a@b.c"}),
pool: DEFAULT_POOL.to_string(),
recipient: None,
max_attempts: DEFAULT_MAX_ATTEMPTS,
idempotency_key: None,
effect: None,
run_id: None,
thread_id: None,
deadline: None,
worker_version: None,
parent: None,
},
Utc::now(),
)
}
#[tokio::test]
async fn outbox_file_round_trip_and_corrupt_tolerance() {
let root = std::env::temp_dir().join(format!("rusty-outbox-test-{}", uuid::Uuid::new_v4()));
let pending = OutboxRecord::new(task("t-1"), Utc::now());
persist(&root, &pending).await.unwrap();
let mut published = OutboxRecord::new(task("t-2"), Utc::now());
published.published_at = Some(Utc::now());
persist(&root, &published).await.unwrap();
std::fs::write(dir(&root).join("broken.json"), "{ not json").unwrap();
let loaded = load(&root);
assert_eq!(loaded.len(), 2);
assert!(loaded["t-1"].published_at.is_none());
assert!(loaded["t-2"].published_at.is_some());
assert_eq!(loaded["t-1"].task.task_id, "t-1");
let _ = std::fs::remove_dir_all(root);
}
#[test]
fn outbox_id_mirrors_the_task_id() {
let record = OutboxRecord::new(task("t-9"), Utc::now());
assert_eq!(record.outbox_id, "t-9");
assert_eq!(record.tenant, "acme");
assert!(record.published_at.is_none());
}
}