Skip to main content

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    ) -> Pin<Box<dyn Future<Output = Result<Uuid>> + Send + '_>>;
31
32    /// Request cancellation for a job.
33    fn cancel(
34        &self,
35        job_id: Uuid,
36        reason: Option<String>,
37    ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + '_>>;
38}
39
40/// Trait for starting workflows from function contexts.
41///
42/// This trait allows mutation and action contexts to start workflows
43/// without directly depending on the runtime's WorkflowExecutor.
44pub trait WorkflowDispatch: Send + Sync {
45    /// Get workflow info by name for auth checking.
46    fn get_info(&self, workflow_name: &str) -> Option<WorkflowInfo>;
47
48    /// Start a workflow by its registered name.
49    ///
50    /// # Arguments
51    /// * `workflow_name` - The registered name of the workflow
52    /// * `input` - JSON-serialized input for the workflow
53    ///
54    /// # Returns
55    /// The UUID of the started workflow run
56    fn start_by_name(
57        &self,
58        workflow_name: &str,
59        input: serde_json::Value,
60    ) -> Pin<Box<dyn Future<Output = Result<Uuid>> + Send + '_>>;
61}