AppContext

Struct AppContext 

Source
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

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more