Ниже показан английский оригинал.
create_if_missing
create_if_missing() creates the queue at startup when it is missing; a .fifo name creates a FIFO queue, and a dead-letter queue is created too.
The subscription calls CreateQueue when it opens and the account does not have the queue yet.
Without the step, a missing queue stops the service at startup.
A name that ends in .fifo creates a FIFO queue with content-based deduplication.
A dead-letter queue the registration declares is created the same way.
Every other attribute of a new queue keeps the SQS default, a 30 second visibility timeout among them.
The step needs the sqs:CreateQueue permission.
It suits a local stack and a test; production queues are usually managed as infrastructure.
In process every queue a service names exists already, so the step shows its effect in a live test.
The step is on SqsQueue, and on the mount chain through SqsSubscription::create_if_missing, as in the example.
Example
A drafts queue the service opens itself on a fresh stack.
- Macros
- Manual
/// Reads drafts from a queue the service opens itself on a fresh stack.
#[subscriber(SqsQueue)]
async fn review(draft: &Draft) -> HandlerOutcome {
tracing::info!(draft.id, "draft reviewed");
HandlerOutcome::ack()
}
fn service(broker: SqsBroker, queue: String) -> impl App<State = ()> {
RustStream::new(AppInfo::new("drafts", "0.1.0")).with_broker(broker, |b| {
b.include(review.name(queue).create_if_missing());
})
}
/// Reads drafts from a queue the service opens itself on a fresh stack.
struct Review;
impl Handle<Draft> for Review {
fn handle(
&self,
draft: &Draft,
_outs: &(),
_ctx: &mut Context<'_>,
) -> impl Future<Output = Result<(), HandlerOutcome>> {
tracing::info!(draft.id, "draft reviewed");
ready(Ok(()))
}
}
fn service(broker: SqsBroker, queue: String) -> impl App<State = ()> {
RustStream::new(AppInfo::new("drafts", "0.1.0")).with_broker(broker, |b| {
b.include(
subscriber(Unnamed::<SqsQueue>::new(), Review)
.build()
.name(queue)
.create_if_missing(),
);
})
}