-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Miguel A. Cabrera Minagorri <[email protected]>
- Loading branch information
Showing
9 changed files
with
205 additions
and
54 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use log::warn; | ||
use redis::AsyncCommands; | ||
|
||
pub enum EventExporterEnum { | ||
Redis(Redis), | ||
} | ||
|
||
/* | ||
* General event exporter that wraps many types of event exporters | ||
* We cannot use the typical Box<dyn ExporterTrait> to create a common interface because trait methods cannot be async so we just create variants and invoke theit methods | ||
*/ | ||
pub struct EventExporter { | ||
exporter: Option<EventExporterEnum>, | ||
} | ||
impl EventExporter { | ||
pub fn new_none_exporter() -> Self { | ||
Self { exporter: None } | ||
} | ||
pub async fn new_redis_exporter(redis_url: &str, channel: &str) -> Self { | ||
Self { | ||
exporter: Some(EventExporterEnum::Redis(Redis::new(redis_url, channel).await)), | ||
} | ||
} | ||
pub async fn publish(&mut self, message: &str) { | ||
if let Some(exporter) = &mut self.exporter { | ||
match exporter { | ||
EventExporterEnum::Redis(pblsr) => pblsr.publish(message).await, | ||
} | ||
} | ||
} | ||
} | ||
|
||
/* | ||
* Redis event exporter | ||
*/ | ||
pub struct Redis { | ||
connection: redis::aio::Connection, | ||
channel: String, | ||
} | ||
impl Redis { | ||
async fn new(redis_url: &str, channel: &str) -> Self { | ||
let client = redis::Client::open(redis_url).expect("Unable to create Redis client with the provided URL, please check the value of the PIPELESS_REDIS_URL env var"); | ||
let con = client.get_tokio_connection().await.expect("Failed to connect to Redis"); | ||
|
||
Self { connection: con, channel: channel.to_owned() } | ||
} | ||
|
||
async fn publish(&mut self, message: &str) { | ||
if let Err(err) = self.connection.publish::<&str, &str, i32>(&self.channel, message).await { | ||
warn!("Error publishing message to Redis: {}", err.to_string()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,4 @@ pub mod dispatcher; | |
pub mod stages; | ||
pub mod cli; | ||
pub mod kvs; | ||
pub mod event_exporters; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.