diff --git a/ofborg/src/message/buildjob.rs b/ofborg/src/message/buildjob.rs index 2ba10fb..522aadc 100644 --- a/ofborg/src/message/buildjob.rs +++ b/ofborg/src/message/buildjob.rs @@ -48,14 +48,11 @@ impl Actions { }; return vec![ - worker::Action::Publish(worker::QueueMsg{ - exchange: Some("build-results".to_owned()), - routing_key: None, - mandatory: true, - immediate: false, - properties: Some(props), - content: serde_json::to_string(&msg).unwrap().into_bytes() - }), + worker::publish_serde_action( + Some("build-results".to_owned()), + None, + &msg + ), worker::Action::Ack ]; } @@ -69,21 +66,12 @@ impl Actions { success: success }; - let props = protocol::basic::BasicProperties { - content_type: Some("application/json".to_owned()), - ..Default::default() - }; - - return vec![ - worker::Action::Publish(worker::QueueMsg{ - exchange: Some("build-results".to_owned()), - routing_key: None, - mandatory: true, - immediate: false, - properties: Some(props), - content: serde_json::to_string(&msg).unwrap().into_bytes() - }), + worker::publish_serde_action( + Some("build-results".to_owned()), + None, + &msg + ), worker::Action::Ack ]; } diff --git a/ofborg/src/tasks/githubcommentfilter.rs b/ofborg/src/tasks/githubcommentfilter.rs index 2eb97cf..4348a84 100644 --- a/ofborg/src/tasks/githubcommentfilter.rs +++ b/ofborg/src/tasks/githubcommentfilter.rs @@ -107,21 +107,11 @@ impl worker::SimpleWorker for GitHubCommentWorker { attrs: attrs, }; - let props = BasicProperties { - content_type: Some("application/json".to_owned()), - ..Default::default() - }; - - response.push( - worker::Action::Publish(worker::QueueMsg{ - exchange: Some("build-jobs".to_owned()), - routing_key: None, - mandatory: true, - immediate: false, - properties: Some(props), - content: serde_json::to_string(&msg).unwrap().into_bytes() - }) - ); + response.push(worker::publish_serde_action( + Some("build-jobs".to_owned()), + None, + &msg + )); } commentparser::Instruction::Eval => { let msg = massrebuildjob::MassRebuildJob{ @@ -129,21 +119,11 @@ impl worker::SimpleWorker for GitHubCommentWorker { pr: pr_msg.clone(), }; - let props = BasicProperties { - content_type: Some("application/json".to_owned()), - ..Default::default() - }; - - response.push( - worker::Action::Publish(worker::QueueMsg{ - exchange: None, - routing_key: Some("mass-rebuild-check-jobs".to_owned()), - mandatory: true, - immediate: false, - properties: Some(props), - content: serde_json::to_string(&msg).unwrap().into_bytes() - }) - ); + response.push(worker::publish_serde_action( + None, + Some("mass-rebuild-check-jobs".to_owned()), + &msg + )); } } diff --git a/ofborg/src/worker.rs b/ofborg/src/worker.rs index e8f6f0c..5f2eb61 100644 --- a/ofborg/src/worker.rs +++ b/ofborg/src/worker.rs @@ -2,7 +2,8 @@ use amqp::Basic; use amqp::{Consumer, Channel}; use amqp::protocol::basic::{Deliver,BasicProperties}; use std::marker::Send; - +use serde::Serialize; +use serde_json; pub struct Worker { internal: T @@ -31,6 +32,23 @@ pub struct QueueMsg { pub content: Vec, } +pub fn publish_serde_action(exchange: Option, routing_key: Option, msg: &T) -> Action + where + T: Serialize { + let props = BasicProperties { + content_type: Some("application/json".to_owned()), + ..Default::default() + }; + + return Action::Publish(QueueMsg{ + exchange: exchange, + routing_key: routing_key, + mandatory: true, + immediate: false, + properties: Some(props), + content: serde_json::to_string(&msg).unwrap().into_bytes() + }); +} pub trait SimpleWorker { type J;