Expand description
Owner alerts: an email to the app’s owner when something needs attention, without flooding the inbox.
record stores an Alert (a dotted reason such as
payment.failed, an optional key such as the user’s id, a detail) in
the alerts table (ocre g alerts creates it) and emails it to
ALERT_EMAIL at once, unless:
- the same reason with the same key was emailed in the last
DEDUP_MINUTES(30): the alert is kept asdeduped, no email; HOURLY_CAP(20) alerts were emailed in the last hour: the alert waits asoverflowfor the digest, one email listing what was held back by reason (send_digest, from a schedule).
The decision is part of the INSERT (one statement), so concurrent
requests cannot both email the same alert. Alerts are owner-facing:
plain text, never translated, only ever sent to ALERT_EMAIL.
send_digest also forgets alerts older than KEEP_DAYS.
§Free plan
record: one D1 write, plus one email when it is sent (the mail
adapter’s own limits apply: Resend’s free plan sends 100 a day, which
the hourly cap keeps alerts well under). send_digest: one D1 write,
one read, one delete, and an email only when alerts were held back.
§Examples
use ocre::{Ctx, alerts::{self, Alert}};
async fn payment_failed(ctx: &Ctx, user_id: i64, provider_message: &str) {
let alert = Alert::new("payment.failed").key(user_id).detail(format!("Provider said: {provider_message}"));
alerts::record(ctx, alert).await; // never fails the request: a problem is logged
}
// src/schedules/alert_digest.rs (`ocre g schedule alert_digest "every 15 minutes"`):
pub async fn run(ctx: &Ctx) -> ocre::Result<()> {
alerts::send_digest(ctx).await.map(|_| ())
}Structs§
- Alert
- Something the owner should know about: a reason, what it concerns, and details.
Constants§
- ALERT_
EMAIL - Worker variable holding the owner’s address, where alerts go:
ALERT_EMAIL. - DEDUP_
MINUTES - Minutes during which the same reason and key are emailed once.
- HOURLY_
CAP - Alert emails per hour at most; later alerts wait for the digest.
- KEEP_
DAYS - Days alerts are kept;
send_digestdeletes older ones. - TABLE_
SQL - The
alertstable, created by the migration ofocre g alerts.
Functions§
- record
- Records
alertand emails it toALERT_EMAILunless it is a repeat or over the hourly cap. - send_
digest - Emails one digest of the alerts held back by the hourly cap, forgets alerts older than
KEEP_DAYS, and returns how many alerts the digest listed.