Skip to main content

ContextFactory

Trait ContextFactory 

Source
pub trait ContextFactory: Send + Sync {
    // Required method
    fn build(&self, actor_json: &Value) -> Result<Box<dyn ScriptContext>>;
}
Expand description

Builds a ScriptContext from JSON captured at schedule time.

Install on ChrononBuilder::context_factory at boot. The executor calls Self::build for every dispatched run using the job’s actor_json.

ImplementationWhen to use
JsonScriptContextFactoryExamples / handlers that only need label + actor JSON
NoOpContextFactoryTests and benches
CustomProduction identity, sessions, permissions

§Examples

Custom factory sketch:

use chronon_core::{ContextFactory, Result, ScriptContext};
use serde_json::Value;

struct AppCtx { label: String, actor_json: Value }
impl ScriptContext for AppCtx {
    fn label(&self) -> &str { &self.label }
    fn actor_json(&self) -> &Value { &self.actor_json }
}

struct AppFactory;
impl ContextFactory for AppFactory {
    fn build(&self, actor_json: &Value) -> Result<Box<dyn ScriptContext>> {
        Ok(Box::new(AppCtx {
            label: actor_json.get("user").and_then(|v| v.as_str()).unwrap_or("anon").into(),
            actor_json: actor_json.clone(),
        }))
    }
}

let ctx = AppFactory.build(&serde_json::json!({"user": "bob"})).unwrap();
assert_eq!(ctx.label(), "bob");

Required Methods§

Source

fn build(&self, actor_json: &Value) -> Result<Box<dyn ScriptContext>>

Reconstruct handler context from actor JSON stored on the job.

Returns IdentityError (mapped to ChrononError::Internal) when the payload cannot be decoded into application identity.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§