MeiliSearch/meilisearch-http/src/routes/health.rs

74 lines
2.0 KiB
Rust
Raw Normal View History

use crate::error::ResponseError;
2019-10-31 15:00:36 +01:00
use crate::Data;
2020-04-10 19:05:05 +02:00
use actix_web as aweb;
use actix_web::{get, put, web, HttpResponse};
2019-10-31 15:00:36 +01:00
use heed::types::{Str, Unit};
use serde::Deserialize;
const UNHEALTHY_KEY: &str = "_is_unhealthy";
#[get("/health")]
2020-04-10 19:05:05 +02:00
pub async fn get_health(data: web::Data<Data>) -> aweb::Result<HttpResponse> {
let reader = data
.db
.main_read_txn()
2020-04-09 10:39:34 +02:00
.map_err(|err| ResponseError::Internal(err.to_string()))?;
2019-10-31 15:00:36 +01:00
let common_store = data.db.common_store();
2019-10-31 15:00:36 +01:00
if let Ok(Some(_)) = common_store.get::<_, Str, Unit>(&reader, UNHEALTHY_KEY) {
2020-04-09 10:39:34 +02:00
return Err(ResponseError::Maintenance.into());
2019-10-31 15:00:36 +01:00
}
Ok(HttpResponse::Ok().finish())
2019-10-31 15:00:36 +01:00
}
2020-04-10 19:05:05 +02:00
pub async fn set_healthy(data: web::Data<Data>) -> aweb::Result<HttpResponse> {
let mut writer = data
.db
.main_write_txn()
2020-04-09 10:39:34 +02:00
.map_err(|err| ResponseError::Internal(err.to_string()))?;
let common_store = data.db.common_store();
2020-04-10 19:05:05 +02:00
common_store
.delete::<_, Str>(&mut writer, UNHEALTHY_KEY)
.map_err(|e| ResponseError::Internal(e.to_string()))?;
2020-04-10 19:05:05 +02:00
writer
.commit()
2020-04-09 10:39:34 +02:00
.map_err(|err| ResponseError::Internal(err.to_string()))?;
2019-10-31 15:00:36 +01:00
Ok(HttpResponse::Ok().finish())
2019-10-31 15:00:36 +01:00
}
2020-04-10 19:05:05 +02:00
pub async fn set_unhealthy(data: web::Data<Data>) -> aweb::Result<HttpResponse> {
let mut writer = data
.db
.main_write_txn()
2020-04-09 10:39:34 +02:00
.map_err(|err| ResponseError::Internal(err.to_string()))?;
let common_store = data.db.common_store();
2020-04-10 19:05:05 +02:00
common_store
.put::<_, Str, Unit>(&mut writer, UNHEALTHY_KEY, &())
.map_err(|e| ResponseError::Internal(e.to_string()))?;
2020-04-10 19:05:05 +02:00
writer
.commit()
2020-04-09 10:39:34 +02:00
.map_err(|err| ResponseError::Internal(err.to_string()))?;
2019-10-31 15:00:36 +01:00
Ok(HttpResponse::Ok().finish())
2019-10-31 15:00:36 +01:00
}
#[derive(Deserialize, Clone)]
pub struct HealtBody {
2019-10-31 15:00:36 +01:00
health: bool,
}
#[put("/health")]
pub async fn change_healthyness(
data: web::Data<Data>,
body: web::Json<HealtBody>,
2020-04-09 10:39:34 +02:00
) -> aweb::Result<HttpResponse> {
2019-10-31 15:00:36 +01:00
if body.health {
set_healthy(data).await
2019-10-31 15:00:36 +01:00
} else {
set_unhealthy(data).await
2019-10-31 15:00:36 +01:00
}
}