Skip to content

Commit a64196b

Browse files
janeoajschwe
andauthored
ohos monitor: Allow configuring spawned runner types (#125)
This is to be able to host builder and runner separately by passing an argument --------- Signed-off-by: Jane <5373400+janeoa@users.noreply.github.com> Signed-off-by: Jonathan Schwender <55576758+jschwe@users.noreply.github.com> Co-authored-by: Jonathan Schwender <55576758+jschwe@users.noreply.github.com>
1 parent a46358c commit a64196b

2 files changed

Lines changed: 50 additions & 30 deletions

File tree

docker/docker_jit_monitor/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
clap = { version = "4.2", features = ["derive"] }
7+
clap = { version = "4.2", features = ["derive", "env"] }
88
anyhow = "1"
99
log = "0.4"
1010
env_logger = "0.11.4"

docker/docker_jit_monitor/src/main.rs

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{
88
use thiserror::Error;
99

1010
use anyhow::{anyhow, Context};
11-
use clap::Parser;
11+
use clap::{Parser, ValueEnum};
1212
use log::{error, info, warn};
1313

1414
use crate::github_api::{get_idle_runners, spawn_runner};
@@ -37,10 +37,27 @@ struct Args {
3737
#[clap(
3838
short,
3939
long,
40+
env = "SERVO_OHOS_CI_CONCURRENT_BUILDERS",
4041
help = "Number of concurrent builder github runners on this machine",
4142
default_value_t = 1
4243
)]
4344
concurrent_builders: u8,
45+
#[clap(
46+
long,
47+
env = "SERVO_OHOS_CI_MONITOR_MODE",
48+
value_enum,
49+
default_value_t = Mode::Both,
50+
help = "set if you want only one type of runner running"
51+
)]
52+
mode: Mode,
53+
}
54+
55+
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, ValueEnum)]
56+
enum Mode {
57+
#[default]
58+
Both,
59+
Builder,
60+
Runner,
4461
}
4562

4663
struct RunnerConfig {
@@ -152,14 +169,6 @@ enum ContainerType {
152169
}
153170

154171
impl ContainerType {
155-
/// This iterator will go from Builder -> Runner and then stop.
156-
fn iter() -> ContainerTypeIterator {
157-
ContainerTypeIterator {
158-
current: None,
159-
finished: false,
160-
}
161-
}
162-
163172
/// The number of concurrent instances we allow for this container type
164173
fn concurrent_number(&self, args: &Args) -> usize {
165174
match self {
@@ -169,36 +178,47 @@ impl ContainerType {
169178
}
170179
}
171180

181+
impl From<Mode> for ContainerTypeIterator {
182+
fn from(value: Mode) -> Self {
183+
match value {
184+
Mode::Both => ContainerTypeIterator {
185+
remaining: vec![ContainerType::Builder, ContainerType::Runner],
186+
},
187+
Mode::Builder => ContainerTypeIterator {
188+
remaining: vec![ContainerType::Builder],
189+
},
190+
Mode::Runner => ContainerTypeIterator {
191+
remaining: vec![ContainerType::Runner],
192+
},
193+
}
194+
}
195+
}
196+
172197
struct ContainerTypeIterator {
173-
current: Option<ContainerType>,
174-
finished: bool,
198+
remaining: Vec<ContainerType>,
175199
}
176200

177201
impl Iterator for ContainerTypeIterator {
178202
type Item = ContainerType;
179203

180204
fn next(&mut self) -> Option<Self::Item> {
181-
if self.finished {
182-
return None;
183-
}
184-
self.current = match self.current {
185-
None => Some(ContainerType::Builder),
186-
Some(ContainerType::Builder) => Some(ContainerType::Runner),
187-
Some(ContainerType::Runner) => {
188-
self.finished = true;
189-
None
190-
}
191-
};
192-
self.current.clone()
205+
self.remaining.pop()
193206
}
194207
}
195208

196209
#[test]
197-
fn iter_test() {
198-
let mut it = ContainerType::iter();
199-
assert_eq!(it.next(), Some(ContainerType::Builder));
200-
assert_eq!(it.next(), Some(ContainerType::Runner));
201-
assert_eq!(it.next(), None);
210+
fn single_type_mode_test() {
211+
let both_types = ContainerTypeIterator::from(Mode::Both).collect::<Vec<_>>();
212+
assert_eq!(
213+
both_types,
214+
vec![ContainerType::Runner, ContainerType::Builder]
215+
);
216+
217+
let builder_types = ContainerTypeIterator::from(Mode::Builder).collect::<Vec<_>>();
218+
assert_eq!(builder_types, vec![ContainerType::Builder]);
219+
220+
let runner_types = ContainerTypeIterator::from(Mode::Runner).collect::<Vec<_>>();
221+
assert_eq!(runner_types, vec![ContainerType::Runner]);
202222
}
203223

204224
#[derive(Debug)]
@@ -236,7 +256,7 @@ fn main() -> anyhow::Result<()> {
236256

237257
loop {
238258
let exiting = EXITING.load(Ordering::Relaxed);
239-
for container_type in ContainerType::iter() {
259+
for container_type in ContainerTypeIterator::from(args.mode) {
240260
if running_containers
241261
.iter()
242262
.filter(|container| container.container_type == container_type)

0 commit comments

Comments
 (0)