|
| 1 | +// Forwards compatibility from old operation ctx to new workflows |
| 2 | + |
| 3 | +use std::{fmt::Debug, time::Duration}; |
| 4 | + |
| 5 | +use global_error::prelude::*; |
| 6 | +use serde::Serialize; |
| 7 | +use uuid::Uuid; |
| 8 | + |
| 9 | +use crate::{ |
| 10 | + DatabaseHandle, DatabasePostgres, Operation, OperationCtx, OperationInput, Signal, Workflow, |
| 11 | + WorkflowError, WorkflowInput, |
| 12 | +}; |
| 13 | + |
| 14 | +pub async fn dispatch_workflow<I, B>( |
| 15 | + ctx: &rivet_operation::OperationContext<B>, |
| 16 | + input: I, |
| 17 | +) -> GlobalResult<Uuid> |
| 18 | +where |
| 19 | + I: WorkflowInput, |
| 20 | + <I as WorkflowInput>::Workflow: Workflow<Input = I>, |
| 21 | + B: Debug + Clone, |
| 22 | +{ |
| 23 | + if ctx.from_workflow { |
| 24 | + bail!("cannot dispatch a workflow from an operation within a workflow execution. trigger it from the workflow's body."); |
| 25 | + } |
| 26 | + |
| 27 | + let name = I::Workflow::name(); |
| 28 | + |
| 29 | + tracing::debug!(%name, ?input, "dispatching workflow"); |
| 30 | + |
| 31 | + let id = Uuid::new_v4(); |
| 32 | + |
| 33 | + // Serialize input |
| 34 | + let input_val = serde_json::to_value(input) |
| 35 | + .map_err(WorkflowError::SerializeWorkflowOutput) |
| 36 | + .map_err(GlobalError::raw)?; |
| 37 | + |
| 38 | + db(ctx) |
| 39 | + .await? |
| 40 | + .dispatch_workflow(ctx.ray_id(), id, &name, input_val) |
| 41 | + .await |
| 42 | + .map_err(GlobalError::raw)?; |
| 43 | + |
| 44 | + tracing::info!(%name, ?id, "workflow dispatched"); |
| 45 | + |
| 46 | + Ok(id) |
| 47 | +} |
| 48 | + |
| 49 | +pub async fn wait_for_workflow<W: Workflow, B: Debug + Clone>( |
| 50 | + ctx: &rivet_operation::OperationContext<B>, |
| 51 | + workflow_id: Uuid, |
| 52 | +) -> GlobalResult<W::Output> { |
| 53 | + tracing::info!(name=W::name(), id=?workflow_id, "waiting for workflow"); |
| 54 | + |
| 55 | + let period = Duration::from_millis(50); |
| 56 | + let mut interval = tokio::time::interval(period); |
| 57 | + loop { |
| 58 | + interval.tick().await; |
| 59 | + |
| 60 | + // Check if state finished |
| 61 | + let workflow = db(ctx) |
| 62 | + .await? |
| 63 | + .get_workflow(workflow_id) |
| 64 | + .await |
| 65 | + .map_err(GlobalError::raw)? |
| 66 | + .ok_or(WorkflowError::WorkflowNotFound) |
| 67 | + .map_err(GlobalError::raw)?; |
| 68 | + if let Some(output) = workflow.parse_output::<W>().map_err(GlobalError::raw)? { |
| 69 | + return Ok(output); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +pub async fn workflow<I, B>( |
| 75 | + ctx: &rivet_operation::OperationContext<B>, |
| 76 | + input: I, |
| 77 | +) -> GlobalResult<<<I as WorkflowInput>::Workflow as Workflow>::Output> |
| 78 | +where |
| 79 | + I: WorkflowInput, |
| 80 | + <I as WorkflowInput>::Workflow: Workflow<Input = I>, |
| 81 | + B: Debug + Clone, |
| 82 | +{ |
| 83 | + let workflow_id = dispatch_workflow(ctx, input).await?; |
| 84 | + let output = wait_for_workflow::<I::Workflow, _>(ctx, workflow_id).await?; |
| 85 | + Ok(output) |
| 86 | +} |
| 87 | + |
| 88 | +pub async fn signal<I: Signal + Serialize, B: Debug + Clone>( |
| 89 | + ctx: &rivet_operation::OperationContext<B>, |
| 90 | + workflow_id: Uuid, |
| 91 | + input: I, |
| 92 | +) -> GlobalResult<Uuid> { |
| 93 | + if ctx.from_workflow { |
| 94 | + bail!("cannot dispatch a signal from an operation within a workflow execution. trigger it from the workflow's body."); |
| 95 | + } |
| 96 | + |
| 97 | + tracing::debug!(name=%I::name(), %workflow_id, "dispatching signal"); |
| 98 | + |
| 99 | + let signal_id = Uuid::new_v4(); |
| 100 | + |
| 101 | + // Serialize input |
| 102 | + let input_val = serde_json::to_value(input) |
| 103 | + .map_err(WorkflowError::SerializeSignalBody) |
| 104 | + .map_err(GlobalError::raw)?; |
| 105 | + |
| 106 | + db(ctx) |
| 107 | + .await? |
| 108 | + .publish_signal(ctx.ray_id(), workflow_id, signal_id, I::name(), input_val) |
| 109 | + .await |
| 110 | + .map_err(GlobalError::raw)?; |
| 111 | + |
| 112 | + Ok(signal_id) |
| 113 | +} |
| 114 | + |
| 115 | +pub async fn op<I, B>( |
| 116 | + ctx: &rivet_operation::OperationContext<B>, |
| 117 | + input: I, |
| 118 | +) -> GlobalResult<<<I as OperationInput>::Operation as Operation>::Output> |
| 119 | +where |
| 120 | + I: OperationInput, |
| 121 | + <I as OperationInput>::Operation: Operation<Input = I>, |
| 122 | + B: Debug + Clone, |
| 123 | +{ |
| 124 | + let mut ctx = OperationCtx::new( |
| 125 | + db(ctx).await?, |
| 126 | + ctx.conn(), |
| 127 | + ctx.ray_id(), |
| 128 | + ctx.req_ts(), |
| 129 | + ctx.from_workflow(), |
| 130 | + I::Operation::name(), |
| 131 | + ); |
| 132 | + |
| 133 | + I::Operation::run(&mut ctx, &input) |
| 134 | + .await |
| 135 | + .map_err(WorkflowError::OperationFailure) |
| 136 | + .map_err(GlobalError::raw) |
| 137 | +} |
| 138 | + |
| 139 | +// Get crdb pool as a trait object |
| 140 | +async fn db<B: Debug + Clone>( |
| 141 | + ctx: &rivet_operation::OperationContext<B>, |
| 142 | +) -> GlobalResult<DatabaseHandle> { |
| 143 | + let crdb = ctx.crdb().await?; |
| 144 | + |
| 145 | + Ok(DatabasePostgres::from_pool(crdb)) |
| 146 | +} |
0 commit comments