crabbyq 1.0.0

A declarative async Rust framework for message-driven microservices.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
pub struct CrabbyService<B> {
    routes: Vec<ServiceRoute>,
    broker: B,
    error_topic: Option<String>,
    error_headers: Option<crate::brokers::base::HeaderMap>,
    shutdown_signal: Option<ShutdownSignal>,
    shutdown_hook: Option<Box<dyn ShutdownHook>>,
}

use crate::brokers::Broker;
use crate::brokers::base::{BrokerError, BrokerMessage, HeaderMap};
use crate::errors::CrabbyError;
use crate::event::Event;
use crate::publish::Publisher;
use crate::response::{HandlerOutcome, error_outcome};
use futures_util::{Stream, StreamExt};
#[cfg(feature = "json")]
use serde::Serialize;
use std::future::Future;
use std::pin::Pin;
use tower::Service;
use tower::ServiceExt;
use tower::util::BoxService;

#[cfg(not(feature = "json"))]
use crate::publish::PreparedPublishPayload;
#[cfg(feature = "json")]
use crate::publish::json_payload;

type ShutdownSignal = Pin<Box<dyn Future<Output = ()> + Send>>;
type ShutdownHookFuture = Pin<Box<dyn Future<Output = Result<(), CrabbyError>> + Send>>;
pub(crate) type ServiceMessageStream = Pin<Box<dyn Stream<Item = BrokerMessage> + Send + Unpin>>;
pub(crate) type MessageStreamInitFuture =
    Pin<Box<dyn Future<Output = Result<ServiceMessageStream, BrokerError>> + Send>>;

pub(crate) trait MessageStreamFactory: Send {
    fn init(self: Box<Self>) -> MessageStreamInitFuture;
}

pub(crate) struct ServiceRoute {
    pub(crate) subject: String,
    pub(crate) error_topic: Option<String>,
    pub(crate) error_headers: Option<HeaderMap>,
    pub(crate) stream_factory: Option<Box<dyn MessageStreamFactory>>,
    pub(crate) service: BoxService<Event, HandlerOutcome, CrabbyError>,
}

#[cfg(feature = "json")]
#[derive(Serialize)]
struct ErrorEvent {
    subject: String,
    reply_to: Option<String>,
    headers: Option<HeaderMap>,
    payload: Vec<u8>,
    error: String,
}

trait ShutdownHook: Send {
    fn call(self: Box<Self>, publisher: Publisher) -> ShutdownHookFuture;
}

impl<F, Fut> ShutdownHook for F
where
    F: FnOnce(Publisher) -> Fut + Send + 'static,
    Fut: Future<Output = Result<(), CrabbyError>> + Send + 'static,
{
    fn call(self: Box<Self>, publisher: Publisher) -> ShutdownHookFuture {
        Box::pin((*self)(publisher))
    }
}

