Skip to content

Commit 593fa3b

Browse files
committed
Return status information from status endpoint
The status endpoint is currently only used as a liveness probe for kubernetes deployments but returning a version and start time provides a useful sanity check of the running version without having to use the kubernetes dashboard. The dockerfile needs to be updated to include the build.rs file and set the workdir. Previously, cargo was running in the root directory and would complain about infinite file system loops in /proc/.
1 parent c6f4514 commit 593fa3b

File tree

8 files changed

+120
-5
lines changed

8 files changed

+120
-5
lines changed

Cargo.lock

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ async-graphql-axum = "7.0.13"
1515
axum = "0.7.9"
1616
axum-extra = { version = "0.9.3", features = ["typed-header"] }
1717
chrono = "0.4.39"
18-
clap = { version = "4.5.23", features = ["cargo", "derive", "env"] }
18+
clap = { version = "4.5.23", features = ["cargo", "derive", "env", "string"] }
1919
futures = "0.3.31"
2020
opentelemetry = "0.27.1"
2121
opentelemetry-otlp = "0.27.0"
@@ -38,3 +38,6 @@ httpmock = { version = "0.7.0", default-features = false }
3838
rstest = "0.23.0"
3939
serde_json = "1.0.133"
4040
tempfile = "3.14.0"
41+
42+
[build-dependencies]
43+
built = { version = "0.7.5", features = ["git2", "chrono"] }

Dockerfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ RUN rustup target add x86_64-unknown-linux-musl && \
55
apt-get install -y musl-tools musl-dev && \
66
update-ca-certificates
77

8+
WORKDIR /build
9+
810
COPY ./Cargo.toml ./Cargo.toml
911
COPY ./Cargo.lock ./Cargo.lock
12+
COPY ./build.rs ./build.rs
1013
COPY ./.env ./.env
1114
COPY ./src ./src
1215
COPY ./.sqlx ./.sqlx
@@ -20,7 +23,7 @@ LABEL org.opencontainers.image.source=https://github.com/DiamondLightSource/numt
2023
LABEL org.opencontainers.image.description="Central co-ordinator for scan numbers and file locations"
2124
LABEL org.opencontainers.image.licenses=Apache-2.0
2225

23-
COPY --from=build ./target/x86_64-unknown-linux-musl/release/numtracker /app/numtracker
26+
COPY --from=build /build/target/x86_64-unknown-linux-musl/release/numtracker /app/numtracker
2427

2528
CMD ["serve"]
2629
ENTRYPOINT ["/app/numtracker"]

build.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
fn main() {
2-
println!("cargo:rerun-if-changed=migrations");
2+
println!("cargo::rerun-if-changed=migrations");
3+
built::write_built_file().expect("Failed to write build time information");
4+
// Force the application to be rebuilt after committing to ensure build info is up to date
5+
println!("cargo::rerun-if-changed=.git/refs");
36
}

src/build_info.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//! Compile time build information provided by built
2+
3+
use chrono::Local;
4+
use serde::Serialize;
5+
6+
include!(concat!(env!("OUT_DIR"), "/built.rs"));
7+
8+
/// User friendly label for marking a build as debug or not
9+
pub const DEBUG_LABEL: &str = if DEBUG { " (debug)" } else { "" };
10+
/// User friendly label for indicating repo state
11+
pub const DIRTY_LABEL: &str = match GIT_DIRTY {
12+
Some(true) => " (+unstaged changes)",
13+
_ => "",
14+
};
15+
16+
pub fn build_info() -> String {
17+
format!(
18+
concat!("- {}{}\n", "Built: {}\n", "Commit: {}{}"),
19+
PKG_VERSION,
20+
DEBUG_LABEL,
21+
BUILT_TIME_UTC,
22+
GIT_COMMIT_HASH.unwrap_or("Unknown"),
23+
DIRTY_LABEL
24+
)
25+
}
26+
27+
#[derive(Debug, Clone, Serialize)]
28+
pub struct ServerStatus {
29+
version: String,
30+
start_time: String,
31+
build: String,
32+
}
33+
34+
impl ServerStatus {
35+
pub fn new() -> Self {
36+
Self {
37+
version: PKG_VERSION.into(),
38+
start_time: Local::now().to_rfc3339(),
39+
build: GIT_COMMIT_HASH.unwrap_or("Unknown").into(),
40+
}
41+
}
42+
}

src/cli.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ use tracing::Level;
2121
use url::Url;
2222

2323
#[derive(Debug, Parser)]
24+
#[clap(version)]
25+
#[clap(long_version = crate::build_info::build_info())]
2426
pub struct Cli {
2527
#[clap(short, long, default_value = "numtracker.db", env = "NUMTRACKER_DB")]
2628
pub(crate) db: PathBuf,

src/graphql.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@ use async_graphql_axum::{GraphQLRequest, GraphQLResponse};
3232
use auth::{AuthError, PolicyCheck};
3333
use axum::response::{Html, IntoResponse};
3434
use axum::routing::{get, post};
35-
use axum::{Extension, Router};
35+
use axum::{Extension, Json, Router};
3636
use axum_extra::headers::authorization::Bearer;
3737
use axum_extra::headers::Authorization;
3838
use axum_extra::TypedHeader;
3939
use chrono::{Datelike, Local};
4040
use tokio::net::TcpListener;
4141
use tracing::{info, instrument, trace, warn};
4242

43+
use crate::build_info::ServerStatus;
4344
use crate::cli::ServeOptions;
4445
use crate::db_service::{
4546
BeamlineConfiguration, BeamlineConfigurationUpdate, SqliteScanPathService,
@@ -54,6 +55,7 @@ use crate::template::{FieldSource, PathTemplate};
5455
mod auth;
5556

5657
pub async fn serve_graphql(db: &Path, opts: ServeOptions) {
58+
let server_status = Json(ServerStatus::new());
5759
let db = SqliteScanPathService::connect(db)
5860
.await
5961
.expect("Unable to open DB");
@@ -71,7 +73,7 @@ pub async fn serve_graphql(db: &Path, opts: ServeOptions) {
7173
let app = Router::new()
7274
// status check endpoint allows external processes to monitor status of server without
7375
// making graphql queries
74-
.route("/status", get(|| async {}))
76+
.route("/status", get(server_status))
7577
.route("/graphql", post(graphql_handler))
7678
.route("/graphiql", get(graphiql))
7779
.layer(Extension(schema));

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use std::error::Error;
1717
use cli::{Cli, Command};
1818
use tracing::debug;
1919

20+
mod build_info;
2021
mod cli;
2122
mod db_service;
2223
mod graphql;

0 commit comments

Comments
 (0)