Merge pull request #491 from LnL7/lapin-evaluation-filter

lapin evaluation filter
This commit is contained in:
Daiderd Jordan 2020-05-21 23:37:00 +02:00 committed by GitHub
commit b7a8808fc3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 76 deletions

View file

@ -1,26 +1,26 @@
use amqp::Basic;
use ofborg::config;
use ofborg::easyamqp::{self, ChannelExt, ConsumerExt};
use ofborg::tasks;
use ofborg::worker;
use std::env; use std::env;
use std::error::Error;
use async_std::task;
use tracing::info; use tracing::info;
fn main() { use ofborg::config;
let cfg = config::load(env::args().nth(1).unwrap().as_ref()); use ofborg::easyamqp::{self, ChannelExt, ConsumerExt};
use ofborg::easylapin;
use ofborg::tasks;
fn main() -> Result<(), Box<dyn Error>> {
ofborg::setup_log(); ofborg::setup_log();
info!("Hello, world!"); let arg = env::args()
.nth(1)
.expect("usage: evaluation-filter <config>");
let cfg = config::load(arg.as_ref());
let mut session = easyamqp::session_from_config(&cfg.rabbitmq).unwrap(); let conn = easylapin::from_config(&cfg.rabbitmq)?;
info!("Connected to rabbitmq"); let mut chan = task::block_on(conn.create_channel())?;
let mut channel = session.open_channel(1).unwrap(); chan.declare_exchange(easyamqp::ExchangeConfig {
channel
.declare_exchange(easyamqp::ExchangeConfig {
exchange: "github-events".to_owned(), exchange: "github-events".to_owned(),
exchange_type: easyamqp::ExchangeType::Topic, exchange_type: easyamqp::ExchangeType::Topic,
passive: false, passive: false,
@ -28,63 +28,50 @@ fn main() {
auto_delete: false, auto_delete: false,
no_wait: false, no_wait: false,
internal: false, internal: false,
}) })?;
.unwrap();
channel chan.declare_queue(easyamqp::QueueConfig {
.declare_queue(easyamqp::QueueConfig {
queue: "mass-rebuild-check-jobs".to_owned(), queue: "mass-rebuild-check-jobs".to_owned(),
passive: false, passive: false,
durable: true, durable: true,
exclusive: false, exclusive: false,
auto_delete: false, auto_delete: false,
no_wait: false, no_wait: false,
}) })?;
.unwrap();
channel let queue_name = String::from("mass-rebuild-check-jobs");
.declare_queue(easyamqp::QueueConfig { chan.declare_queue(easyamqp::QueueConfig {
queue: "mass-rebuild-check-inputs".to_owned(), queue: queue_name.clone(),
passive: false, passive: false,
durable: true, durable: true,
exclusive: false, exclusive: false,
auto_delete: false, auto_delete: false,
no_wait: false, no_wait: false,
}) })?;
.unwrap();
channel chan.bind_queue(easyamqp::BindQueueConfig {
.bind_queue(easyamqp::BindQueueConfig { queue: queue_name.clone(),
queue: "mass-rebuild-check-inputs".to_owned(),
exchange: "github-events".to_owned(), exchange: "github-events".to_owned(),
routing_key: Some("pull_request.nixos/nixpkgs".to_owned()), routing_key: Some("pull_request.nixos/nixpkgs".to_owned()),
no_wait: false, no_wait: false,
}) })?;
.unwrap();
channel.basic_prefetch(1).unwrap(); let handle = chan.consume(
let mut channel = channel tasks::evaluationfilter::EvaluationFilterWorker::new(cfg.acl()),
.consume(
worker::new(tasks::evaluationfilter::EvaluationFilterWorker::new(
cfg.acl(),
)),
easyamqp::ConsumeConfig { easyamqp::ConsumeConfig {
queue: "mass-rebuild-check-inputs".to_owned(), queue: queue_name.clone(),
consumer_tag: format!("{}-evaluation-filter", cfg.whoami()), consumer_tag: format!("{}-evaluation-filter", cfg.whoami()),
no_local: false, no_local: false,
no_ack: false, no_ack: false,
no_wait: false, no_wait: false,
exclusive: false, exclusive: false,
}, },
) )?;
.unwrap();
channel.start_consuming(); info!("Fetching jobs from {}", &queue_name);
task::block_on(handle);
info!("Finished consuming?"); drop(conn); // Close connection.
channel.close(200, "Bye").unwrap();
info!("Closed the channel");
session.close(200, "Good Bye");
info!("Closed the session... EOF"); info!("Closed the session... EOF");
Ok(())
} }

View file

@ -3,7 +3,7 @@ use crate::ghevent;
use crate::message::{evaluationjob, Pr, Repo}; use crate::message::{evaluationjob, Pr, Repo};
use crate::worker; use crate::worker;
use tracing::info; use tracing::{debug_span, info};
pub struct EvaluationFilterWorker { pub struct EvaluationFilterWorker {
acl: acl::ACL, acl: acl::ACL,
@ -30,6 +30,9 @@ impl worker::SimpleWorker for EvaluationFilterWorker {
} }
fn consumer(&mut self, job: &ghevent::PullRequestEvent) -> worker::Actions { fn consumer(&mut self, job: &ghevent::PullRequestEvent) -> worker::Actions {
let span = debug_span!("job", pr = ?job.number);
let _enter = span.enter();
if !self.acl.is_repo_eligible(&job.repository.full_name) { if !self.acl.is_repo_eligible(&job.repository.full_name) {
info!("Repo not authorized ({})", job.repository.full_name); info!("Repo not authorized ({})", job.repository.full_name);
return vec![worker::Action::Ack]; return vec![worker::Action::Ack];