This commit is contained in:
Quentin de Quelen
2020-04-09 10:39:34 +02:00
committed by qdequele
parent 6c581fb3bd
commit 5ec130e6dc
11 changed files with 122 additions and 140 deletions

View File

@ -1,5 +1,6 @@
use crate::error::ResponseError;
use actix_web::*;
use actix_web::{web, get, put, HttpResponse};
use actix_web as aweb;
use crate::Data;
use heed::types::{Str, Unit};
use serde::Deserialize;
@ -9,14 +10,14 @@ const UNHEALTHY_KEY: &str = "_is_unhealthy";
#[get("/health")]
pub async fn get_health(
data: web::Data<Data>,
) -> Result<HttpResponse> {
) -> aweb::Result<HttpResponse> {
let reader = data.db.main_read_txn()
.map_err(|_| ResponseError::CreateTransaction)?;
.map_err(|err| ResponseError::Internal(err.to_string()))?;
let common_store = data.db.common_store();
if let Ok(Some(_)) = common_store.get::<_, Str, Unit>(&reader, UNHEALTHY_KEY) {
return Err(ResponseError::Maintenance)?;
return Err(ResponseError::Maintenance.into());
}
Ok(HttpResponse::Ok().finish())
@ -24,28 +25,28 @@ pub async fn get_health(
pub async fn set_healthy(
data: web::Data<Data>,
) -> Result<HttpResponse> {
) -> aweb::Result<HttpResponse> {
let mut writer = data.db.main_write_txn()
.map_err(|_| ResponseError::CreateTransaction)?;
.map_err(|err| ResponseError::Internal(err.to_string()))?;
let common_store = data.db.common_store();
common_store.delete::<_, Str>(&mut writer, UNHEALTHY_KEY)
.map_err(|e| ResponseError::Internal(e.to_string()))?;
writer.commit()
.map_err(|_| ResponseError::CommitTransaction)?;
.map_err(|err| ResponseError::Internal(err.to_string()))?;
Ok(HttpResponse::Ok().finish())
}
pub async fn set_unhealthy(
data: web::Data<Data>,
) -> Result<HttpResponse> {
) -> aweb::Result<HttpResponse> {
let mut writer = data.db.main_write_txn()
.map_err(|_| ResponseError::CreateTransaction)?;
.map_err(|err| ResponseError::Internal(err.to_string()))?;
let common_store = data.db.common_store();
common_store.put::<_, Str, Unit>(&mut writer, UNHEALTHY_KEY, &())
.map_err(|e| ResponseError::Internal(e.to_string()))?;
writer.commit()
.map_err(|_| ResponseError::CommitTransaction)?;
.map_err(|err| ResponseError::Internal(err.to_string()))?;
Ok(HttpResponse::Ok().finish())
}
@ -59,7 +60,7 @@ pub struct HealtBody {
pub async fn change_healthyness(
data: web::Data<Data>,
body: web::Json<HealtBody>,
) -> Result<HttpResponse> {
) -> aweb::Result<HttpResponse> {
if body.health {
set_healthy(data).await
} else {