Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions crates/database/src/auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ LIMIT 1
sqlx::query_as(QUERY).fetch_optional(ex).await
}

pub async fn last_used_auction_id(ex: &mut PgConnection) -> Result<Option<i64>, sqlx::Error> {
const QUERY: &str = r#"
SELECT id
FROM auctions
ORDER BY id DESC
LIMIT 1
;"#;
sqlx::query_scalar(QUERY).fetch_optional(ex).await
}

pub async fn get_next_auction_id(ex: &mut PgConnection) -> Result<AuctionId, sqlx::Error> {
const QUERY: &str =
r#"SELECT nextval(pg_get_serial_sequence('auctions', 'id'))::bigint as next_id;"#;
Expand Down
3 changes: 2 additions & 1 deletion crates/e2e/src/setup/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub const ACCOUNT_ENDPOINT: &str = "/api/v1/account";
pub const AUCTION_ENDPOINT: &str = "/api/v1/auction";
pub const TRADES_ENDPOINT: &str = "/api/v1/trades";
pub const VERSION_ENDPOINT: &str = "/api/v1/version";
pub const READY_ENDPOINT: &str = "/api/v1/ready";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The /api/v1/version endpoint is still active in the router, so VERSION_ENDPOINT should not be replaced. Keep both VERSION_ENDPOINT and READY_ENDPOINT constants.

Suggested change
pub const READY_ENDPOINT: &str = "/api/v1/ready";
pub const VERSION_ENDPOINT: &str = "/api/v1/version";
pub const READY_ENDPOINT: &str = "/api/v1/ready";

pub const SOLVER_COMPETITION_ENDPOINT: &str = "/api/v2/solver_competition";
const LOCAL_DB_URL: &str = "postgresql://";

Expand Down Expand Up @@ -415,7 +416,7 @@ impl<'a> Services<'a> {

async fn wait_for_api_to_come_up() {
let is_up = || async {
reqwest::get(format!("{API_HOST}{VERSION_ENDPOINT}"))
reqwest::get(format!("{API_HOST}{READY_ENDPOINT}"))
.await
.is_ok()
};
Expand Down
2 changes: 2 additions & 0 deletions crates/orderbook/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ mod get_user_orders;
mod post_order;
mod post_quote;
mod put_app_data;
mod ready;
mod version;

const ALLOWED_METHODS: &[axum::http::Method] = &[
Expand Down Expand Up @@ -279,6 +280,7 @@ pub fn handle_all_routes(
get(get_total_surplus::get_total_surplus_handler),
),
("GET", "/api/v1/version", get(version::version_handler)),
("GET", "/api/v1/ready", get(ready::get_ready_handler)),
// Routes under `/restricted/api/` are not exposed publicly. WAF and
// infra rules restrict access to authenticated partners.
// New internal-only endpoints MUST use this prefix.
Expand Down
19 changes: 19 additions & 0 deletions crates/orderbook/src/api/ready.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use {
crate::api::AppState,
axum::{
extract::State,
http::StatusCode,
response::{IntoResponse, Response},
},
std::sync::Arc,
};

pub async fn get_ready_handler(State(state): State<Arc<AppState>>) -> Response {
match state.database_write.last_used_auction_id().await {
Ok(_maybe_id) => StatusCode::OK.into_response(),
Err(err) => {
tracing::error!(?err, "/api/v1/ready");
crate::api::internal_error_reply()
}
}
}
17 changes: 16 additions & 1 deletion crates/orderbook/src/database/auctions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use {crate::dto, anyhow::Result};
use {
crate::dto,
anyhow::{Context, Result},
};

impl super::Postgres {
pub async fn most_recent_auction(&self) -> Result<Option<dto::AuctionWithId>> {
Expand All @@ -16,4 +19,16 @@ impl super::Postgres {
let auction = dto::AuctionWithId { id, auction };
Ok(Some(auction))
}

pub async fn last_used_auction_id(&self) -> Result<Option<i64>> {
let _timer = super::Metrics::get()
.database_queries
.with_label_values(&["last_used_auction_id"])
.start_timer();

let mut ex = self.pool.acquire().await?;
database::auction::last_used_auction_id(&mut ex)
.await
.context("could not fetch last used auction_id")
}
}
Loading