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
//! # Actor Mailbox
//!
//! This module defines the core interfaces and data structures for persistent message queues.
//!
//! ## Reliable Queue Workflow
//!
//! This interface is designed as a reliable queue to prevent message loss when a consumer
//! crashes during message processing. The workflow is as follows:
//!
//! 1. **`dequeue()`**: The consumer retrieves a batch of messages from the queue. These messages
//! are atomically marked as `Inflight` in the database, but are **not deleted**.
//! 2. **Process messages**: The consumer processes these messages locally.
//! 3. **`ack()`**: When a message has been successfully processed, the consumer calls
//! `ack(message_id)`. This **permanently deletes** the message, marking the successful
//! completion of the work unit.
//!
//! If the consumer crashes after `dequeue` but before `ack`, those `Inflight` messages remain
//! in the database. On the next consumer restart, a "cleanup" routine can be implemented to
//! reprocess these "stuck" messages.
use crateStorageResult;
use async_trait;
use ;
use ;
use Arc;
use Uuid;
/// Message priority
/// Message record retrieved from the queue
/// Message processing status
/// Mailbox statistics
/// Observer notified whenever a [`Mailbox`] implementation observes a
/// change in its queue depth.
///
/// Installed on a mailbox via [`Mailbox::set_depth_observer`]. Backends
/// that cannot cheaply report depth on every enqueue may return `false`
/// from that method, in which case consumers must fall back to periodic
/// polling via [`Mailbox::status`].
///
/// The observer is called synchronously from the enqueue code path;
/// implementations should therefore keep their work short (typically a
/// bounded `try_send` into a channel). Implementations must not block
/// the enqueue path and must tolerate missed notifications.
/// Mailbox interface - defines core operations for message persistence
///
/// ## Usage example: `dequeue -> process -> ack` loop
///
/// The `dequeue` method automatically retrieves the next batch of messages. Callers need not
/// worry about batch size; that detail is handled internally by the implementation.
///
/// ```rust,no_run
/// use actr_runtime_mailbox::prelude::*;
/// use std::time::Duration;
///
/// async fn message_processor(mailbox: impl Mailbox) {
/// loop {
/// // 1. Retrieve the next batch of messages from the queue
/// match mailbox.dequeue().await {
/// Ok(messages) => {
/// if messages.is_empty() {
/// tokio::time::sleep(Duration::from_secs(1)).await;
/// continue;
/// }
///
/// // 2. Process messages one by one
/// for msg in messages {
/// println!("Processing message: {}", msg.id);
/// // ... execute your business logic here ...
///
/// // 3. After successful processing, acknowledge this message
/// if let Err(e) = mailbox.ack(msg.id).await {
/// eprintln!("Failed to ack message {}: {}", msg.id, e);
/// }
/// }
/// }
/// Err(e) => {
/// eprintln!("Failed to dequeue messages: {}", e);
/// tokio::time::sleep(Duration::from_secs(5)).await; // Database error, wait longer
/// }
/// }
/// }
/// }
/// ```