LCOV - code coverage report
Current view: top level - compute_tools/src/http/routes - terminate.rs (source / functions) Coverage Total Hit
Test: 5fe7fa8d483b39476409aee736d6d5e32728bfac.info Lines: 0.0 % 32 0
Test Date: 2025-03-12 16:10:49 Functions: 0.0 % 3 0

            Line data    Source code
       1              : use std::sync::Arc;
       2              : 
       3              : use axum::extract::State;
       4              : use axum::response::{IntoResponse, Response};
       5              : use compute_api::responses::ComputeStatus;
       6              : use http::StatusCode;
       7              : use tokio::task;
       8              : use tracing::info;
       9              : 
      10              : use crate::compute::{ComputeNode, forward_termination_signal};
      11              : use crate::http::JsonResponse;
      12              : 
      13              : /// Terminate the compute.
      14            0 : pub(in crate::http) async fn terminate(State(compute): State<Arc<ComputeNode>>) -> Response {
      15            0 :     {
      16            0 :         let mut state = compute.state.lock().unwrap();
      17            0 :         if state.status == ComputeStatus::Terminated {
      18            0 :             return StatusCode::CREATED.into_response();
      19            0 :         }
      20              : 
      21            0 :         if !matches!(state.status, ComputeStatus::Empty | ComputeStatus::Running) {
      22            0 :             return JsonResponse::invalid_status(state.status);
      23            0 :         }
      24            0 : 
      25            0 :         state.set_status(ComputeStatus::TerminationPending, &compute.state_changed);
      26            0 :         drop(state);
      27            0 :     }
      28            0 : 
      29            0 :     forward_termination_signal();
      30            0 :     info!("sent signal and notified waiters");
      31              : 
      32              :     // Spawn a blocking thread to wait for compute to become Terminated.
      33              :     // This is needed to do not block the main pool of workers and
      34              :     // be able to serve other requests while some particular request
      35              :     // is waiting for compute to finish configuration.
      36            0 :     let c = compute.clone();
      37            0 :     task::spawn_blocking(move || {
      38            0 :         let mut state = c.state.lock().unwrap();
      39            0 :         while state.status != ComputeStatus::Terminated {
      40            0 :             state = c.state_changed.wait(state).unwrap();
      41            0 :             info!(
      42            0 :                 "waiting for compute to become {}, current status: {:?}",
      43            0 :                 ComputeStatus::Terminated,
      44            0 :                 state.status
      45              :             );
      46              :         }
      47            0 :     })
      48            0 :     .await
      49            0 :     .unwrap();
      50            0 : 
      51            0 :     info!("terminated Postgres");
      52              : 
      53            0 :     StatusCode::OK.into_response()
      54            0 : }
        

Generated by: LCOV version 2.1-beta