Function service
If you need to run some long-running service in your app and Roadster doesn't provide built-in support for the
specific service you need, you can implement
Service
directly. This gives you the
most control over the service, especially if you implement
ServiceBuilder
as well.
If you don't want to implement Service
yourself, you can simply run the service in an async
function and pass
that function to a
FunctionService
.
fn build_app() -> RoadsterApp<AppContext> {
RoadsterApp::builder()
// Use the default `AppContext` for this example
.state_provider(|context| Ok(context))
// Register the example function-based service
.add_service(
FunctionService::builder()
.name("example-service")
.function(example_service)
.build(),
)
.build()
}
async fn example_service(
_state: AppContext,
_cancel_token: CancellationToken,
) -> RoadsterResult<()> {
info!("Running example function-based service");
Ok(())
}