Watchfire
Agents
Watchfire (smeltery::watchfire) runs the app's background work on one runtime: agents (long-lived, supervised
tasks such as pollers, scrapers and bots), jobs (short tasks dispatched from the app, on a queue) and scheduled
tasks (started by the clock). app/agents/mod.rs registers everything; bootstrap/app.rs wires it with
.agents(app::agents::register) (the trait is smeltery::watchfire::AgentsExt). serve runs the agents next to
the HTTP server and work runs them alone; both stop them within SHUTDOWN_TIMEOUT.
use smeltery::watchfire::prelude::*;
pub fn register(w: &mut Watchfire) {
// Quick forms: closures, supervised like any agent.
w.every(30.secs(), "heartbeat", |ctx| async move {
ctx.log().info("tick");
Ok(())
});
w.run("scraper", |ctx| async move {
while ctx.sleep(5.secs()).await {
let page = ctx.http().get("https://example.com/").await?.text().await?;
ctx.counter("bytes").add(page.len() as i64);
}
Ok(())
})
.restart(Restart::OnFailure)
.backoff(1.secs()..=30.secs())
.group("scrapers");
w.on_event("post.created", "notify", |ctx, event| async move {
ctx.log().info(format!("new post: {}", event.payload));
Ok(())
});
w.group("scrapers").limit(2);
w.rate_limit("example.com", 2.per_second());
w.schedule()
.call("cleanup", |_ctx| async move { Ok(()) })
.every(5.mins())
.overlap(Overlap::Skip);
w.schedule().agent("scraper").cron("0 3 * * *");
}
A full agent is a struct (smeltery make:agent PricePoller); its fields survive restarts:
use smeltery::watchfire::prelude::*;
/// Polls prices.
#[derive(Default)]
pub struct PricePoller {
last: Option<String>,
}
impl Agent for PricePoller {
fn name(&self) -> String {
"price_poller".into()
}
fn config(&self) -> AgentConfig {
AgentConfig::default()
.restart(Restart::OnFailure)
.backoff(1.secs()..=60.secs())
.heartbeat_timeout(2.mins())
}
async fn run(&mut self, ctx: AgentCtx) -> Result<(), AgentError> {
let mut ticker = ctx.interval(30.secs());
while ticker.tick().await {
// `tick` heartbeats, and returns false once the agent is asked to stop
self.last = Some("42".into());
}
Ok(())
}
}
Register it with w.agent(price_poller::PricePoller::default()), or a pool of them with
w.pool(4, "fetcher", Fetcher::new) (named fetcher#0 … fetcher#3).