Struct AgentRuntime

Source
pub struct AgentRuntime(/* private fields */);
Expand description

Represents the initialized and active Acton agent system runtime.

This struct is obtained after successfully launching the system via ActonApp::launch(). It holds the internal state of the running system, including a reference to the central message broker and a registry of top-level agents.

AgentRuntime provides the primary methods for interacting with the system as a whole, such as creating new top-level agents (new_agent, spawn_agent, etc.) and initiating a graceful shutdown of all agents (shutdown_all).

It is cloneable, allowing different parts of an application to hold references to the runtime environment.

Implementations§

Source§

impl AgentRuntime

Source

pub async fn new_agent_with_name<State>( &mut self, name: String, ) -> ManagedAgent<Idle, State>
where State: Default + Send + Debug + 'static,

Creates a new top-level agent builder (ManagedAgent<Idle, State>) with a specified root name.

This method initializes a ManagedAgent in the Idle state, configured with a root Ern derived from the provided name and linked to the system’s broker. The agent is registered as a top-level agent within the runtime.

The returned agent is ready for further configuration (e.g., adding message handlers via act_on) before being started by calling .start() on it.

§Type Parameters
  • State: The user-defined state type for the agent. Must implement Default, Send, Debug, and be 'static.
§Arguments
  • name: A string that will form the root name of the agent’s Ern.
§Returns

A ManagedAgent<Idle, State> instance, ready for configuration and starting.

§Panics

Panics if creating the root Ern from the provided name fails or if creating the internal AgentConfig fails.

Source

pub async fn new_agent<State>(&mut self) -> ManagedAgent<Idle, State>
where State: Default + Send + Debug + 'static,

Creates a new top-level agent builder (ManagedAgent<Idle, State>) with a default name (“agent”).

Similar to AgentRuntime::new_agent_with_name, but uses a default root name “agent” for the agent’s Ern. The agent is registered as a top-level agent within the runtime.

The returned agent is ready for further configuration before being started via .start().

§Type Parameters
  • State: The user-defined state type for the agent. Must implement Default, Send, Debug, and be 'static.
§Returns

A ManagedAgent<Idle, State> instance, ready for configuration and starting.

§Panics

Panics if creating the internal AgentConfig fails.

Source

pub fn agent_count(&self) -> usize

Returns the number of top-level agents currently registered in the runtime.

This count only includes agents directly created via the AgentRuntime and does not include child agents supervised by other agents.

Source

pub async fn new_agent_with_config<State>( &mut self, config: AgentConfig, ) -> ManagedAgent<Idle, State>
where State: Default + Send + Debug + 'static,

Creates a new top-level agent builder (ManagedAgent<Idle, State>) using a provided configuration.

This method initializes a ManagedAgent in the Idle state using the specified AgentConfig. It ensures the agent is configured with the system’s broker if not already set in the config. The agent is registered as a top-level agent within the runtime.

The returned agent is ready for further configuration before being started via .start().

§Type Parameters
  • State: The user-defined state type for the agent. Must implement Default, Send, Debug, and be 'static.
§Arguments
  • config: The AgentConfig to use for the new agent. The broker field will be overridden with the system broker if it’s None.
§Returns

A ManagedAgent<Idle, State> instance, ready for configuration and starting.

Source

pub fn broker(&self) -> AgentHandle

Returns a clone of the handle ([BrokerRef]) to the system’s central message broker.

Source

pub async fn spawn_agent_with_setup_fn<State>( &mut self, config: AgentConfig, setup_fn: impl FnOnce(ManagedAgent<Idle, State>) -> Pin<Box<dyn Future<Output = AgentHandle> + Send + 'static>>, ) -> Result<AgentHandle>
where State: Default + Send + Debug + 'static,

Creates, configures, and starts a top-level agent using a provided configuration and setup function.

This method combines agent creation (using config), custom asynchronous setup (setup_fn), and starting the agent. The setup_fn receives the agent in the Idle state, performs necessary configurations (like adding message handlers), and must call .start() to transition the agent to the Started state, returning its AgentHandle.

The agent is registered as a top-level agent within the runtime.

§Type Parameters
  • State: The state type of the agent. Must implement Default, Send, Debug, and be 'static.
§Arguments
  • config: The AgentConfig to use for creating the agent. The broker field will be overridden with the system broker if it’s None.
  • setup_fn: An asynchronous closure that takes the ManagedAgent<Idle, State>, configures it, calls .start(), and returns the resulting AgentHandle. The closure must be Send + 'static.
§Returns

A Result containing the AgentHandle of the successfully spawned agent, or an error if agent creation or the setup_fn fails.

Source

pub async fn shutdown_all(&mut self) -> Result<()>

Initiates a graceful shutdown of the entire Acton system.

This method attempts to stop all registered top-level agents (and consequently their descendant children through the stop propagation mechanism) by sending them a [SystemSignal::Terminate]. It waits for all top-level agent tasks to complete. Finally, it stops the central message broker agent.

§Returns

An anyhow::Result<()> indicating whether the shutdown process completed successfully. Errors during the stopping of individual agents or the broker will be propagated.

Source

pub async fn spawn_agent<State>( &mut self, setup_fn: impl FnOnce(ManagedAgent<Idle, State>) -> Pin<Box<dyn Future<Output = AgentHandle> + Send + 'static>>, ) -> Result<AgentHandle>
where State: Default + Send + Debug + 'static,

Creates, configures, and starts a top-level agent using a default configuration and a setup function.

This is a convenience method similar to AgentRuntime::spawn_agent_with_setup_fn, but it automatically creates a default AgentConfig (with a default name and the system broker). The provided setup_fn configures and starts the agent.

The agent is registered as a top-level agent within the runtime.

§Type Parameters
  • State: The state type of the agent. Must implement Default, Send, Debug, and be 'static.
§Arguments
  • setup_fn: An asynchronous closure that takes the ManagedAgent<Idle, State>, configures it, calls .start(), and returns the resulting AgentHandle. The closure must be Send + 'static.
§Returns

A Result containing the AgentHandle of the successfully spawned agent, or an error if agent creation or the setup_fn fails.

§Errors

Returns an error if the default AgentConfig cannot be created.

Trait Implementations§

Source§

impl Clone for AgentRuntime

Source§

fn clone(&self) -> AgentRuntime

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for AgentRuntime

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for AgentRuntime

Source§

fn default() -> AgentRuntime

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

impl From<ActonApp> for AgentRuntime

Converts an ActonApp marker into an initialized AgentRuntime.

This implementation defines the system bootstrap process triggered by ActonApp::launch(). It performs the following steps:

  1. Spawns a background Tokio task dedicated to initializing the AgentBroker.
  2. Uses a oneshot channel to receive the AgentHandle of the initialized broker back from the background task.
  3. Blocks the current thread using tokio::task::block_in_place while waiting for the broker initialization to complete. This ensures that ActonApp::launch() does not return until the core system components (like the broker) are ready.
  4. Constructs the AgentRuntime using the received broker handle.

Warning: The use of block_in_place means this conversion should typically only happen once at the very start of the application within the main thread or a dedicated initialization thread, before the main asynchronous workload begins. Calling this from within an existing Tokio runtime task could lead to deadlocks or performance issues.

Source§

fn from(_acton: ActonApp) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> ActonMessage for T
where T: Any + Send + Sync + Debug + DynClone + 'static,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Implementation of as_any for the blanket impl.

Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Implementation of as_any_mut for the blanket impl.

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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