mocra_core/queue/
compensation.rs1use crate::common::model::{
2 Request, Response,
3 message::{TaskErrorEvent, TaskEvent, TaskParserEvent},
4};
5use crate::errors::Result;
6use crate::utils::logger::LogModel;
7use async_trait::async_trait;
8use std::sync::Arc;
9
10pub trait Identifiable {
12 fn get_id(&self) -> String;
13
14 fn partition_key(&self) -> String {
24 self.get_id()
25 }
26}
27
28impl Identifiable for LogModel {
29 fn get_id(&self) -> String {
30 self.request_id
31 .map(|id| id.to_string())
32 .unwrap_or_else(|| self.task_id.clone())
33 }
34}
35
36impl Identifiable for Request {
37 fn get_id(&self) -> String {
38 self.id.to_string()
39 }
40}
41
42impl Identifiable for Response {
43 fn get_id(&self) -> String {
44 self.id.to_string()
45 }
46}
47
48impl Identifiable for TaskParserEvent {
49 fn get_id(&self) -> String {
50 self.id.to_string()
51 }
52}
53
54impl Identifiable for TaskErrorEvent {
55 fn get_id(&self) -> String {
56 self.id.to_string()
57 }
58}
59
60impl Identifiable for TaskEvent {
61 fn get_id(&self) -> String {
62 self.run_id.to_string()
63 }
64
65 fn partition_key(&self) -> String {
68 self.account.clone()
69 }
70}
71
72#[async_trait]
73pub trait Compensator: Send + Sync {
74 async fn add_task(&self, topic: &str, id: &str, payload: Arc<Vec<u8>>) -> Result<()>;
76 async fn remove_task(&self, topic: &str, id: &str) -> Result<()>;
78}