Skip to main content

Supervisor

Struct Supervisor 

Source
pub struct Supervisor { /* private fields */ }
Expand description

服务生命周期 Supervisor:注册一批服务,按依赖 DAG 编排 prepare→run→cleanup。

Implementations§

Source§

impl Supervisor

Source

pub fn new() -> Supervisor

空 supervisor(自带一个空 ServiceBus)。

Source

pub fn add(self, service: Arc<dyn Service>) -> Supervisor

注册一个(必选)服务。其 prepare/run 失败会触发整体回滚/收束。

名为 add 是既定的 builder API(非 std::ops::Add)。

Source

pub fn add_optional(self, service: Arc<dyn Service>) -> Supervisor

注册一个可选服务:它的 prepare/run 失败只记日志,回滚其他服务、 触发整体收束。用于附加能力型服务(如 web 后台)。

约定:可选服务应是依赖图的叶子(不被其他服务 deps() 引用);依赖一个被跳过的 可选服务属配置错误。

Source

pub fn bus(&self) -> ServiceBus

共享 ServiceBus 的克隆(廉价)。供外部预置句柄或事后查询。

Source

pub async fn run(self, shutdown: CancellationToken) -> Result<(), Error>

编排整套生命周期。shutdown 是宿主的关停信号——supervisor 只监听、从不取消它 (收束时取消的是自己派生的子树 token),服务全部自然结束不影响宿主。

  1. 按依赖拓扑序逐层 prepare(任一失败 → 对已就绪者逆序 cleanup 后返回 Err);
  2. 全部 run spawn 进 JoinSet,各持子树的子 token; await 至 shutdown 触发或某 run 返回 Err;
  3. 取消子树,按逆拓扑序 cleanup

返回:若某 run 提前以 Err 收束则透传该错误,否则 Ok(())

Trait Implementations§

Source§

impl Default for Supervisor

Source§

fn default() -> Supervisor

Returns the “default value” for a type. Read more

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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