|
| 1 | +use tracing::info; |
| 2 | + |
| 3 | +use crate::worker::{grpc, ServiceWithAuthorization}; |
| 4 | + |
| 5 | +pub struct Context { |
| 6 | + spawn_index: u16, |
| 7 | + workflow_run_id: String, |
| 8 | + workflow_step_run_id: String, |
| 9 | + workflow_service_client: grpc::workflow_service_client::WorkflowServiceClient< |
| 10 | + tonic::service::interceptor::InterceptedService< |
| 11 | + tonic::transport::Channel, |
| 12 | + ServiceWithAuthorization, |
| 13 | + >, |
| 14 | + >, |
| 15 | +} |
| 16 | + |
| 17 | +impl Context { |
| 18 | + pub(crate) fn new( |
| 19 | + workflow_run_id: String, |
| 20 | + workflow_step_run_id: String, |
| 21 | + workflow_service_client: grpc::workflow_service_client::WorkflowServiceClient< |
| 22 | + tonic::service::interceptor::InterceptedService< |
| 23 | + tonic::transport::Channel, |
| 24 | + ServiceWithAuthorization, |
| 25 | + >, |
| 26 | + >, |
| 27 | + ) -> Self { |
| 28 | + Self { |
| 29 | + spawn_index: 0, |
| 30 | + workflow_run_id, |
| 31 | + workflow_service_client, |
| 32 | + workflow_step_run_id, |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + pub async fn trigger_workflow<I: serde::Serialize>( |
| 37 | + &mut self, |
| 38 | + workflow_name: &str, |
| 39 | + input: I, |
| 40 | + ) -> anyhow::Result<()> { |
| 41 | + info!("Scheduling another workflow {workflow_name}"); |
| 42 | + let response = self |
| 43 | + .workflow_service_client |
| 44 | + .trigger_workflow(grpc::TriggerWorkflowRequest { |
| 45 | + name: workflow_name.to_owned(), |
| 46 | + input: serde_json::to_string(&input).expect("must succeed"), |
| 47 | + parent_id: Some(self.workflow_run_id.clone()), |
| 48 | + parent_step_run_id: Some(self.workflow_step_run_id.clone()), |
| 49 | + child_index: Some(self.spawn_index as i32), |
| 50 | + child_key: None, |
| 51 | + additional_metadata: None, // FIXME: Add support. |
| 52 | + desired_worker_id: None, // FIXME: Add support. |
| 53 | + priority: Some(1), // FIXME: Add support. |
| 54 | + }) |
| 55 | + .await |
| 56 | + .map_err(crate::InternalError::CouldNotTriggerWorkflow) |
| 57 | + .map_err(crate::Error::Internal)? |
| 58 | + .into_inner(); |
| 59 | + info!( |
| 60 | + "Scheduled another workflow run ID: {}", |
| 61 | + response.workflow_run_id |
| 62 | + ); |
| 63 | + self.spawn_index += 1; |
| 64 | + Ok(()) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +pub(crate) type StepFunction = |
| 69 | + dyn Fn( |
| 70 | + Context, |
| 71 | + serde_json::Value, |
| 72 | + ) -> futures_util::future::LocalBoxFuture<'static, anyhow::Result<serde_json::Value>>; |
0 commit comments