-
Notifications
You must be signed in to change notification settings - Fork 152
persist current auction in background task #3861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -97,18 +97,30 @@ impl Postgres { | |
| }) | ||
| } | ||
|
|
||
| pub async fn get_next_auction_id(&self) -> Result<dto::AuctionId> { | ||
| let _timer = super::Metrics::get() | ||
| .database_queries | ||
| .with_label_values(&["get_next_auction_id"]) | ||
| .start_timer(); | ||
|
|
||
| let mut ex = self.pool.acquire().await?; | ||
| let id = database::auction::get_next_auction_id(&mut ex).await?; | ||
| Ok(id) | ||
| } | ||
|
|
||
| pub async fn replace_current_auction( | ||
| &self, | ||
| id: dto::AuctionId, | ||
| auction: &dto::RawAuctionData, | ||
| ) -> Result<dto::AuctionId> { | ||
| ) -> Result<()> { | ||
| let _timer = super::Metrics::get() | ||
| .database_queries | ||
| .with_label_values(&["replace_current_auction"]) | ||
| .with_label_values(&["insert_auction_with_id"]) | ||
| .start_timer(); | ||
|
|
||
| let data = serde_json::to_value(auction)?; | ||
| let mut ex = self.pool.acquire().await?; | ||
| let id = database::auction::replace_auction(&mut ex, &data).await?; | ||
| Ok(id) | ||
| database::auction::insert_auction_with_id(&mut ex, id, &data).await?; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To supplement my point, I'd have to dive up to here to understand that |
||
| Ok(()) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -63,27 +63,52 @@ impl Persistence { | |
| LeaderLock::new(self.postgres.pool.clone(), key, Duration::from_millis(200)) | ||
| } | ||
|
|
||
| /// There is always only one `current` auction. | ||
| /// | ||
| /// This method replaces the current auction with the given one. | ||
| /// | ||
| /// If the given auction is successfully saved, it is also archived. | ||
| pub async fn replace_current_auction( | ||
| &self, | ||
| auction: &domain::RawAuctionData, | ||
| ) -> Result<domain::auction::Id, DatabaseError> { | ||
| let _timer = observe::metrics::metrics() | ||
| .on_auction_overhead_start("autopilot", "replace_auction_in_db"); | ||
| let auction = dto::auction::from_domain(auction.clone()); | ||
| /// Fetches the ID that should be used for the next auction. | ||
| pub async fn get_next_auction_id(&self) -> Result<domain::auction::Id, DatabaseError> { | ||
| let _timer = Metrics::get() | ||
| .database_queries | ||
| .with_label_values(&["get_next_auction_id"]) | ||
| .start_timer(); | ||
| self.postgres | ||
| .replace_current_auction(&auction) | ||
| .get_next_auction_id() | ||
| .await | ||
| .inspect(|&id| { | ||
| self.archive_auction(dto::auction::Auction { id, auction }); | ||
| }) | ||
| .map_err(DatabaseError) | ||
| } | ||
|
|
||
| /// Spawns a background task that replaces the current auction in the DB | ||
| /// with the new one. | ||
| pub fn replace_current_auction_in_db( | ||
| &self, | ||
| id: domain::auction::Id, | ||
| auction: &domain::RawAuctionData, | ||
|
Comment on lines
+82
to
+83
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the variables could have better names, it's kind of hard to tell which is which by just looking at it. Is the ID the new one, or is it contained in AuctionData? |
||
| ) { | ||
| let auction_dto = dto::auction::from_domain(auction.clone()); | ||
| let db = self.postgres.clone(); | ||
| tokio::task::spawn(async move { | ||
| let _ = db | ||
| .replace_current_auction(id, &auction_dto) | ||
| .await | ||
| .inspect_err(|err| tracing::warn!(?err, "failed to replace auction in DB")); | ||
| }); | ||
| } | ||
|
|
||
| /// Spawns a background task that uploads the auction to S3. | ||
| pub fn upload_auction_to_s3(&self, id: domain::auction::Id, auction: &domain::RawAuctionData) { | ||
| if auction.orders.is_empty() { | ||
| return; | ||
| } | ||
| let Some(s3) = self.s3.clone() else { | ||
| return; | ||
| }; | ||
| let auction_dto = dto::auction::from_domain(auction.clone()); | ||
| tokio::task::spawn(async move { | ||
| match s3.upload(id.to_string(), &auction_dto).await { | ||
| Ok(key) => tracing::info!(?key, "uploaded auction to s3"), | ||
| Err(err) => tracing::warn!(?err, "failed to upload auction to s3"), | ||
|
Comment on lines
+106
to
+107
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add the auction id to the logs too? I'm not sure the error has it |
||
| } | ||
| }); | ||
| } | ||
|
|
||
| /// Finds solvable orders based on the order's min validity period. | ||
| pub async fn all_solvable_orders( | ||
| &self, | ||
|
|
@@ -95,35 +120,6 @@ impl Persistence { | |
| .context("failed to fetch all solvable orders") | ||
| } | ||
|
|
||
| /// Saves the given auction to storage for debugging purposes. | ||
| /// | ||
| /// There is no intention to retrieve this data programmatically. | ||
| fn archive_auction(&self, instance: dto::auction::Auction) { | ||
| let Some(uploader) = self.s3.clone() else { | ||
| return; | ||
| }; | ||
| if instance.auction.orders.is_empty() { | ||
| tracing::info!("skip upload of empty auction"); | ||
| return; | ||
| } | ||
| tokio::spawn( | ||
| async move { | ||
| match uploader | ||
| .upload(instance.id.to_string(), &instance.auction) | ||
| .await | ||
| { | ||
| Ok(key) => { | ||
| tracing::info!(?key, "uploaded auction to s3"); | ||
| } | ||
| Err(err) => { | ||
| tracing::warn!(?err, "failed to upload auction to s3"); | ||
| } | ||
| } | ||
| } | ||
| .instrument(tracing::Span::current()), | ||
| ); | ||
| } | ||
|
|
||
| /// Saves the competition data to the DB | ||
| pub async fn save_competition( | ||
| &self, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.