impl<B> CrabbyService<B>
where
    B: Broker + Clone,
{
    pub(crate) fn new(routes: Vec<ServiceRoute>, broker: B) -> Self {
        Self {
            routes,
            broker,
            error_topic: None,
            error_headers: None,
            shutdown_signal: None,
            shutdown_hook: None,
        }
    }

    /// Configures a fallback error topic for routes that do not define their
    /// own [`Router::on_error`][crate::Router::on_error] policy.
    ///
    /// Handler failures are always logged. This topic is only used when the
    /// matched route does not carry a router-level error topic.
    pub fn on_error(mut self, topic: &str) -> Self {
        self.error_topic = Some(topic.to_string());
        self
    }

    /// Alias for [`CrabbyService::on_error`] using DLQ terminology.
    pub fn dlq(self, topic: &str) -> Self {
        self.on_error(topic)
    }

    /// Configures fallback headers for published error events.
    ///
    /// These headers are merged with route-level error headers when an error is
    /// published. Route-level headers override service-level ones on duplicate
    /// keys.
    pub fn error_headers(mut self, headers: HeaderMap) -> Self {
        self.error_headers = Some(headers);
        self
    }

    /// Replaces the default Ctrl+C handling with a custom graceful shutdown signal.
    ///
    /// Once this signal resolves, the service stops accepting new messages,
    /// runs the optional shutdown hook while the broker is still available, and
    /// then returns from [`CrabbyService::serve`].
    pub fn with_graceful_shutdown<F>(mut self, signal: F) -> Self
    where
        F: Future<Output = ()> + Send + 'static,
    {
        self.shutdown_signal = Some(Box::pin(signal));
        self
    }

    /// Registers a final async hook that runs during graceful shutdown.
    ///
    /// The hook receives a live [`Publisher`] so the service can emit one last
    /// message before the broker-backed runtime is dropped.
    pub fn on_shutdown<F, Fut>(mut self, hook: F) -> Self
    where
        F: FnOnce(Publisher) -> Fut + Send + 'static,
        Fut: Future<Output = Result<(), CrabbyError>> + Send + 'static,
    {
        self.shutdown_hook = Some(Box::new(hook));
        self
    }

    /// Starts the broker subscription loop and dispatches incoming messages.
    ///
    /// Error behavior is:
    /// - handler failures are always logged;
    /// - route-level `on_error(...)` is used first when present;
    /// - otherwise the service-level fallback `on_error(...)`/`dlq(...)` is used;
    /// - if no error topic is configured, the failure is only logged.
    pub async fn serve(mut self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let publisher = Publisher::new(self.broker.clone());

        // Collecting subjects for plain broker subscription
        let subjects: Vec<String> = self
            .routes
            .iter()
            .filter(|route| route.stream_factory.is_none())
            .map(|route| route.subject.clone())
            .collect();

        let mut streams: Vec<ServiceMessageStream> = Vec::new();

        if !subjects.is_empty() {
            tracing::info!("Subscribing to subjects: {:?}", subjects);
            streams.push(Box::pin(self.broker.subscribe(&subjects).await?));
        }

        for route in &mut self.routes {
            if let Some(factory) = route.stream_factory.take() {
                streams.push(factory.init().await?);
            }
        }

        if streams.is_empty() {
            tracing::warn!("No routes registered, service will not subscribe to any subjects");
            self.wait_for_shutdown().await?;
            self.run_shutdown_hook(publisher).await;
            return Ok(());
        }

        let mut stream = futures_util::stream::select_all(streams);

        tracing::info!("CrabbyQ service started!");

        let mut shutdown_signal = self
            .shutdown_signal
            .take()
            .unwrap_or_else(default_shutdown_signal);

        loop {
            tokio::select! {
                _ = &mut shutdown_signal => {
                    tracing::info!("Shutdown signal received, stopping message intake");
                    break;
                }
                message = stream.next() => {
                    match message {
                        Some(msg) => self.handle_message(msg).await,
                        None => break,
                    }
                }
            }
        }

        self.run_shutdown_hook(publisher).await;
        tracing::info!("CrabbyQ service stopped");
        Ok(())
    }

    async fn wait_for_shutdown(&mut self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        if let Some(signal) = self.shutdown_signal.take() {
            signal.await;
            return Ok(());
        }

        tokio::signal::ctrl_c()
            .await
            .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)
    }

    async fn run_shutdown_hook(&mut self, publisher: Publisher) {
        let Some(hook) = self.shutdown_hook.take() else {
            return;
        };

        if let Err(error) = hook.call(publisher).await {
            tracing::error!("Shutdown hook error: {}", error);
        }
    }

    async fn handle_message(&mut self, mut msg: BrokerMessage) {
        let subject = msg.subject.clone();
        let acknowledger = msg.acknowledger.take();
        let event = Event::new(msg.subject, msg.payload.into(), msg.headers, msg.reply_to);

        match self.dispatch_event(&subject, event).await {
            Some(result) => {
                let error_message = result.outcome.error_message.clone();
                let route_error_topic = result.route_error_topic.clone();
                let route_error_headers = result.route_error_headers.clone();
                let service_error_topic = self.error_topic.clone();
                let service_error_headers = self.error_headers.clone();
                let reply_to = result.reply_to.clone();
                let headers = result.headers.clone();
                let payload = result.payload.clone();

                Self::publish_reply(
                    &self.broker,
                    &subject,
                    reply_to.as_deref(),
                    result.outcome.response,
                )
                .await;
                Self::publish_error(
                    &self.broker,
                    &subject,
                    error_message,
                    route_error_topic,
                    route_error_headers,
                    service_error_topic,
                    service_error_headers,
                    reply_to,
                    headers,
                    payload,
                )
                .await;

                if result.outcome.error_message.is_none() {
                    Self::ack_message(&subject, acknowledger).await;
                }
            }
            None => tracing::warn!("No handler found for subject: {}", subject),
        }
    }

    async fn ack_message(
        subject: &str,
        acknowledger: Option<Box<dyn crate::brokers::base::Acknowledger>>,
    ) {
        let Some(acknowledger) = acknowledger else {
            return;
        };

        if let Err(error) = acknowledger.ack().await {
            tracing::error!("Ack error for subject '{}': {}", subject, error);
        }
    }

    async fn publish_error(
        broker: &B,
        subject: &str,
        error_message: Option<String>,
        route_error_topic: Option<String>,
        route_error_headers: Option<HeaderMap>,
        service_error_topic: Option<String>,
        service_error_headers: Option<HeaderMap>,
        reply_to: Option<String>,
        message_headers: Option<HeaderMap>,
        payload: Vec<u8>,
    ) {
        let Some(error_message) = error_message else {
            return;
        };

        // Logging is unconditional so operational visibility does not depend on
        // whether the application configured an error topic.
        tracing::error!("Handler error for subject '{}': {}", subject, error_message);

        let error_topic = route_error_topic
            .as_deref()
            .or(service_error_topic.as_deref());

        let Some(error_topic) = error_topic else {
            return;
        };

        let mut error_headers = service_error_headers.unwrap_or_default();
        if let Some(route_headers) = route_error_headers {
            error_headers.extend(route_headers);
        }

        #[cfg(feature = "json")]
        let prepared = match json_payload(ErrorEvent {
            subject: subject.to_string(),
            reply_to,
            headers: message_headers,
            payload,
            error: error_message.clone(),
        }) {
            Ok(prepared) => prepared,
            Err(e) => {
                tracing::error!(
                    "Failed to serialize error event for subject '{}': {}",
                    subject,
                    e
                );
                return;
            }
        };

        #[cfg(not(feature = "json"))]
        let prepared = PreparedPublishPayload {
            payload: error_message.into_bytes(),
            headers: None,
        };

        let mut prepared = prepared;
        prepared.headers = match (
            prepared.headers,
            (!error_headers.is_empty()).then_some(error_headers),
        ) {
            (None, extra) => extra,
            (Some(base), None) => Some(base),
            (Some(mut base), Some(extra)) => {
                base.extend(extra);
                Some(base)
            }
        };

        if let Err(e) = broker
            .publish(error_topic, &prepared.payload, prepared.headers.as_ref())
            .await
        {
            tracing::error!(
                "Error publish failure for subject '{}' to topic '{}': {}",
                subject,
                error_topic,
                e
            );
        }
    }

    async fn dispatch_event(&mut self, subject: &str, event: Event) -> Option<DispatchResult> {
        let reply_to = event.reply_to().map(str::to_owned);
        let headers = event.headers().cloned();
        let payload = event.payload.clone().to_vec();

        for route in &mut self.routes {
            if subject != route.subject {
                continue;
            }

            let outcome = match route.service.ready().await {
                Ok(ready) => match ready.call(event).await {
                    Ok(outcome) => outcome,
                    Err(e) => match error_outcome(e) {
                        Ok(outcome) => outcome,
                        Err(error) => {
                            tracing::error!(
                                "Failed to convert service error for subject '{}': {}",
                                subject,
                                error
                            );
                            HandlerOutcome {
                                response: None,
                                error_message: Some("internal handler error".to_string()),
                            }
                        }
                    },
                },
                Err(e) => match error_outcome(e) {
                    Ok(outcome) => outcome,
                    Err(error) => {
                        tracing::error!(
                            "Failed to convert readiness error for subject '{}': {}",
                            subject,
                            error
                        );
                        HandlerOutcome {
                            response: None,
                            error_message: Some("service readiness error".to_string()),
                        }
                    }
                },
            };

            return Some(DispatchResult {
                reply_to,
                headers,
                payload,
                route_error_topic: route.error_topic.clone(),
                route_error_headers: route.error_headers.clone(),
                outcome,
            });
        }

        None
    }

    async fn publish_reply(
        broker: &B,
        subject: &str,
        reply_to: Option<&str>,
        response: Option<crate::publish::PreparedPublishPayload>,
    ) {
        let (Some(reply_to), Some(response)) = (reply_to, response) else {
            return;
        };

        if let Err(e) = broker
            .publish(&reply_to, &response.payload, response.headers.as_ref())
            .await
        {
            tracing::error!("Reply publish error for subject '{}': {}", subject, e);
        }
    }
}

struct DispatchResult {
    reply_to: Option<String>,
    headers: Option<HeaderMap>,
    payload: Vec<u8>,
    route_error_topic: Option<String>,
    route_error_headers: Option<HeaderMap>,
    outcome: HandlerOutcome,
}

fn default_shutdown_signal() -> ShutdownSignal {
    Box::pin(async {
        if let Err(error) = tokio::signal::ctrl_c().await {
            tracing::error!("Failed to listen for Ctrl+C: {}", error);
        }
    })
}