feat: with String EventInfo

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2023-10-05 22:09:56 +02:00
parent bc28451f8d
commit 4a88d2fecd
13 changed files with 89 additions and 131 deletions

View File

@@ -93,9 +93,9 @@ impl Node {
impl crunch::traits::Event for $(message) {
fn event_info() -> ::crunch::traits::EventInfo {
::crunch::traits::EventInfo {
domain: "my-domain",
entity_type: "my-entity-type",
event_name: "my-event-name",
domain: "my-domain".into(),
entity_type: "my-entity-type".into(),
event_name: "my-event-name".into(),
}
}
}

View File

@@ -1,12 +1,15 @@
extern crate capnpc;
fn main() {
capnpc::CompilerCommand::new()
.output_path("src/")
.src_prefix("schemas/")
.file("schemas/envelope.capnp")
.run()
.unwrap();
#[cfg(feature = "capnp")]
{
extern crate capnpc;
capnpc::CompilerCommand::new()
.output_path("src/")
.src_prefix("schemas/")
.file("schemas/envelope.capnp")
.run()
.unwrap();
}
std::fs::create_dir_all("src/generated").unwrap();
let mut config = prost_build::Config::default();

View File

@@ -33,10 +33,11 @@ pub struct InMemoryPersistence {
#[async_trait]
impl crunch_traits::Persistence for InMemoryPersistence {
async fn insert(&self, event_info: &EventInfo, content: Vec<u8>) -> anyhow::Result<()> {
let msg = crunch_envelope::proto::wrap(event_info.domain, event_info.entity_type, &content);
let msg =
crunch_envelope::proto::wrap(&event_info.domain, &event_info.entity_type, &content);
let msg = Msg {
id: uuid::Uuid::new_v4().to_string(),
info: *event_info,
info: event_info.to_owned(),
msg,
state: MsgState::Pending,
};
@@ -71,7 +72,7 @@ impl crunch_traits::Persistence for InMemoryPersistence {
let (content, _) = crunch_envelope::proto::unwrap(event.msg.as_slice())
.map_err(|e| PersistenceError::GetErr(anyhow::anyhow!(e)))?;
Ok(Some((event.info, content)))
Ok(Some((event.info.to_owned(), content)))
}
async fn update_published(&self, event_id: &str) -> Result<(), PersistenceError> {

View File

@@ -64,7 +64,7 @@ impl Transport for InMemoryTransport {
.expect("transport to be available, as we just created it");
sender
.send(TransportEnvelope {
info: *event_info,
info: event_info.to_owned(),
content,
})
.map_err(|e| anyhow::anyhow!(e.to_string()))

View File

@@ -18,4 +18,5 @@ uuid.workspace = true
sqlx.workspace = true
serde.workspace = true
serde_json.workspace = true
tokio-stream = {workspace = true, features = ["sync"]}
tokio-stream = {workspace = true, features = ["sync"]}
chrono.workspace = true

View File

@@ -36,13 +36,15 @@ struct InsertResp {
#[derive(Clone, Serialize, Deserialize)]
struct PgEventInfo {
domain: &'static str,
entity_type: &'static str,
event_name: &'static str,
domain: String,
entity_type: String,
event_name: String,
}
impl From<&EventInfo> for PgEventInfo {
fn from(value: &EventInfo) -> Self {
let value = value.to_owned();
Self {
domain: value.domain,
entity_type: value.entity_type,
@@ -51,6 +53,26 @@ impl From<&EventInfo> for PgEventInfo {
}
}
impl From<PgEventInfo> for EventInfo {
fn from(value: PgEventInfo) -> Self {
EventInfo {
domain: value.domain,
entity_type: value.entity_type,
event_name: value.event_name,
}
}
}
#[allow(dead_code)]
#[derive(sqlx::FromRow)]
struct OutboxEvent {
id: Uuid,
metadata: Json<PgEventInfo>,
content: Vec<u8>,
inserted_time: chrono::DateTime<chrono::Utc>,
state: String,
}
#[async_trait]
impl crunch_traits::Persistence for PostgresPersistence {
// FIXME: Need some sort of concurrency control mechanism. If the insert fails or is done twice, then that user may receive multiple requests.
@@ -93,15 +115,30 @@ FOR UPDATE;
.map_err(|e| anyhow::anyhow!(e))
.map_err(PersistenceError::AnyErr)?;
let id = match resp {
Some(InsertResp { id }) => Some(id.to_string()),
None => None,
};
let id = resp.map(|InsertResp { id }| id.to_string());
Ok(id.map(|id| (id, Box::new(PostgresTx {}) as crunch_traits::DynTx)))
}
async fn get(&self, event_id: &str) -> Result<Option<(EventInfo, Vec<u8>)>, PersistenceError> {
todo!()
let event = sqlx::query_as::<_, OutboxEvent>("SELECT * from outbox where id = $1")
.bind(
Uuid::parse_str(event_id)
.map_err(|e| anyhow::anyhow!(e))
.map_err(PersistenceError::GetErr)?,
)
.fetch_optional(&self.pool)
.await
.map_err(|e| anyhow::anyhow!(e))
.map_err(PersistenceError::GetErr)?;
match event {
Some(event) => {
let metadata = event.metadata.to_owned();
Ok(Some((EventInfo::from(metadata.0), event.content)))
}
None => Ok(None),
}
}
async fn update_published(&self, event_id: &str) -> Result<(), PersistenceError> {
todo!()

View File

@@ -25,11 +25,11 @@ pub trait Deserializer {
Self: Sized;
}
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone)]
pub struct EventInfo {
pub domain: &'static str,
pub entity_type: &'static str,
pub event_name: &'static str,
pub domain: String,
pub entity_type: String,
pub event_name: String,
}
impl Display for EventInfo {

View File

@@ -26,9 +26,9 @@ impl crunch::traits::Deserializer for SomeEvent {
impl crunch::traits::Event for SomeEvent {
fn event_info() -> crunch::traits::EventInfo {
crunch::traits::EventInfo {
domain: "some-domain",
entity_type: "some-entity",
event_name: "some-event",
domain: "some-domain".into(),
entity_type: "some-entity".into(),
event_name: "some-event".into(),
}
}
}

View File

@@ -26,9 +26,9 @@ impl crunch::traits::Deserializer for SomeEvent {
impl crunch::traits::Event for SomeEvent {
fn event_info() -> crunch::traits::EventInfo {
crunch::traits::EventInfo {
domain: "some-domain",
entity_type: "some-entity",
event_name: "some-event",
domain: "some-domain".into(),
entity_type: "some-entity".into(),
event_name: "some-event".into(),
}
}
}

View File

@@ -27,9 +27,9 @@ impl Deserializer for SomeEvent {
impl Event for SomeEvent {
fn event_info() -> EventInfo {
EventInfo {
domain: "some-domain",
entity_type: "some-entity",
event_name: "some-event",
domain: "some-domain".into(),
entity_type: "some-entity".into(),
event_name: "some-event".into(),
}
}
}