Skip to content

Commit 2a45040

Browse files
committed
PR feedback - simpilfy requeuing, remove C-like OOP and correct queries
1 parent ecca6ff commit 2a45040

File tree

4 files changed

+318
-435
lines changed

4 files changed

+318
-435
lines changed

database/src/lib.rs

Lines changed: 57 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use intern::intern;
55
use serde::{Deserialize, Serialize};
66
use std::fmt::{self, Display, Formatter};
77
use std::hash;
8-
use std::ops::{Add, Deref, Sub};
8+
use std::ops::{Add, Sub};
99
use std::sync::Arc;
1010
use std::time::Duration;
1111

@@ -806,13 +806,24 @@ pub struct ArtifactCollection {
806806

807807
#[derive(Debug, Clone, Serialize, Deserialize)]
808808
pub enum CommitJobType {
809-
Try(u32),
810-
Master(u32),
811-
Release(String),
809+
Try { pr: u32 },
810+
Master { pr: u32 },
811+
Release { tag: String },
812+
}
813+
814+
impl CommitJobType {
815+
/// Get the name of the type as a `str`
816+
pub fn name(&self) -> &'static str {
817+
match self {
818+
CommitJobType::Try { pr: _ } => "try",
819+
CommitJobType::Master { pr: _ } => "master",
820+
CommitJobType::Release { tag: _ } => "release",
821+
}
822+
}
812823
}
813824

