|
| 1 | +use async_nats::Client; |
| 2 | +use prost::Message; |
| 3 | +use std::collections::HashMap; |
| 4 | +use taurus_core::runtime::engine::{EmitType, ExecutionId, RespondEmitter}; |
| 5 | +use tokio::sync::mpsc; |
| 6 | +use tucana::shared::value::Kind::{StringValue, StructValue}; |
| 7 | +use tucana::shared::{Struct, Value}; |
| 8 | + |
| 9 | +const DEFAULT_TOPIC_PREFIX: &str = "runtime.emitter"; |
| 10 | + |
| 11 | +pub struct NATSRespondEmitter { |
| 12 | + tx: mpsc::UnboundedSender<NATSEmitMessage>, |
| 13 | +} |
| 14 | + |
| 15 | +struct NATSEmitMessage { |
| 16 | + execution_id: ExecutionId, |
| 17 | + emit_type: EmitType, |
| 18 | + value: Value, |
| 19 | +} |
| 20 | + |
| 21 | +impl NATSRespondEmitter { |
| 22 | + pub fn new(client: Client) -> Self { |
| 23 | + Self::with_topic_prefix(client, DEFAULT_TOPIC_PREFIX) |
| 24 | + } |
| 25 | + |
| 26 | + pub fn with_topic_prefix(client: Client, topic_prefix: impl Into<String>) -> Self { |
| 27 | + let topic_prefix = topic_prefix.into(); |
| 28 | + let (tx, mut rx) = mpsc::unbounded_channel::<NATSEmitMessage>(); |
| 29 | + |
| 30 | + // Keep the public emitter API synchronous while publishing asynchronously. |
| 31 | + // This worker serializes outbound lifecycle events to one NATS topic per execution: |
| 32 | + // `<topic_prefix>.<execution_id>`. |
| 33 | + // Event type is embedded in the payload so subscribers do not need four topic bindings. |
| 34 | + tokio::spawn(async move { |
| 35 | + while let Some(message) = rx.recv().await { |
| 36 | + let topic = format!("{}.{}", topic_prefix, message.execution_id); |
| 37 | + let encoded_payload = encode_emit_message(message.emit_type, message.value); |
| 38 | + |
| 39 | + if let Err(err) = client |
| 40 | + .publish(topic.clone(), encoded_payload.encode_to_vec().into()) |
| 41 | + .await |
| 42 | + { |
| 43 | + log::error!( |
| 44 | + "Failed to publish runtime emit message on '{}': {:?}", |
| 45 | + topic, |
| 46 | + err |
| 47 | + ); |
| 48 | + } |
| 49 | + } |
| 50 | + }); |
| 51 | + |
| 52 | + Self { tx } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +impl RespondEmitter for NATSRespondEmitter { |
| 57 | + fn emit(&self, execution_id: ExecutionId, emit_type: EmitType, value: Value) { |
| 58 | + if let Err(err) = self.tx.send(NATSEmitMessage { |
| 59 | + execution_id, |
| 60 | + emit_type, |
| 61 | + value, |
| 62 | + }) { |
| 63 | + log::debug!( |
| 64 | + "Dropped runtime emit message because NATS emitter worker is unavailable: {:?}", |
| 65 | + err |
| 66 | + ); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +fn encode_emit_message(emit_type: EmitType, payload: Value) -> Value { |
| 72 | + let emit_type_value = Value { |
| 73 | + kind: Some(StringValue(emit_type_as_str(emit_type).to_string())), |
| 74 | + }; |
| 75 | + |
| 76 | + Value { |
| 77 | + kind: Some(StructValue(Struct { |
| 78 | + fields: HashMap::from([ |
| 79 | + ("emit_type".to_string(), emit_type_value), |
| 80 | + ("payload".to_string(), payload), |
| 81 | + ]), |
| 82 | + })), |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +fn emit_type_as_str(emit_type: EmitType) -> &'static str { |
| 87 | + match emit_type { |
| 88 | + EmitType::StartingExec => "starting", |
| 89 | + EmitType::OngoingExec => "ongoing", |
| 90 | + EmitType::FinishedExec => "finished", |
| 91 | + EmitType::FailedExec => "failed", |
| 92 | + } |
| 93 | +} |
0 commit comments