Skip to content

Commit

Permalink
enh: SqliteWorker unhealthy until 1st heartbeat (#8310)
Browse files Browse the repository at this point in the history
Co-authored-by: Henry Fontanier <[email protected]>
  • Loading branch information
fontanierh and Henry Fontanier authored Oct 29, 2024
1 parent 9f1d851 commit 27d861f
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions core/bin/sqlite_worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ struct WorkerState {

registry: Arc<Mutex<HashMap<String, DatabaseEntry>>>,
is_shutting_down: Arc<AtomicBool>,
first_heartbeat_success: Arc<AtomicBool>,
}

impl WorkerState {
Expand All @@ -75,6 +76,7 @@ impl WorkerState {
// TODO: store an instant of the last access for each DB.
registry: Arc::new(Mutex::new(HashMap::new())),
is_shutting_down: Arc::new(AtomicBool::new(false)),
first_heartbeat_success: Arc::new(AtomicBool::new(false)),
}
}

Expand Down Expand Up @@ -110,7 +112,13 @@ impl WorkerState {
}

async fn heartbeat(&self) -> Result<()> {
self._core_request("POST").await
match self._core_request("POST").await {
Ok(response) => {
self.first_heartbeat_success.store(true, Ordering::SeqCst);
Ok(response)
}
Err(e) => Err(e),
}
}

async fn shutdown(&self) -> Result<()> {
Expand Down Expand Up @@ -159,8 +167,12 @@ impl WorkerState {

/// Index
async fn index() -> &'static str {
"sqlite_worker server ready"
async fn index(State(state): State<Arc<WorkerState>>) -> Result<&'static str, StatusCode> {
if state.first_heartbeat_success.load(Ordering::SeqCst) {
Ok("sqlite_worker server ready")
} else {
Err(StatusCode::SERVICE_UNAVAILABLE)
}
}

// Databases
Expand Down Expand Up @@ -323,7 +335,9 @@ fn main() {
)
.with_state(state.clone());

let health_check_router = Router::new().route("/", get(index));
let health_check_router = Router::new()
.route("/", get(index))
.with_state(state.clone());

let app = Router::new().merge(router).merge(health_check_router);

Expand Down

0 comments on commit 27d861f

Please sign in to comment.