Skip to main content

Retry after

retry_after publishes a copy of the delivery after a delay: to its own topic for MqttTopic, to a topic the mount site names for MqttFilter.

The server moves nothing on a negative answer, so the copy is a publish made by the service. A subscription on a topic is reached again by publishing to that topic, and the descriptor reports it, so the mount site names nothing. A subscription on a filter reads many topics and reports no address. There the mount names the destination with .out_retry(policy).to(name), and a chain that names none does not compile. The copy comes back to the subscription when the named topic matches the filter. The copy leaves through the policy of out_retry, such as MqttPublish, and carries the retry count in the RETRY_COUNT_HEADER header.

On a device the copy waits in the store of the mount, and .retry_capacity::<N>() after the mount sets how many wait at once.

Example​

A topic subscription, asked for again in five seconds.

/// The valve is still moving: the job is asked for again in five seconds. A copy that carries its
/// retry count is the second attempt, and it is acknowledged.
#[subscriber(MqttTopic::new("jobs/run"))]
async fn run(job: &Job, ctx: &mut Context<'_>) -> HandlerOutcome {
let _ = job.id;
match ctx.headers().get_str(RETRY_COUNT_HEADER) {
Some(_) => HandlerOutcome::ack(),
None => HandlerOutcome::retry_after(Duration::from_secs(5)),
}
}

#[ruststream::app(runtime = embassy)]
fn app(broker: MqttBroker) -> EmbeddedApp<impl Mounts> {
// Room for two copies waiting at once.
EmbeddedApp::new().with_broker(broker, |b| b.include(run).retry_capacity::<2>())
}

A filter​

The mount names jobs/retry, which the filter jobs/+ matches.

/// A filter reads many topics and addresses none of them, so the mount site names where a copy
/// goes: `jobs/retry` matches the filter, and the copy comes back to the handler.
#[subscriber(MqttFilter::new("jobs/+"))]
async fn reconcile(job: &Job, ctx: &mut Context<'_>) -> HandlerOutcome {
let _ = job.id;
match ctx.headers().get_str(RETRY_COUNT_HEADER) {
Some(_) => HandlerOutcome::ack(),
None => HandlerOutcome::retry_after(Duration::from_secs(5)),
}
}

#[ruststream::app(runtime = embassy)]
fn filter_app(broker: MqttBroker) -> EmbeddedApp<impl Mounts> {
EmbeddedApp::new().with_broker(broker, |b| {
b.include(reconcile)
.out_retry(MqttPublish::default())
.to("jobs/retry")
.retry_capacity::<2>()
})
}