Simplify publishing serde jobs

This commit is contained in:
Graham Christensen 2017-11-24 13:49:22 -05:00
parent 80f720fe02
commit fd3cc55b2a
No known key found for this signature in database
GPG key ID: ACA1C1D120C83D5C
3 changed files with 39 additions and 53 deletions

View file

@ -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
];
}

View file

@ -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
));
}
}

View file

@ -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<T: SimpleWorker> {
internal: T
@ -31,6 +32,23 @@ pub struct QueueMsg {
pub content: Vec<u8>,
}
pub fn publish_serde_action<T: ?Sized>(exchange: Option<String>, routing_key: Option<String>, 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;