@@ -8,7 +8,7 @@ use std::{
88use thiserror:: Error ;
99
1010use anyhow:: { anyhow, Context } ;
11- use clap:: Parser ;
11+ use clap:: { Parser , ValueEnum } ;
1212use log:: { error, info, warn} ;
1313
1414use 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
4663struct RunnerConfig {
@@ -152,14 +169,6 @@ enum ContainerType {
152169}
153170
154171impl 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+
172197struct ContainerTypeIterator {
173- current : Option < ContainerType > ,
174- finished : bool ,
198+ remaining : Vec < ContainerType > ,
175199}
176200
177201impl 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