summaryrefslogtreecommitdiff
path: root/ofborg/tickborg/src/bin/builder.rs
blob: 5930ae569624ee5d95af2b1fbf3d5c4d0cf97473 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use std::env;
use std::error::Error;
use std::future::Future;
use std::path::Path;
use std::pin::Pin;

use futures_util::future;
use tracing::{error, info, warn};

use tickborg::easyamqp::{self, ChannelExt, ConsumerExt};
use tickborg::easylapin;
use tickborg::{checkout, config, tasks};

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    tickborg::setup_log();

    let arg = env::args()
        .nth(1)
        .unwrap_or_else(|| panic!("usage: {} <config>", std::env::args().next().unwrap()));
    let cfg = config::load(arg.as_ref());

    let Some(builder_cfg) = config::load(arg.as_ref()).builder else {
        error!("No builder configuration found!");
        panic!();
    };

    let conn = easylapin::from_config(&builder_cfg.rabbitmq).await?;
    let mut handles: Vec<Pin<Box<dyn Future<Output = ()> + Send>>> = Vec::new();

    for system in &cfg.build.system {
        handles.push(self::create_handle(&conn, &cfg, system.to_string()).await?);
    }

    future::join_all(handles).await;

    drop(conn); // Close connection.
    info!("Closed the session... EOF");
    Ok(())
}

#[allow(clippy::type_complexity)]
async fn create_handle(
    conn: &lapin::Connection,
    cfg: &config::Config,
    system: String,
) -> Result<Pin<Box<dyn Future<Output = ()> + Send>>, Box<dyn Error>> {
    let mut chan = conn.create_channel().await?;

    let cloner = checkout::cached_cloner(Path::new(&cfg.checkout.root));
    let build_executor = cfg.build_executor();

    chan.declare_exchange(easyamqp::ExchangeConfig {
        exchange: "build-jobs".to_owned(),
        exchange_type: easyamqp::ExchangeType::Fanout,
        passive: false,
        durable: true,
        auto_delete: false,
        no_wait: false,
        internal: false,
    })
    .await?;

    let queue_name = if cfg.runner.build_all_jobs != Some(true) {
        let queue_name = format!("build-inputs-{system}");
        chan.declare_queue(easyamqp::QueueConfig {
            queue: queue_name.clone(),
            passive: false,
            durable: true,
            exclusive: false,
            auto_delete: false,
            no_wait: false,
        })
        .await?;
        queue_name
    } else {
        warn!("Building all jobs, please don't use this unless you're");
        warn!("developing and have Graham's permission!");
        let queue_name = "".to_owned();
        chan.declare_queue(easyamqp::QueueConfig {
            queue: queue_name.clone(),
            passive: false,
            durable: false,
            exclusive: true,
            auto_delete: true,
            no_wait: false,
        })
        .await?;
        queue_name
    };

    chan.bind_queue(easyamqp::BindQueueConfig {
        queue: queue_name.clone(),
        exchange: "build-jobs".to_owned(),
        routing_key: None,
        no_wait: false,
    })
    .await?;

    let handle = easylapin::NotifyChannel(chan)
        .consume(
            tasks::build::BuildWorker::new(cloner, build_executor, system, cfg.runner.identity.clone()),
            easyamqp::ConsumeConfig {
                queue: queue_name.clone(),
                consumer_tag: format!("{}-builder", cfg.whoami()),
                no_local: false,
                no_ack: false,
                no_wait: false,
                exclusive: false,
            },
        )
        .await?;

    info!("Fetching jobs from {}", &queue_name);
    Ok(handle)
}