-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): implement some control APIs (#524)
* mem usage * DNS * restart * clippy * clippy * clippy * clippy
- Loading branch information
Showing
9 changed files
with
267 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use std::sync::Arc; | ||
|
||
use axum::{ | ||
body::Body, | ||
extract::{ws::Message, FromRequest, Query, Request, State, WebSocketUpgrade}, | ||
response::IntoResponse, | ||
Json, | ||
}; | ||
use http::HeaderMap; | ||
use serde::{Deserialize, Serialize}; | ||
use tracing::{debug, warn}; | ||
|
||
use crate::app::api::AppState; | ||
|
||
use super::utils::is_request_websocket; | ||
|
||
#[derive(Deserialize)] | ||
pub struct GetMemoryQuery { | ||
interval: Option<u64>, | ||
} | ||
|
||
#[derive(Serialize)] | ||
struct GetMemoryResponse { | ||
inuse: usize, | ||
oslimit: usize, | ||
} | ||
pub async fn handle( | ||
headers: HeaderMap, | ||
State(state): State<Arc<AppState>>, | ||
q: Query<GetMemoryQuery>, | ||
req: Request<Body>, | ||
) -> impl IntoResponse { | ||
if !is_request_websocket(headers) { | ||
let mgr = state.statistics_manager.clone(); | ||
let snapshot = GetMemoryResponse { | ||
inuse: mgr.memory_usage(), | ||
oslimit: 0, | ||
}; | ||
return Json(snapshot).into_response(); | ||
} | ||
|
||
let ws = match WebSocketUpgrade::from_request(req, &state).await { | ||
Ok(ws) => ws, | ||
Err(e) => { | ||
warn!("ws upgrade error: {}", e); | ||
return e.into_response(); | ||
} | ||
}; | ||
|
||
ws.on_failed_upgrade(|e| { | ||
warn!("ws upgrade error: {}", e); | ||
}) | ||
.on_upgrade(move |mut socket| async move { | ||
let interval = q.interval; | ||
|
||
let mgr = state.statistics_manager.clone(); | ||
|
||
loop { | ||
let snapshot = GetMemoryResponse { | ||
inuse: mgr.memory_usage(), | ||
oslimit: 0, | ||
}; | ||
let j = serde_json::to_vec(&snapshot).unwrap(); | ||
let body = String::from_utf8(j).unwrap(); | ||
|
||
if let Err(e) = socket.send(Message::Text(body)).await { | ||
debug!("send memory snapshot failed: {}", e); | ||
break; | ||
} | ||
|
||
tokio::time::sleep(tokio::time::Duration::from_secs( | ||
interval.unwrap_or(1), | ||
)) | ||
.await; | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#[cfg(unix)] | ||
use std::os::unix::process::CommandExt; | ||
|
||
use axum::{response::IntoResponse, Json}; | ||
use serde_json::Map; | ||
|
||
pub async fn handle() -> impl IntoResponse { | ||
match std::env::current_exe() { | ||
Ok(exec) => { | ||
let mut map = Map::new(); | ||
map.insert("status".to_owned(), "ok".into()); | ||
tokio::spawn(async move { | ||
tokio::time::sleep(std::time::Duration::from_secs(1)).await; | ||
|
||
#[cfg(unix)] | ||
{ | ||
use tracing::info; | ||
|
||
let err = std::process::Command::new(exec) | ||
.args(std::env::args().skip(1)) | ||
.envs(std::env::vars()) | ||
.exec(); | ||
info!("process restarted: {}", err); | ||
} | ||
#[cfg(windows)] | ||
{ | ||
use tracing::error; | ||
|
||
match std::process::Command::new(exec) | ||
.args(std::env::args().skip(1)) | ||
.envs(std::env::vars()) | ||
.stdin(std::process::Stdio::inherit()) | ||
.stdout(std::process::Stdio::inherit()) | ||
.stderr(std::process::Stdio::inherit()) | ||
.spawn() | ||
{ | ||
Ok(_) => { | ||
// exit the current process | ||
std::process::exit(0); | ||
} | ||
Err(e) => { | ||
error!("Failed to restart: {}", e); | ||
} | ||
} | ||
} | ||
}); | ||
Json(map).into_response() | ||
} | ||
Err(e) => { | ||
(http::StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters