OrderSystem

Struct OrderSystem 

Source
pub struct OrderSystem {
    pub order_client: OrderClient,
    pub user_client: UserClient,
    pub product_client: ProductClient,
    handles: Vec<JoinHandle<()>>,
}
Expand description

The main runtime orchestrator for the actor-based order management system.

OrderSystem is responsible for:

  • Lifecycle Management: Starting and stopping all actors in the system
  • Dependency Wiring: Connecting actors that depend on each other (e.g., OrderClient needs UserClient)
  • Resource Coordination: Managing shared resources like ID generators

§Architecture

The system consists of three main actors:

  • User Actor: Manages user entities (CRUD operations)
  • Product Actor: Manages product entities with stock tracking
  • Order Actor: Manages orders and coordinates with User and Product actors

§Example

ⓘ
let system = OrderSystem::new();

// Use the clients to interact with actors
let user_id = system.user_client.create_user(user_data).await?;
let product_id = system.product_client.create_product(product_data).await?;
let order_id = system.order_client.create_order(order_data).await?;

// Gracefully shut down when done
system.shutdown().await?;

Fields§

§order_client: OrderClient

Client for interacting with the Order actor

§user_client: UserClient

Client for interacting with the User actor

§product_client: ProductClient

Client for interacting with the Product actor

§handles: Vec<JoinHandle<()>>

Task handles for all running actors (used for graceful shutdown)

Implementations§

Source§

impl OrderSystem

Source

pub fn new() -> Self

Creates and initializes a new OrderSystem with all actors running.

This method:

  1. Creates ID generators for each entity type
  2. Spawns ResourceActors for User, Product, and Order
  3. Wires up dependencies (OrderClient depends on UserClient and ProductClient)
  4. Spawns each actor in its own Tokio task
§Returns

A fully initialized OrderSystem with all actors running and ready to accept requests.

Source

pub async fn shutdown(self) -> Result<(), String>

Gracefully shuts down the entire system.

This method:

  1. Drops all clients, which closes their communication channels
  2. Waits for all actor tasks to complete
  3. Returns an error if any actor task panicked
§Shutdown Process

When clients are dropped, the underlying channels are closed. Each ResourceActor detects the closed channel and exits its event loop gracefully.

§Returns
  • Ok(()) if all actors shut down cleanly
  • Err(String) if any actor task failed or panicked
§Example
ⓘ
let system = OrderSystem::new();
// ... use the system ...
system.shutdown().await?;

Trait Implementations§

Source§

impl Default for OrderSystem

Source§

fn default() -> Self

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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

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

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

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.
§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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