forge_core/function/dispatch.rs
1use std::future::Future;
2use std::pin::Pin;
3
4use uuid::Uuid;
5
6use crate::error::Result;
7use crate::job::JobInfo;
8use crate::workflow::WorkflowInfo;
9
10/// Trait for dispatching jobs from function contexts.
11///
12/// This trait allows mutation and action contexts to dispatch background jobs
13/// without directly depending on the runtime's JobDispatcher.
14pub trait JobDispatch: Send + Sync {
15 /// Get job info by name for auth checking.
16 fn get_info(&self, job_type: &str) -> Option<JobInfo>;
17
18 /// Dispatch a job by its registered name.
19 ///
20 /// # Arguments
21 /// * `job_type` - The registered name of the job type
22 /// * `args` - JSON-serialized arguments for the job
23 ///
24 /// # Returns
25 /// The UUID of the dispatched job
26 fn dispatch_by_name(
27 &self,
28 job_type: &str,
29 args: serde_json::Value,
30 owner_subject: Option<String>,
31 ) -> Pin<Box<dyn Future<Output = Result<Uuid>> + Send + '_>>;
32
33 /// Request cancellation for a job.
34 fn cancel(
35 &self,
36 job_id: Uuid,
37 reason: Option<String>,
38 ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + '_>>;
39}
40
41/// Trait for starting workflows from function contexts.
42///
43/// This trait allows mutation and action contexts to start workflows
44/// without directly depending on the runtime's WorkflowExecutor.
45pub trait WorkflowDispatch: Send + Sync {
46 /// Get workflow info by name for auth checking.
47 fn get_info(&self, workflow_name: &str) -> Option<WorkflowInfo>;
48
49 /// Start a workflow by its registered name.
50 ///
51 /// # Arguments
52 /// * `workflow_name` - The registered name of the workflow
53 /// * `input` - JSON-serialized input for the workflow
54 ///
55 /// # Returns
56 /// The UUID of the started workflow run
57 fn start_by_name(
58 &self,
59 workflow_name: &str,
60 input: serde_json::Value,
61 owner_subject: Option<String>,
62 ) -> Pin<Box<dyn Future<Output = Result<Uuid>> + Send + '_>>;
63}