814825
#[derive(Debug, Clone, Serialize, Deserialize)]
815-
pub struct CommitJobEntity {
826+
pub struct CommitJob {
816827
pub sha: String,
817828
pub parent_sha: String,
818829
pub commit_time: Date,
@@ -822,113 +833,45 @@ pub struct CommitJobEntity {
822833
pub runs: Option<i32>,
823834
pub backends: Option<String>,
824835
pub job_type: CommitJobType,
836+
pub state: CommitJobState,
837+
}
838+
839+
#[derive(Debug, Clone, Serialize, Deserialize)]
840+
pub enum CommitJobState {
841+
Queued,
842+
Finished(CommitJobFinished),
843+
Failed(CommitJobFailed),
844+
InProgress(CommitJobInProgress),
825845
}
826846

827847
#[derive(Debug, Clone, Serialize, Deserialize)]
828848
pub struct CommitJobInProgress {
829-
pub commit_job: CommitJobEntity,
830849
pub machine_id: String,
831850
pub started_at: Date,
832851
}
833852

834853
#[derive(Debug, Clone, Serialize, Deserialize)]
835854
pub struct CommitJobFinished {
836-
pub commit_job: CommitJobEntity,
837855
pub machine_id: String,
838856
pub started_at: Date,
839857
pub finished_at: Date,
840858
}
841859

842860
#[derive(Debug, Clone, Serialize, Deserialize)]
843861
pub struct CommitJobFailed {
844-
pub commit_job: CommitJobEntity,
845862
pub machine_id: String,
846863
pub started_at: Date,
847864
pub finished_at: Date,
848865
}
849866

850-
#[derive(Debug, Clone, Serialize, Deserialize)]
851-
pub enum CommitJob {
852-
Queued(CommitJobEntity),
853-
InProgress(CommitJobInProgress),
854-
Finished(CommitJobFinished),
855-
Failed(CommitJobFailed),
856-
}
857-
858867
impl CommitJob {
859-
/// Returns `Some(&CommitJobEntity)` only if the job is still queued.
860-
pub fn as_queued(&self) -> Option<&CommitJobEntity> {
861-
match self {
862-
CommitJob::Queued(e) => Some(e),
863-
_ => None,
864-
}
865-
}
866-
867-
/// Returns `Some(&CommitJobInProgress)` while the job is running.
868-
pub fn as_in_progress(&self) -> Option<&CommitJobInProgress> {
869-
match self {
870-
CommitJob::InProgress(ip) => Some(ip),
871-
_ => None,
872-
}
873-
}
874-
875-
/// Returns `Some(&CommitJobFinished)` once the job is done.
876-
pub fn as_finished(&self) -> Option<&CommitJobFinished> {
877-
match self {
878-
CommitJob::Finished(fin) => Some(fin),
879-
_ => None,
880-
}
881-
}
882-
883868
/// Get the status as a string
884869
pub fn status(&self) -> &'static str {
885-
match self {
886-
CommitJob::Queued(_) => "queued",
887-
CommitJob::InProgress(_) => "in_progress",
888-
CommitJob::Finished(_) => "finished",
889-
CommitJob::Failed(_) => "failed",
890-
}
891-
}
892-
893-
/// True when `status == "finished"`.
894-
pub fn is_finished(&self) -> bool {
895-
matches!(self, CommitJob::Finished(_))
896-
}
897-
898-
/// Will compose the column names for the job type
899-
pub fn get_enqueue_column_names(&self) -> Vec<String> {
900-
let mut base_columns = vec![
901-
String::from("sha"),
902-
String::from("parent_sha"),
903-
String::from("commit_type"),
904-
String::from("commit_time"),
905-
String::from("status"),
906-
String::from("target"),
907-
String::from("include"),
908-
String::from("exclude"),
909-
String::from("runs"),
910-
String::from("backends"),
911-
];
912-
913-
/* This is the last column */
914-
match self.job_type {
915-
CommitJobType::Try(_) => base_columns.push("pr".into()),
916-
CommitJobType::Master(_) => base_columns.push("pr".into()),
917-
CommitJobType::Release(_) => base_columns.push("release_tag".into()),
918-
};
919-
920-
base_columns
921-
}
922-
}
923-
924-
impl Deref for CommitJob {
925-
type Target = CommitJobEntity;
926-
fn deref(&self) -> &Self::Target {
927-
match self {
928-
CommitJob::Queued(e) => e,
929-
CommitJob::InProgress(ip) => &ip.commit_job,
930-
CommitJob::Finished(fin) => &fin.commit_job,
931-
CommitJob::Failed(fail) => &fail.commit_job,
870+
match self.state {
871+
CommitJobState::Queued => "queued",
872+
CommitJobState::InProgress(_) => "in_progress",
873+
CommitJobState::Finished(_) => "finished",
874+
CommitJobState::Failed(_) => "failed",
932875
}
933876
}
934877
}
@@ -953,39 +896,29 @@ fn commit_job_create(
953896
backends: Option<String>,
954897
) -> CommitJob {
955898
let job_type = match commit_type {
956-
"try" => CommitJobType::Try(pr.expect("`pr` cannot be `None` for a Commit of type `try`")),
957-
"master" => {
958-
CommitJobType::Master(pr.expect("`pr` cannot be `None` for a Commit of type `master`"))
959-
}
960-
"release" => CommitJobType::Release(
961-
release_tag.expect("`release_tag` cannot be `None` for a Commit of type `release`"),
962-
),
899+
"try" => CommitJobType::Try {
900+
pr: pr.expect("`pr` cannot be `None` for a Commit of type `try`"),
901+
},
902+
"master" => CommitJobType::Master {
903+
pr: pr.expect("`pr` cannot be `None` for a Commit of type `master`"),
904+
},
905+
"release" => CommitJobType::Release {
906+
tag: release_tag
907+
.expect("`release_tag` cannot be `None` for a Commit of type `release`"),
908+
},
963909
_ => panic!("Unhandled commit_type {}", commit_type),
964910
};
965911

966-
let commit_job = CommitJobEntity {
967-
sha,
968-
parent_sha,
969-
commit_time,
970-
target,
971-
include,
972-
exclude,
973-
runs,
974-
backends,
975-
job_type,
976-
};
977-
978-
match status {
979-
"queued" => CommitJob::Queued(commit_job),
912+
let state = match status {
913+
"queued" => CommitJobState::Queued,
980914

981915
"in_progress" => {
982916
let started_at =
983917
started_at.expect("`started_at` must be Some for an `in_progress` job");
984918
let machine_id =
985919
machine_id.expect("`machine_id` must be Some for an `in_progress` job");
986920

987-
CommitJob::InProgress(CommitJobInProgress {
988-
commit_job,
921+
CommitJobState::InProgress(CommitJobInProgress {
989922
started_at,
990923
machine_id,
991924
})
@@ -1000,15 +933,13 @@ fn commit_job_create(
1000933
machine_id.expect("`machine_id` must be Some for finished or failed a job");
1001934

1002935
if status == "finished" {
1003-
CommitJob::Finished(CommitJobFinished {
1004-
commit_job,
936+
CommitJobState::Finished(CommitJobFinished {
1005937
started_at,
1006938
finished_at,
1007939
machine_id,
1008940
})
1009941
} else {
1010-
CommitJob::Failed(CommitJobFailed {
1011-
commit_job,
942+
CommitJobState::Failed(CommitJobFailed {
1012943
started_at,
1013944
finished_at,
1014945
machine_id,
@@ -1019,43 +950,18 @@ fn commit_job_create(
1019950
other => {
1020951
panic!("unknown status `{other}` (expected `queued`, `in_progress`, `finished` or `failed`)")
1021952
}
1022-
}
1023-
}
1024-
1025-
pub struct CommitsByType<'a> {
1026-
pub r#try: Vec<(&'a CommitJob, u32)>,
1027-
pub master: Vec<(&'a CommitJob, u32)>,
1028-
pub release: Vec<(&'a CommitJob, String)>,
1029-
}
1030-
1031-
/// Given a vector of `CommitJobs` bucket them out into;
1032-
/// `try`, `master` and `release` (in that order)
1033-
pub fn split_queued_commit_jobs(commit_jobs: &[CommitJob]) -> CommitsByType<'_> {
1034-
// Split jobs by type as that determines what we enter into the database,
1035-
// `ToSql` is quite finiky about lifetimes. Moreover the column names
1036-
// change depending on the commit job type. `master` and `try` have
1037-
// a `pr` column whereas `release` has a `release_rag` column
1038-
let (try_commits, master_commits, release_commits) = commit_jobs.iter().fold(
1039-
(vec![], vec![], vec![]),
1040-
|(mut try_commits, mut master_commits, mut release_commits), job| {
1041-
let entity = job
1042-
.as_queued()
1043-
.expect("Can only enqueue jobs with a status of `queued`");
1044-
1045-
match &entity.job_type {
1046-
crate::CommitJobType::Try(pr) => try_commits.push((job, *pr)),
1047-
crate::CommitJobType::Master(pr) => master_commits.push((job, *pr)),
1048-
crate::CommitJobType::Release(release_tag) => {
1049-
release_commits.push((job, release_tag.clone()))
1050-
}
1051-
}
1052-
(try_commits, master_commits, release_commits)
1053-
},
1054-
);
953+
};
1055954

1056-
CommitsByType {
1057-
r#try: try_commits,
1058-
master: master_commits,
1059-
release: release_commits,
955+
CommitJob {
956+
sha,
957+
parent_sha,
958+
commit_time,
959+
target,
960+
include,
961+
exclude,
962+
runs,
963+
backends,
964+
job_type,
965+
state,
1060966
}
1061967
}

database/src/pool.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,15 @@ pub trait Connection: Send + Sync {
180180
/// Removes all data associated with the given artifact.
181181
async fn purge_artifact(&self, aid: &ArtifactId);
182182

183-
/// Add a jobs to the queue
184-
async fn enqueue_commit_jobs(&self, jobs: &[CommitJob]);
183+
/// Add a job to the queue
184+
async fn enqueue_commit_job(&self, jobs: &CommitJob);
185185

186186
/// Dequeue jobs, we pass `machine_id` and `target` in case there are jobs
187187
/// the machine was previously doing and can pick up again
188-
async fn dequeue_commit_job(&self, machine_id: &str, target: Target) -> Option<CommitJob>;
188+
async fn take_commit_job(&self, machine_id: &str, target: Target) -> Option<CommitJob>;
189189

190190
/// Mark the job as finished
191-
async fn finish_commit_job(&self, machine_id: &str, target: Target, sha: String) -> bool;
191+
async fn finish_commit_job(&self, machine_id: &str, target: Target, sha: String);
192192
}
193193

194194
#[async_trait::async_trait]

0 commit comments

Comments
 (0)