mirror of
https://github.com/meilisearch/meilisearch.git
synced 2024-11-30 09:04:59 +08:00
Merge #1026
1026: refactor /health r=LegendreM a=frbimo Fixes: #940 Testing: `cargo test` and `cargo build --release` passed Co-authored-by: frbimo <fr.bimo@gmail.com>
This commit is contained in:
commit
372680e2ab
@ -1,47 +1,13 @@
|
|||||||
|
use actix_web::get;
|
||||||
use actix_web::{web, HttpResponse};
|
use actix_web::{web, HttpResponse};
|
||||||
use actix_web::{get, put};
|
|
||||||
use serde::Deserialize;
|
|
||||||
|
|
||||||
use crate::error::{Error, ResponseError};
|
use crate::error::ResponseError;
|
||||||
use crate::helpers::Authentication;
|
|
||||||
use crate::Data;
|
|
||||||
|
|
||||||
pub fn services(cfg: &mut web::ServiceConfig) {
|
pub fn services(cfg: &mut web::ServiceConfig) {
|
||||||
cfg.service(get_health).service(change_healthyness);
|
cfg.service(get_health);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/health")]
|
#[get("/health")]
|
||||||
async fn get_health(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> {
|
async fn get_health() -> Result<HttpResponse, ResponseError> {
|
||||||
let reader = data.db.main_read_txn()?;
|
Ok(HttpResponse::NoContent().finish())
|
||||||
if let Ok(Some(_)) = data.db.get_health(&reader) {
|
|
||||||
return Err(Error::Maintenance.into());
|
|
||||||
}
|
|
||||||
Ok(HttpResponse::Ok().finish())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn set_healthy(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> {
|
|
||||||
data.db.main_write(|w| data.db.set_healthy(w))?;
|
|
||||||
Ok(HttpResponse::Ok().finish())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn set_unhealthy(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> {
|
|
||||||
data.db.main_write(|w| data.db.set_unhealthy(w))?;
|
|
||||||
Ok(HttpResponse::Ok().finish())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize, Clone)]
|
|
||||||
struct HealthBody {
|
|
||||||
health: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[put("/health", wrap = "Authentication::Private")]
|
|
||||||
async fn change_healthyness(
|
|
||||||
data: web::Data<Data>,
|
|
||||||
body: web::Json<HealthBody>,
|
|
||||||
) -> Result<HttpResponse, ResponseError> {
|
|
||||||
if body.health {
|
|
||||||
set_healthy(data).await
|
|
||||||
} else {
|
|
||||||
set_unhealthy(data).await
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,3 @@
|
|||||||
use serde_json::json;
|
|
||||||
use std::convert::Into;
|
|
||||||
|
|
||||||
mod common;
|
mod common;
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
@ -10,29 +7,5 @@ async fn test_healthyness() {
|
|||||||
// Check that the server is healthy
|
// Check that the server is healthy
|
||||||
|
|
||||||
let (_response, status_code) = server.get_health().await;
|
let (_response, status_code) = server.get_health().await;
|
||||||
assert_eq!(status_code, 200);
|
assert_eq!(status_code, 204);
|
||||||
|
|
||||||
// Set the serve Unhealthy
|
|
||||||
let body = json!({
|
|
||||||
"health": false,
|
|
||||||
});
|
|
||||||
let (_response, status_code) = server.update_health(body).await;
|
|
||||||
assert_eq!(status_code, 200);
|
|
||||||
|
|
||||||
// Check that the server is unhealthy
|
|
||||||
|
|
||||||
let (_response, status_code) = server.get_health().await;
|
|
||||||
assert_eq!(status_code, 503);
|
|
||||||
|
|
||||||
// Set the server healthy
|
|
||||||
let body = json!({
|
|
||||||
"health": true,
|
|
||||||
});
|
|
||||||
let (_response, status_code) = server.update_health(body).await;
|
|
||||||
assert_eq!(status_code, 200);
|
|
||||||
|
|
||||||
// Check if the server is healthy
|
|
||||||
|
|
||||||
let (_response, status_code) = server.get_health().await;
|
|
||||||
assert_eq!(status_code, 200);
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user