workflows.rs
1 use crate::core::app::source::Source; 2 use anyhow::{anyhow, Result}; 3 use temporal_client::{tonic::Code, Client, RetryClient, WorkflowOptions}; 4 use temporal_sdk_core::WorkflowClientTrait; 5 use temporal_sdk_core_protos::coresdk::AsJsonPayloadExt; 6 use tracing::{error, info, warn}; 7 8 pub mod golden_path; 9 10 pub async fn start_workflow( 11 client: &RetryClient<Client>, 12 id: String, 13 source: Source, 14 ) -> Result<()> { 15 let input = vec![source.as_json_payload()?]; 16 17 match client 18 .start_workflow( 19 input, 20 "main".to_string(), 21 id, 22 golden_path::name(), 23 None, 24 WorkflowOptions { 25 ..Default::default() 26 }, 27 ) 28 .await 29 { 30 Ok(response) => { 31 info!("workflow started: {response:?}"); 32 Ok(()) 33 } 34 Err(e) => match e.code() { 35 Code::AlreadyExists => { 36 warn!("workflow already exists"); 37 Ok(()) 38 } 39 _ => { 40 error!("failed to start workflow: {}", e.message()); 41 Err(anyhow!("{}", e.code())) 42 } 43 }, 44 } 45 } 46 47 #[cfg(test)] 48 mod tests { 49 use std::path::PathBuf; 50 51 use super::*; 52 use crate::temporal; 53 54 #[tokio::test] 55 async fn test_start_workflow() { 56 let client = temporal::get_client().await.unwrap(); 57 58 start_workflow( 59 &client, 60 "test".to_string(), 61 Source::Git { 62 name: "example-service".to_string(), 63 url: "https://github.com/khuedoan/example-service".to_string(), 64 revision: "828c31f942e8913ab2af53a2841c180586c5b7e1".to_string(), 65 path: PathBuf::from( 66 "/tmp/example-service/828c31f942e8913ab2af53a2841c180586c5b7e1", 67 ), 68 }, 69 ) 70 .await 71 .unwrap(); 72 } 73 74 #[tokio::test] 75 async fn test_start_multiple_workflows() { 76 let client = temporal::get_client().await.unwrap(); 77 78 start_workflow( 79 &client, 80 "test1".to_string(), 81 Source::Git { 82 name: "example-service".to_string(), 83 url: "https://github.com/khuedoan/example-service".to_string(), 84 revision: "828c31f942e8913ab2af53a2841c180586c5b7e1".to_string(), 85 path: PathBuf::from( 86 "/tmp/example-service/828c31f942e8913ab2af53a2841c180586c5b7e1", 87 ), 88 }, 89 ) 90 .await 91 .unwrap(); 92 93 start_workflow( 94 &client, 95 "test1".to_string(), 96 Source::Git { 97 name: "example-service".to_string(), 98 url: "https://github.com/khuedoan/example-service".to_string(), 99 revision: "828c31f942e8913ab2af53a2841c180586c5b7e1".to_string(), 100 path: PathBuf::from( 101 "/tmp/example-service/828c31f942e8913ab2af53a2841c180586c5b7e1", 102 ), 103 }, 104 ) 105 .await 106 .unwrap(); 107 108 start_workflow( 109 &client, 110 "test2".to_string(), 111 Source::Git { 112 name: "example-service".to_string(), 113 url: "https://github.com/khuedoan/example-service".to_string(), 114 revision: "828c31f942e8913ab2af53a2841c180586c5b7e1".to_string(), 115 path: PathBuf::from( 116 "/tmp/example-service/828c31f942e8913ab2af53a2841c180586c5b7e1", 117 ), 118 }, 119 ) 120 .await 121 .unwrap(); 122 } 123 }