Skip to content

Commit 4fca54f

Browse files
committed
Pr Feedback; code tidy up & cron function refactor
1 parent 7c0b968 commit 4fca54f

File tree

3 files changed

+54
-91
lines changed

3 files changed

+54
-91
lines changed

database/src/lib.rs

Lines changed: 13 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -823,13 +823,13 @@ pub enum BenchmarkRequestType {
823823
/// A Try commit
824824
Try {
825825
sha: String,
826-
parent_sha: Option<String>,
826+
parent_sha: String,
827827
pr: u32,
828828
},
829829
/// A Master commit
830830
Master {
831831
sha: String,
832-
parent_sha: Option<String>,
832+
parent_sha: String,
833833
pr: u32,
834834
},
835835
/// A release only has a tag
@@ -857,16 +857,8 @@ impl BenchmarkRequestType {
857857
impl fmt::Display for BenchmarkRequestType {
858858
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
859859
match self {
860-
BenchmarkRequestType::Try {
861-
sha: _,
862-
parent_sha: _,
863-
pr: _,
864-
} => write!(f, "try"),
865-
BenchmarkRequestType::Master {
866-
sha: _,
867-
parent_sha: _,
868-
pr: _,
869-
} => write!(f, "master"),
860+
BenchmarkRequestType::Try { .. } => write!(f, "try"),
861+
BenchmarkRequestType::Master { .. } => write!(f, "master"),
870862
BenchmarkRequestType::Release { tag: _ } => write!(f, "release"),
871863
}
872864
}
@@ -904,7 +896,7 @@ impl BenchmarkRequest {
904896

905897
pub fn create_try(
906898
sha: &str,
907-
parent_sha: Option<&str>,
899+
parent_sha: &str,
908900
pr: u32,
909901
created_at: DateTime<Utc>,
910902
status: BenchmarkRequestStatus,
@@ -915,7 +907,7 @@ impl BenchmarkRequest {
915907
commit_type: BenchmarkRequestType::Try {
916908
pr,
917909
sha: sha.to_string(),
918-
parent_sha: parent_sha.map(|it| it.to_string()),
910+
parent_sha: parent_sha.to_string(),
919911
},
920912
created_at,
921913
completed_at: None,
@@ -927,7 +919,7 @@ impl BenchmarkRequest {
927919

928920
pub fn create_master(
929921
sha: &str,
930-
parent_sha: Option<&str>,
922+
parent_sha: &str,
931923
pr: u32,
932924
created_at: DateTime<Utc>,
933925
status: BenchmarkRequestStatus,
@@ -938,7 +930,7 @@ impl BenchmarkRequest {
938930
commit_type: BenchmarkRequestType::Master {
939931
pr,
940932
sha: sha.to_string(),
941-
parent_sha: parent_sha.map(|it| it.to_string()),
933+
parent_sha: parent_sha.to_string(),
942934
},
943935
created_at,
944936
completed_at: None,
@@ -952,32 +944,16 @@ impl BenchmarkRequest {
952944
/// `release`
953945
pub fn tag(&self) -> &str {
954946
match &self.commit_type {
955-
BenchmarkRequestType::Try {
956-
sha,
957-
parent_sha: _,
958-
pr: _,
959-
}
960-
| BenchmarkRequestType::Master {
961-
sha,
962-
parent_sha: _,
963-
pr: _,
964-
} => sha,
947+
BenchmarkRequestType::Try { sha, .. } | BenchmarkRequestType::Master { sha, .. } => sha,
965948
BenchmarkRequestType::Release { tag } => tag,
966949
}
967950
}
968951

969952
pub fn pr(&self) -> Option<&u32> {
970953
match &self.commit_type {
971-
BenchmarkRequestType::Try {
972-
sha: _,
973-
parent_sha: _,
974-
pr,
954+
BenchmarkRequestType::Try { pr, .. } | BenchmarkRequestType::Master { pr, .. } => {
955+
Some(pr)
975956
}
976-
| BenchmarkRequestType::Master {
977-
sha: _,
978-
parent_sha: _,
979-
pr,
980-
} => Some(pr),
981957
BenchmarkRequestType::Release { tag: _ } => None,
982958
}
983959
}
@@ -988,19 +964,8 @@ impl BenchmarkRequest {
988964

989965
pub fn parent_sha(&self) -> Option<&str> {
990966
match &self.commit_type {
991-
BenchmarkRequestType::Try {
992-
sha: _,
993-
parent_sha,
994-
pr: _,
995-
}
996-
| BenchmarkRequestType::Master {
997-
sha: _,
998-
parent_sha,
999-
pr: _,
1000-
} => match parent_sha {
1001-
Some(parent_sha) => Some(parent_sha),
1002-
None => None,
1003-
},
967+
BenchmarkRequestType::Try { parent_sha, .. }
968+
| BenchmarkRequestType::Master { parent_sha, .. } => Some(parent_sha),
1004969
BenchmarkRequestType::Release { tag: _ } => None,
1005970
}
1006971
}

database/src/pool.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ mod tests {
386386
let time = chrono::DateTime::from_str("2021-09-01T00:00:00.000Z").unwrap();
387387
let master_benchmark_request = BenchmarkRequest::create_master(
388388
"a-sha-1",
389-
Some("parent-sha-1"),
389+
"parent-sha-1",
390390
42,
391391
time,
392392
BenchmarkRequestStatus::WaitingForParent,
@@ -396,7 +396,7 @@ mod tests {
396396

397397
let try_benchmark_request = BenchmarkRequest::create_try(
398398
"b-sha-2",
399-
Some("parent-sha-2"),
399+
"parent-sha-2",
400400
32,
401401
time,
402402
BenchmarkRequestStatus::WaitingForParent,

site/src/job_queue.rs

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,52 @@ use database::{BenchmarkRequest, BenchmarkRequestStatus};
66
use parking_lot::RwLock;
77
use tokio::time::{self, Duration};
88

9-
impl SiteCtxt {
10-
/// Store the latest master commits or do nothing if all of them are
11-
/// already in the database
12-
pub async fn enqueue_master_commits(&self) {
13-
let conn = self.conn().await;
14-
let master_commits = &self.get_master_commits().commits;
15-
// TODO; delete at some point in the future
16-
let cutoff: chrono::DateTime<Utc> =
17-
chrono::DateTime::from_str("2025-06-01T00:00:00.000Z").unwrap();
9+
/// Store the latest master commits or do nothing if all of them are
10+
/// already in the database
11+
async fn enqueue_master_commits(ctxt: &Arc<SiteCtxt>) {
12+
let conn = ctxt.conn().await;
13+
let master_commits = &ctxt.get_master_commits().commits;
14+
// TODO; delete at some point in the future
15+
let cutoff: chrono::DateTime<Utc> =
16+
chrono::DateTime::from_str("2025-06-01T00:00:00.000Z").unwrap();
1817

19-
for master_commit in master_commits {
20-
// We don't want to add masses of obsolete data
21-
if master_commit.time >= cutoff {
22-
let pr = master_commit.pr.unwrap_or(0);
23-
let benchmark = BenchmarkRequest::create_master(
24-
&master_commit.sha,
25-
Some(&master_commit.parent_sha),
26-
pr,
27-
master_commit.time,
28-
BenchmarkRequestStatus::WaitingForParent,
29-
"",
30-
"",
31-
);
32-
conn.insert_benchmark_request(&benchmark).await;
33-
}
18+
for master_commit in master_commits {
19+
// We don't want to add masses of obsolete data
20+
if master_commit.time >= cutoff {
21+
let pr = master_commit.pr.unwrap_or(0);
22+
let benchmark = BenchmarkRequest::create_master(
23+
&master_commit.sha,
24+
&master_commit.parent_sha,
25+
pr,
26+
master_commit.time,
27+
BenchmarkRequestStatus::WaitingForParent,
28+
"",
29+
"",
30+
);
31+
conn.insert_benchmark_request(&benchmark).await;
3432
}
3533
}
3634
}
3735

38-
/// Inserts and manages the queue at `seconds` interval
39-
async fn cron_enqueue_jobs(site_ctxt: Arc<RwLock<Option<Arc<SiteCtxt>>>>, seconds: u64) {
40-
let mut interval = time::interval(Duration::from_secs(seconds));
41-
42-
loop {
43-
let ctxt = site_ctxt.clone();
44-
if let Some(ctxt_clone) = {
45-
let guard = ctxt.read();
46-
guard.as_ref().cloned()
47-
} {
48-
ctxt_clone.enqueue_master_commits().await;
49-
}
50-
51-
interval.tick().await;
52-
println!("Cron job executed at: {:?}", std::time::SystemTime::now());
53-
}
36+
/// For queueing jobs, add the jobs you want to queue to this function
37+
async fn cron_enqueue_jobs(site_ctxt: &Arc<SiteCtxt>) {
38+
// Put the master commits into the `benchmark_requests` queue
39+
enqueue_master_commits(&site_ctxt).await;
5440
}
5541

5642
/// Entry point for the cron
5743
pub async fn cron_main(site_ctxt: Arc<RwLock<Option<Arc<SiteCtxt>>>>, seconds: u64) {
58-
cron_enqueue_jobs(site_ctxt, seconds).await;
44+
let ctxt = site_ctxt.clone();
45+
let mut interval = time::interval(Duration::from_secs(seconds));
46+
47+
if let Some(ctxt_clone) = {
48+
let guard = ctxt.read();
49+
guard.as_ref().cloned()
50+
} {
51+
loop {
52+
cron_enqueue_jobs(&ctxt_clone).await;
53+
interval.tick().await;
54+
println!("Cron job executed at: {:?}", std::time::SystemTime::now());
55+
}
56+
}
5957
}

0 commit comments

Comments
 (0)