#[non_exhaustive]pub struct DaemonContext {
pub daemon_name: String,
pub instance_id: Uuid,
/* private fields */
}Expand description
Context available to daemon handlers.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.daemon_name: String§instance_id: UuidImplementations§
Source§impl DaemonContext
impl DaemonContext
Sourcepub fn new(
daemon_name: String,
instance_id: Uuid,
db_pool: PgPool,
http_client: CircuitBreakerClient,
shutdown_rx: Receiver<bool>,
) -> Self
pub fn new( daemon_name: String, instance_id: Uuid, db_pool: PgPool, http_client: CircuitBreakerClient, shutdown_rx: Receiver<bool>, ) -> Self
Create a new daemon context.
Sourcepub fn with_kv(self, kv: Arc<dyn KvHandle>) -> Self
pub fn with_kv(self, kv: Arc<dyn KvHandle>) -> Self
Attach a KV store handle. Called by the runtime before handing the context to the handler.
Sourcepub fn with_job_dispatch(self, dispatcher: Arc<dyn JobDispatch>) -> Self
pub fn with_job_dispatch(self, dispatcher: Arc<dyn JobDispatch>) -> Self
Set job dispatcher.
Sourcepub fn with_workflow_dispatch(
self,
dispatcher: Arc<dyn WorkflowDispatch>,
) -> Self
pub fn with_workflow_dispatch( self, dispatcher: Arc<dyn WorkflowDispatch>, ) -> Self
Set workflow dispatcher.
Sourcepub fn with_env_provider(self, provider: Arc<dyn EnvProvider>) -> Self
pub fn with_env_provider(self, provider: Arc<dyn EnvProvider>) -> Self
Set environment provider.
pub fn db(&self) -> ForgeDb
Sourcepub async fn conn(&self) -> Result<ForgeConn<'static>>
pub async fn conn(&self) -> Result<ForgeConn<'static>>
Acquire a connection compatible with sqlx compile-time checked macros.
pub fn http(&self) -> HttpClient
pub fn raw_http(&self) -> &Client
pub fn set_http_timeout(&mut self, timeout: Option<Duration>)
Sourcepub async fn dispatch_job<T: Serialize>(
&self,
job_type: &str,
args: T,
) -> Result<Uuid>
pub async fn dispatch_job<T: Serialize>( &self, job_type: &str, args: T, ) -> Result<Uuid>
Dispatch a background job.
Sourcepub async fn dispatch<J: ForgeJob>(&self, args: J::Args) -> Result<Uuid>
pub async fn dispatch<J: ForgeJob>(&self, args: J::Args) -> Result<Uuid>
Type-safe dispatch: resolves the job name from the type’s ForgeJob
impl and serializes the args at the call site.
Sourcepub async fn cancel_job(
&self,
job_id: Uuid,
reason: Option<String>,
) -> Result<bool>
pub async fn cancel_job( &self, job_id: Uuid, reason: Option<String>, ) -> Result<bool>
Request cancellation for a job.
Sourcepub async fn start_workflow<T: Serialize>(
&self,
workflow_name: &str,
input: T,
) -> Result<Uuid>
pub async fn start_workflow<T: Serialize>( &self, workflow_name: &str, input: T, ) -> Result<Uuid>
Start a workflow.
Sourcepub async fn start<W: ForgeWorkflow>(&self, input: W::Input) -> Result<Uuid>
pub async fn start<W: ForgeWorkflow>(&self, input: W::Input) -> Result<Uuid>
Type-safe workflow start.
Sourcepub fn is_shutdown_requested(&self) -> bool
pub fn is_shutdown_requested(&self) -> bool
Check if shutdown has been requested.
Sourcepub async fn shutdown_signal(&self)
pub async fn shutdown_signal(&self)
Wait for shutdown signal.
Use this in a tokio::select! to handle graceful shutdown:
tokio::select! {
_ = tokio::time::sleep(Duration::from_secs(60)) => {}
_ = ctx.shutdown_signal() => break,
}Sourcepub async fn tick(&self, interval: Duration) -> bool
pub async fn tick(&self, interval: Duration) -> bool
Sleep for interval, waking early if shutdown is requested.
Returns true if the daemon should continue, false if shutdown was
requested before or during the sleep. Intended for the main daemon loop:
while ctx.tick(Duration::from_secs(60)).await {
// do periodic work
}Trait Implementations§
Source§impl EnvAccess for DaemonContext
impl EnvAccess for DaemonContext
fn env_provider(&self) -> &dyn EnvProvider
fn env(&self, key: &str) -> Option<String>
fn env_or(&self, key: &str, default: &str) -> String
fn env_require(&self, key: &str) -> Result<String>
fn env_parse<T: FromStr>(&self, key: &str) -> Result<T>
Source§fn env_parse_or<T: FromStr>(&self, key: &str, default: T) -> Result<T>
fn env_parse_or<T: FromStr>(&self, key: &str, default: T) -> Result<T>
fn env_contains(&self, key: &str) -> bool
Auto Trait Implementations§
impl !Freeze for DaemonContext
impl !RefUnwindSafe for DaemonContext
impl Send for DaemonContext
impl Sync for DaemonContext
impl Unpin for DaemonContext
impl UnsafeUnpin for DaemonContext
impl !UnwindSafe for DaemonContext
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§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