-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for triggering nested workflows.
- Loading branch information
1 parent
7a1b6c9
commit 191efc2
Showing
12 changed files
with
270 additions
and
69 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use hatchet_sdk::{Client, Context, StepBuilder, WorkflowBuilder}; | ||
|
||
async fn execute_hello( | ||
context: Context, | ||
_: serde_json::Value, | ||
) -> anyhow::Result<serde_json::Value> { | ||
context | ||
.trigger_workflow( | ||
"world", | ||
serde_json::json!({ | ||
"x": 42 | ||
}), | ||
) | ||
.await?; | ||
Ok(serde_json::json!({ | ||
"message": "Hello" | ||
})) | ||
} | ||
|
||
async fn execute_world( | ||
_context: Context, | ||
_: serde_json::Value, | ||
) -> anyhow::Result<serde_json::Value> { | ||
Ok(serde_json::json!({ | ||
"message": "World" | ||
})) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> anyhow::Result<()> { | ||
dotenv::dotenv().ok(); | ||
tracing_subscriber::fmt() | ||
.with_target(false) | ||
.with_env_filter( | ||
tracing_subscriber::EnvFilter::from_default_env() | ||
.add_directive("hatchet_sdk=debug".parse()?), | ||
) | ||
.init(); | ||
|
||
let client = Client::new()?; | ||
let mut worker = client.worker("example_spawn_workflow").build(); | ||
worker.register_workflow( | ||
WorkflowBuilder::default() | ||
.name("hello") | ||
.step( | ||
StepBuilder::default() | ||
.name("execute") | ||
.function(&execute_hello) | ||
.build()?, | ||
) | ||
.build()?, | ||
); | ||
worker.register_workflow( | ||
WorkflowBuilder::default() | ||
.name("world") | ||
.step( | ||
StepBuilder::default() | ||
.name("execute") | ||
.function(&execute_world) | ||
.build()?, | ||
) | ||
.build()?, | ||
); | ||
worker.start().await?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use futures_util::lock::Mutex; | ||
use tracing::info; | ||
|
||
use crate::worker::{grpc, ServiceWithAuthorization}; | ||
|
||
pub struct Context { | ||
workflow_run_id: String, | ||
workflow_step_run_id: String, | ||
workflow_service_client_and_spawn_index: Mutex<( | ||
grpc::workflow_service_client::WorkflowServiceClient< | ||
tonic::service::interceptor::InterceptedService< | ||
tonic::transport::Channel, | ||
ServiceWithAuthorization, | ||
>, | ||
>, | ||
u16, | ||
)>, | ||
} | ||
|
||
impl Context { | ||
pub(crate) fn new( | ||
workflow_run_id: String, | ||
workflow_step_run_id: String, | ||
workflow_service_client: grpc::workflow_service_client::WorkflowServiceClient< | ||
tonic::service::interceptor::InterceptedService< | ||
tonic::transport::Channel, | ||
ServiceWithAuthorization, | ||
>, | ||
>, | ||
) -> Self { | ||
Self { | ||
workflow_run_id, | ||
workflow_service_client_and_spawn_index: Mutex::new((workflow_service_client, 0)), | ||
workflow_step_run_id, | ||
} | ||
} | ||
|
||
pub async fn trigger_workflow<I: serde::Serialize>( | ||
&self, | ||
workflow_name: &str, | ||
input: I, | ||
) -> anyhow::Result<()> { | ||
info!("Scheduling another workflow {workflow_name}"); | ||
let mut mutex_guard = self.workflow_service_client_and_spawn_index.lock().await; | ||
let (workflow_service_client, spawn_index) = &mut *mutex_guard; | ||
let response = workflow_service_client | ||
.trigger_workflow(grpc::TriggerWorkflowRequest { | ||
name: workflow_name.to_owned(), | ||
input: serde_json::to_string(&input).expect("must succeed"), | ||
parent_id: Some(self.workflow_run_id.clone()), | ||
parent_step_run_id: Some(self.workflow_step_run_id.clone()), | ||
child_index: Some(*spawn_index as i32), | ||
child_key: None, | ||
additional_metadata: None, // FIXME: Add support. | ||
desired_worker_id: None, // FIXME: Add support. | ||
priority: Some(1), // FIXME: Add support. | ||
}) | ||
.await | ||
.map_err(crate::InternalError::CouldNotTriggerWorkflow) | ||
.map_err(crate::Error::Internal)? | ||
.into_inner(); | ||
info!( | ||
"Scheduled another workflow run ID: {}", | ||
response.workflow_run_id | ||
); | ||
*spawn_index += 1; | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub(crate) type StepFunction = | ||
dyn Fn( | ||
Context, | ||
serde_json::Value, | ||
) -> futures_util::future::LocalBoxFuture<'static, anyhow::Result<serde_json::Value>>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.