pub struct AppState {
pub watches: RwLock<HashMap<String, WatchContext>>,
}Expand description
§AppState
§Description
The AppState structure represents the in-memory runtime state of the orchestrator.
It manages all active WatchContext instances in a concurrent-safe way using an RwLock<HashMap<String, WatchContext>>.
This state is central to the daemon’s operation: it allows concurrent tasks (e.g., socket requests, watchers) to safely read and modify the currently tracked projects.
§Fields
watches: RwLock<HashMap<String, WatchContext>>
A thread-safe map of project identifiers (id) to their associatedWatchContext.
§Associated Methods
§load_from_disk() -> Result<Self>
Loads the state from the persisted watches.json file.
- Reconstructs all
WatchContextobjects from disk. - Re-initializes their loggers.
- Returns a fully built
AppState.
Example:
let state = AppState::load_from_disk().await?;§save_to_disk(&self) -> Result<()>
Serializes the current state into the watches.json file.
Ensures that all current contexts (WatchContext) are persisted.
Example:
state.save_to_disk().await?;§init_watch_file() -> Result<()>
Initializes the watches.json file if it does not exist.
- Ensures the parent directory exists.
- Creates an empty
WatchRegistryif no registry file is found.
Example:
AppState::init_watch_file().await?;§load_watches() -> Result<WatchRegistry>
Loads the contents of watches.json and deserializes it into a WatchRegistry.
This is a low-level helper used internally by other methods.
Example:
let registry = AppState::load_watches().await?;§add_watch(ctx: &WatchContext) -> Result<()>
Adds or updates a WatchContext inside the watches.json file.
- If a project with the same
project_diralready exists, it is updated. - Otherwise, the new watch is appended.
Example:
let ctx = WatchContextBuilder::new(...).build().await?;
AppState::add_watch(&ctx).await?;§remove_watch_by_id(id: &str) -> Result<()>
Removes a WatchContext from watches.json by its identifier.
Example:
AppState::remove_watch_by_id("project-123").await?;§Usage Example
AppState is typically wrapped in an Arc and shared across async tasks.
For instance, in a socket listener:
pub async fn start_socket_listener(state: Arc<AppState>) -> anyhow::Result<()> {
// state is cloned into each request handler
let state = Arc::clone(&state);
// requests are processed concurrently
}This makes AppState the central shared runtime state for the orchestrator daemon.
§Notes
AppStatebridges persistent storage (watches.json) with in-memory runtime state.- It guarantees safe concurrent access to watches via
RwLock. - It is designed to be long-lived and shared across tasks via
Arc<AppState>.
Fields§
§watches: RwLock<HashMap<String, WatchContext>>Implementations§
Source§impl AppState
impl AppState
pub async fn load_from_disk() -> Result<Self, Error>
pub async fn save_to_disk(&self) -> Result<()>
pub async fn init_watch_file() -> Result<()>
Sourcepub async fn load_watches() -> Result<WatchRegistry>
pub async fn load_watches() -> Result<WatchRegistry>
load watches.json file and Serialize it into WatchRegistry
Sourcepub async fn add_watch(ctx: &WatchContext) -> Result<()>
pub async fn add_watch(ctx: &WatchContext) -> Result<()>
adds a view to the watches.json file
Sourcepub async fn remove_watch_by_id(id: &str) -> Result<()>
pub async fn remove_watch_by_id(id: &str) -> Result<()>
deletes a WatchContext in the watches.json file
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for AppState
impl !RefUnwindSafe for AppState
impl Send for AppState
impl Sync for AppState
impl Unpin for AppState
impl !UnwindSafe for AppState
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more