Skip to main content

once

Function once 

Source
pub async fn once<T, F, Fut>(
    db: &Db,
    source: &str,
    event_id: &str,
    payload: &[u8],
    effect: F,
) -> Result<Delivery<T>>
where F: FnOnce() -> Fut, Fut: Future<Output = Result<T>>,
Expand description

Runs effect for the event event_id of source unless it already ran.

The delivery is claimed with one insert into webhook_events (unique on source, event_id), with the raw payload kept for the log. When the effect succeeds the event is marked processed, and later deliveries of the same id get Delivery::Duplicate: answer them 200 so the provider stops retrying. When it fails, the event is marked failed with the error and the error is returned, so the handler answers 500 and the provider’s retry runs the effect again. A delivery still processing after STALE_AFTER seconds is taken over.

Two to three D1 writes per delivery. Recording and effect are separate statements: an effect made of D1 writes should be idempotent itself (INSERT ... ON CONFLICT DO NOTHING, UPDATE ... WHERE status = 'pending') to stay correct if the invocation dies between them.

§Errors

The effect’s error, or a D1 error (the table is missing: run ocre g webhook and ocre migrate).