Skip to content

Commit

Permalink
update: limit_to_one on get_jobs_by_status
Browse files Browse the repository at this point in the history
  • Loading branch information
heemankv committed Jul 23, 2024
1 parent fbd721d commit ce09eeb
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion crates/orchestrator/src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub trait Database: Send + Sync {
) -> Result<Vec<JobItem>>;

// TODO: can be extendible to support multiple status.
async fn get_jobs_by_status(&self, status: JobStatus) -> Result<Vec<JobItem>>;
async fn get_jobs_by_status(&self, status: JobStatus, limit_to_one: bool) -> Result<Vec<JobItem>>;
}

pub trait DatabaseConfig {
Expand Down
10 changes: 7 additions & 3 deletions crates/orchestrator/src/database/mongodb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use async_trait::async_trait;
use color_eyre::eyre::eyre;
use color_eyre::Result;
use mongodb::bson::{Bson, Document};
use mongodb::options::{FindOneOptions, UpdateOptions};
use mongodb::options::{FindOneOptions, FindOptions, UpdateOptions};
use mongodb::{
bson,
bson::doc,
Expand Down Expand Up @@ -291,14 +291,18 @@ impl Database for MongoDb {
Ok(results)
}

async fn get_jobs_by_status(&self, job_status: JobStatus) -> Result<Vec<JobItem>> {
async fn get_jobs_by_status(&self, job_status: JobStatus, limit_to_one: bool) -> Result<Vec<JobItem>> {
let filter = doc! {
"job_status": bson::to_bson(&job_status)?
};
let mut find_options = None;
if limit_to_one {
find_options = Some(FindOptions::builder().limit(Some(1)).build());
}

let mut jobs = self
.get_job_collection()
.find(filter, None)
.find(filter, find_options)
.await
.expect("Failed to fetch jobs by given job type and status");

Expand Down
2 changes: 1 addition & 1 deletion crates/orchestrator/src/workers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub trait Worker: Send + Sync {
async fn is_worker_enabled(&self) -> Result<bool, Box<dyn Error>> {
let config = config().await;

let failed_da_jobs = config.database().get_jobs_by_status(JobStatus::VerificationFailed).await?;
let failed_da_jobs = config.database().get_jobs_by_status(JobStatus::VerificationFailed, true).await?;

if !failed_da_jobs.is_empty() {
return Ok(false);
Expand Down
2 changes: 1 addition & 1 deletion crates/orchestrator/src/workers/snos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Worker for SnosWorker {
let latest_block_number = provider.block_number().await?;
let latest_block_processed_data = config
.database()
.get_latest_job_by_type(JobType::SnosRun)
.get_last_successful_job_by_type(JobType::SnosRun)
.await
.unwrap()
.map(|item| item.internal_id)
Expand Down

0 comments on commit ce09eeb

Please sign in to comment.