Skip to content

Commit c01b313

Browse files
authored
Changed notification signature to pass all by references. (#148)
1 parent b899cfa commit c01b313

10 files changed

Lines changed: 52 additions & 41 deletions

File tree

src/notifiers/base.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ pub trait Notifier {
88
async fn prepare(&mut self) -> RustusResult<()>;
99
async fn send_message(
1010
&self,
11-
message: String,
12-
hook: Hook,
11+
message: &str,
12+
hook: &Hook,
1313
headers_map: &HeaderMap,
1414
) -> RustusResult<()>;
1515
}

src/notifiers/impls/amqp_notifier.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,12 @@ impl Notifier for AMQPNotifier {
153153
#[tracing::instrument(skip(self, message, _header_map))]
154154
async fn send_message(
155155
&self,
156-
message: String,
157-
hook: Hook,
156+
message: &str,
157+
hook: &Hook,
158158
_header_map: &HeaderMap,
159159
) -> RustusResult<()> {
160160
let chan = self.channel_pool.get().await?;
161-
let queue = self.get_queue_name(&hook);
161+
let queue = self.get_queue_name(hook);
162162
let routing_key = self.routing_key.as_ref().unwrap_or(&queue);
163163
let payload = if self.celery {
164164
format!("[[{message}], {{}}, {{}}]").as_bytes().to_vec()

src/notifiers/impls/dir_notifier.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ impl Notifier for DirNotifier {
2828
#[tracing::instrument(skip(self, message, _headers_map))]
2929
async fn send_message(
3030
&self,
31-
message: String,
32-
hook: Hook,
31+
message: &str,
32+
hook: &Hook,
3333
_headers_map: &HeaderMap,
3434
) -> RustusResult<()> {
3535
let hook_path = self.dir.join(hook.to_string());

src/notifiers/impls/file_notifier.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ impl Notifier for FileNotifier {
2727
#[tracing::instrument(err, skip(self, message, _headers_map), fields(exit_status = tracing::field::Empty))]
2828
async fn send_message(
2929
&self,
30-
message: String,
31-
hook: Hook,
30+
message: &str,
31+
hook: &Hook,
3232
_headers_map: &HeaderMap,
3333
) -> RustusResult<()> {
3434
tracing::debug!("Running command: {}", self.command.as_str());

src/notifiers/impls/http_notifier.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ impl Notifier for HttpNotifier {
3737
#[tracing::instrument(err, skip(self, message, header_map), fields(response_body = tracing::field::Empty))]
3838
async fn send_message(
3939
&self,
40-
message: String,
41-
hook: Hook,
40+
message: &str,
41+
hook: &Hook,
4242
header_map: &HeaderMap,
4343
) -> RustusResult<()> {
4444
tracing::debug!("Starting HTTP Hook.");
4545
let idempotency_key = uuid::Uuid::new_v4().to_string();
46+
let body_bytes = bytes::Bytes::copy_from_slice(message.as_bytes());
4647
let requests_vec = self.urls.iter().map(|url| {
4748
tracing::debug!("Preparing request for {}", url);
4849
let mut request = self
@@ -57,7 +58,7 @@ impl Notifier for HttpNotifier {
5758
request = request.header(item.as_str(), value.as_bytes());
5859
}
5960
}
60-
request.body(message.clone()).send()
61+
request.body(body_bytes.clone()).send()
6162
});
6263
for response in requests_vec {
6364
let real_resp = response.await?;

src/notifiers/manager.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,16 @@ impl NotificationManager {
8787
#[tracing::instrument(skip(self, hook, headers_map))]
8888
pub async fn notify_all(
8989
&self,
90-
message: String,
91-
hook: super::hooks::Hook,
90+
message: &str,
91+
hook: &super::hooks::Hook,
9292
headers_map: &HeaderMap,
9393
) -> crate::errors::RustusResult<()> {
94-
for notifier in &self.notifiers {
94+
let collect = self.notifiers.iter().map(|notifier| {
9595
notifier
96-
.send_message(message.clone(), hook, headers_map)
96+
.send_message(message, hook, headers_map)
9797
.in_current_span()
98-
.await?;
99-
}
98+
});
99+
futures::future::try_join_all(collect).await?;
100100
Ok(())
101101
}
102102
}

src/notifiers/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ impl base::Notifier for NotifierImpl {
2727
}
2828
async fn send_message(
2929
&self,
30-
message: String,
31-
hook: hooks::Hook,
30+
message: &str,
31+
hook: &hooks::Hook,
3232
headers_map: &HeaderMap,
3333
) -> crate::errors::RustusResult<()> {
3434
match self {

src/server/routes/create.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,20 @@ pub async fn handler(
141141
state
142142
.notificator
143143
.notify_all(
144-
state.config.notification_config.hooks_format.format(
145-
&uri,
146-
&method,
147-
&addr,
148-
&headers,
149-
state.config.behind_proxy,
150-
&file_info,
151-
),
152-
Hook::PreCreate,
144+
state
145+
.config
146+
.notification_config
147+
.hooks_format
148+
.format(
149+
&uri,
150+
&method,
151+
&addr,
152+
&headers,
153+
state.config.behind_proxy,
154+
&file_info,
155+
)
156+
.as_str(),
157+
&Hook::PreCreate,
153158
&headers,
154159
)
155160
.await?;
@@ -200,7 +205,7 @@ pub async fn handler(
200205
async move {
201206
moved_state
202207
.notificator
203-
.notify_all(message, post_hook, &headers)
208+
.notify_all(&message, &post_hook, &headers)
204209
.await
205210
}
206211
.in_current_span(),

src/server/routes/delete.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,20 @@ pub async fn handler(
4545
state
4646
.notificator
4747
.notify_all(
48-
state.config.notification_config.hooks_format.format(
49-
&uri,
50-
&method,
51-
&addr,
52-
&headers,
53-
state.config.behind_proxy,
54-
&file_info,
55-
),
56-
Hook::PreTerminate,
48+
state
49+
.config
50+
.notification_config
51+
.hooks_format
52+
.format(
53+
&uri,
54+
&method,
55+
&addr,
56+
&headers,
57+
state.config.behind_proxy,
58+
&file_info,
59+
)
60+
.as_str(),
61+
&Hook::PreTerminate,
5762
&headers,
5863
)
5964
.await?;
@@ -80,7 +85,7 @@ pub async fn handler(
8085
async move {
8186
state_cln
8287
.notificator
83-
.notify_all(msg, Hook::PostTerminate, &headers)
88+
.notify_all(&msg, &Hook::PostTerminate, &headers)
8489
.await
8590
}
8691
.in_current_span(),

src/server/routes/upload.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ pub async fn handler(
140140
async move {
141141
state_clone
142142
.notificator
143-
.notify_all(msg, hook, &headers_clone)
143+
.notify_all(&msg, &hook, &headers_clone)
144144
.await
145145
.ok();
146146
}

0 commit comments

Comments
 (0)