33 * @file Operation.hpp
44 * @author Gaspard Kirira
55 *
6- * Copyright 2025, Gaspard Kirira. All rights reserved.
6+ * Copyright 2025, Gaspard Kirira.
7+ * All rights reserved.
78 * https://github.com/vixcpp/vix
9+ *
810 * Use of this source code is governed by a MIT license
911 * that can be found in the License file.
1012 *
2123
2224namespace vix ::sync
2325{
24-
25- enum class OperationStatus : uint8_t
26+ /* *
27+ * @brief Lifecycle status of a sync operation.
28+ *
29+ * OperationStatus represents the durable state of an operation
30+ * as it flows through the sync pipeline.
31+ */
32+ enum class OperationStatus : std::uint8_t
2633 {
34+ /* *
35+ * @brief Operation is persisted and waiting to be processed.
36+ */
2737 Pending = 0 ,
38+
39+ /* *
40+ * @brief Operation has been claimed and is currently being processed.
41+ */
2842 InFlight,
43+
44+ /* *
45+ * @brief Operation completed successfully.
46+ */
2947 Done,
48+
49+ /* *
50+ * @brief Operation failed but may be retried.
51+ */
3052 Failed,
53+
54+ /* *
55+ * @brief Operation failed permanently and will not be retried.
56+ */
3157 PermanentFailed
3258 };
3359
60+ /* *
61+ * @brief Durable representation of a single sync operation.
62+ *
63+ * Operation is the core unit stored in the Outbox and WAL. It is designed
64+ * to be:
65+ * - immutable in intent (what to do)
66+ * - mutable in state (status, attempts, timestamps)
67+ *
68+ * The operation lifecycle is fully driven by durable state transitions,
69+ * enabling crash-safe retries and deterministic recovery.
70+ */
3471 struct Operation
3572 {
73+ /* *
74+ * @brief Unique identifier of the operation.
75+ */
3676 std::string id;
77+
78+ /* *
79+ * @brief Logical kind or type of operation.
80+ *
81+ * Examples: "http.request", "p2p.message", "db.mutation".
82+ */
3783 std::string kind;
84+
85+ /* *
86+ * @brief Target of the operation.
87+ *
88+ * Examples: URL, peer id, resource name.
89+ */
3890 std::string target;
91+
92+ /* *
93+ * @brief Opaque payload associated with the operation.
94+ */
3995 std::string payload;
96+
97+ /* *
98+ * @brief Idempotency key used to deduplicate retries.
99+ */
40100 std::string idempotency_key;
101+
102+ /* *
103+ * @brief Creation timestamp in milliseconds.
104+ */
41105 std::int64_t created_at_ms{0 };
106+
107+ /* *
108+ * @brief Last update timestamp in milliseconds.
109+ */
42110 std::int64_t updated_at_ms{0 };
111+
112+ /* *
113+ * @brief Number of delivery attempts so far.
114+ */
43115 std::uint32_t attempt{0 };
116+
117+ /* *
118+ * @brief Next time when the operation becomes eligible for retry.
119+ */
44120 std::int64_t next_retry_at_ms{0 };
121+
122+ /* *
123+ * @brief Current lifecycle status.
124+ */
45125 OperationStatus status{OperationStatus::Pending};
126+
127+ /* *
128+ * @brief Last error message, if any.
129+ */
46130 std::string last_error;
47131
132+ /* *
133+ * @brief Check whether the operation is completed.
134+ */
48135 bool is_done () const noexcept { return status == OperationStatus::Done; }
136+
137+ /* *
138+ * @brief Check whether the operation is pending.
139+ */
49140 bool is_pending () const noexcept { return status == OperationStatus::Pending; }
141+
142+ /* *
143+ * @brief Check whether the operation is failed (retryable).
144+ */
50145 bool is_failed () const noexcept { return status == OperationStatus::Failed; }
51146
147+ /* *
148+ * @brief Mark the operation as failed.
149+ *
150+ * @param err Error message.
151+ * @param now_ms Current time in milliseconds.
152+ */
52153 void fail (std::string err, std::int64_t now_ms)
53154 {
54155 last_error = std::move (err);
55156 status = OperationStatus::Failed;
56157 updated_at_ms = now_ms;
57158 }
58159
160+ /* *
161+ * @brief Mark the operation as successfully completed.
162+ *
163+ * @param now_ms Completion time in milliseconds.
164+ */
59165 void done (std::int64_t now_ms)
60166 {
61167 status = OperationStatus::Done;
@@ -66,4 +172,4 @@ namespace vix::sync
66172
67173} // namespace vix::sync
68174
69- #endif
175+ #endif // VIX_SYNC_OPERATION_HPP
0 commit comments