跳到主要内容
本页尚未翻译

下面显示的是英文原文。

App

The #[ruststream::app] attribute turns a builder function into the service's main: a binary that runs the service or prints its AsyncAPI document.

The attribute keeps the function and adds a main that hands it to run_main. The function is synchronous, takes no arguments and returns impl App or a RustStream; any other signature is a compile error. The function is the application builder, unchanged. The binary reads its first argument as a command, the same commands the CLI drives through cargo run. run, or no argument, runs the service until SIGINT or SIGTERM. asyncapi gen prints the AsyncAPI document; -o <file> writes it to a file and --yaml switches the format from JSON. asyncapi gen needs the crate's asyncapi feature. help, --help or -h prints the commands the binary understands and exits. With the logging feature, run installs the console logger first, unless the service installed a subscriber of its own. An error is printed to stderr and the process exits with a failure code. The attribute takes the runtime arguments of #[tokio::main]: flavor = "current_thread" and worker_threads = n. The default is a multi-threaded runtime with one worker per core. #[ruststream::app(runtime = embassy)] builds the embedded app for a device instead: the function returns an EmbeddedApp, and the attribute emits an embassy task, app::task, that the board's main spawns. #[ruststream::app(runtime = compio, cores = n)] builds the compio app: the builder has the shape of the tokio one, and the attribute emits a main that runs it on n cores, or on every available core without cores. A hand-written main calls run_main_on to choose the runtime as an AppRuntime value. A test runs the same builder function on TestApp, as the example does.

Example​

#[subscriber("orders")]
async fn accept(order: &Order) -> HandlerOutcome {
tracing::info!(order.id, "accepted");
HandlerOutcome::ack()
}

#[ruststream::app]
fn app() -> impl App {
RustStream::new(AppInfo::new("orders", "0.1.0"))
.with_broker(MemoryBroker::new(), |b| b.include(accept))
}