pub struct AppContext;
Expand description
Facade representing the global (singleton) application context.
The context starts in “alive” state, and can be terminated at any time. It’s also possible to auto-terminate the context when an OS shutdown signal is intercepted. The context can be effectively terminated only once: repeated termination produces no additional effect.
Any number of asynchronous tasks may use the AppContext
facade as a
central reference for whether the application is still alive (not in the
process of shutting down). A task may wait for the
context to be terminated.
§Example
use strut_core::AppContext;
use tokio::task;
#[tokio::main]
async fn main() {
// Spawn a task that waits for context termination
let cleanup_task = task::spawn(async move {
// Wait...
AppContext::terminated().await;
// Perform some cleanup...
});
// Terminate manually
AppContext::terminate();
// Wait for cleanup to complete
cleanup_task.await.unwrap();
}
Implementations§
Source§impl AppContext
impl AppContext
Sourcepub async fn terminated()
pub async fn terminated()
Blocks until the global application context is terminated.
Any number of tasks may await on this method. If a task starts waiting on this method after the context has been terminated, the returned future completes immediately.
Sourcepub fn terminate()
pub fn terminate()
Terminates the global application context. If the context is already
terminated, no additional effect is produced beyond a tracing
event.
When the context is terminated, all tasks waiting on it will unblock.
Sourcepub async fn auto_terminate()
pub async fn auto_terminate()
Schedules listening for the OS shutdown signals, which replaces the default shutdown behavior of this entire OS process. After this method returns, the first intercepted OS shutdown signal will terminate this context.
Take note of the consequences of replacing the default process shutdown behavior.
Repeated calls to this method produce no additional effect.
This method must be awaited to ensure that signal listening has already started by the time the returned future completes.
Sourcepub fn is_terminated() -> bool
pub fn is_terminated() -> bool
Reports whether the global application context has been terminated as of this moment.
This method is not suitable for waiting for it to be terminated. For
such purposes, use AppContext::terminated
.
Sourcepub fn is_alive() -> bool
pub fn is_alive() -> bool
Reports whether the global application context has not yet been terminated as of this moment.
This method is not suitable for waiting for it to be terminated. For
such purposes, use AppContext::terminated
.