Line data Source code
1 : use std::sync::Arc;
2 : use std::thread;
3 :
4 : use tracing::{error, info, instrument};
5 :
6 : use compute_api::responses::ComputeStatus;
7 :
8 : use crate::compute::ComputeNode;
9 :
10 0 : #[instrument(skip_all)]
11 : fn configurator_main_loop(compute: &Arc<ComputeNode>) {
12 : info!("waiting for reconfiguration requests");
13 : loop {
14 : let state = compute.state.lock().unwrap();
15 : let mut state = compute.state_changed.wait(state).unwrap();
16 :
17 : if state.status == ComputeStatus::ConfigurationPending {
18 : info!("got configuration request");
19 : state.status = ComputeStatus::Configuration;
20 : compute.state_changed.notify_all();
21 : drop(state);
22 :
23 : let mut new_status = ComputeStatus::Failed;
24 : if let Err(e) = compute.reconfigure() {
25 : error!("could not configure compute node: {}", e);
26 : } else {
27 : new_status = ComputeStatus::Running;
28 : info!("compute node configured");
29 : }
30 :
31 : // XXX: used to test that API is blocking
32 : // std::thread::sleep(std::time::Duration::from_millis(10000));
33 :
34 : compute.set_status(new_status);
35 : } else if state.status == ComputeStatus::Failed {
36 : info!("compute node is now in Failed state, exiting");
37 : break;
38 : } else {
39 : info!("woken up for compute status: {:?}, sleeping", state.status);
40 : }
41 : }
42 : }
43 :
44 0 : pub fn launch_configurator(compute: &Arc<ComputeNode>) -> thread::JoinHandle<()> {
45 0 : let compute = Arc::clone(compute);
46 0 :
47 0 : thread::Builder::new()
48 0 : .name("compute-configurator".into())
49 0 : .spawn(move || {
50 0 : configurator_main_loop(&compute);
51 0 : info!("configurator thread is exited");
52 0 : })
53 0 : .expect("cannot launch configurator thread")
54 0 : }
|