Skip to content

Commit 0a27dc2

Browse files
committed
PR feedback - simpilfy requeuing, remove C-like OOP and correct queries
1 parent abf50d5 commit 0a27dc2

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

@@ -803,13 +803,24 @@ pub struct ArtifactCollection {
803803

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

811822
#[derive(Debug, Clone, Serialize, Deserialize)]
812-
pub struct CommitJobEntity {
823+
pub struct CommitJob {
813824
pub sha: String,
814825
pub parent_sha: String,
815826
pub commit_time: Date,
@@ -819,113 +830,45 @@ pub struct CommitJobEntity {
819830
pub runs: Option<i32>,
820831
pub backends: Option<String>,
821832
pub job_type: CommitJobType,
833+
pub state: CommitJobState,
834+
}
835+
836+
#[derive(Debug, Clone, Serialize, Deserialize)]
837+
pub enum CommitJobState {
838+
Queued,
839+
Finished(CommitJobFinished),
840+
Failed(CommitJobFailed),
841+
InProgress(CommitJobInProgress),
822842
}
823843

824844
#[derive(Debug, Clone, Serialize, Deserialize)]
825845
pub struct CommitJobInProgress {
826-
pub commit_job: CommitJobEntity,
827846
pub machine_id: String,
828847
pub started_at: Date,
829848
}
830849

831850
#[derive(Debug, Clone, Serialize, Deserialize)]
832851
pub struct CommitJobFinished {
833-
pub commit_job: CommitJobEntity,
834852
pub machine_id: String,
835853
pub started_at: Date,
836854
pub finished_at: Date,
837855
}
838856

839857
#[derive(Debug, Clone, Serialize, Deserialize)]
840858
pub struct CommitJobFailed {
841-
pub commit_job: CommitJobEntity,
842859
pub machine_id: String,
843860
pub started_at: Date,
844861
pub finished_at: Date,
845862
}
846863

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

963-
let commit_job = CommitJobEntity {
964-
sha,
965-
parent_sha,
966-
commit_time,
967-
target,
968-
include,
969-
exclude,
970-
runs,
971-
backends,
972-
job_type,
973-
};
974-
975-
match status {
976-
"queued" => CommitJob::Queued(commit_job),
909+
let state = match status {
910+
"queued" => CommitJobState::Queued,
977911

978912
"in_progress" => {
979913
let started_at =
980914
started_at.expect("`started_at` must be Some for an `in_progress` job");
981915
let machine_id =
982916
machine_id.expect("`machine_id` must be Some for an `in_progress` job");
983917

984-
CommitJob::InProgress(CommitJobInProgress {
985-
commit_job,
918+
CommitJobState::InProgress(CommitJobInProgress {
986919
started_at,
987920
machine_id,
988921
})
@@ -997,15 +930,13 @@ fn commit_job_create(
997930
machine_id.expect("`machine_id` must be Some for finished or failed a job");
998931

999932
if status == "finished" {
1000-
CommitJob::Finished(CommitJobFinished {
1001-
commit_job,
933+
CommitJobState::Finished(CommitJobFinished {
1002934
started_at,
1003935
finished_at,
1004936
machine_id,
1005937
})
1006938
} else {
1007-
CommitJob::Failed(CommitJobFailed {
1008-
commit_job,
939+
CommitJobState::Failed(CommitJobFailed {
1009940
started_at,
1010941
finished_at,
1011942
machine_id,
@@ -1016,43 +947,18 @@ fn commit_job_create(
1016947
other => {
1017948
panic!("unknown status `{other}` (expected `queued`, `in_progress`, `finished` or `failed`)")
1018949
}
1019-
}
1020-
}
1021-
1022-
pub struct CommitsByType<'a> {
1023-
pub r#try: Vec<(&'a CommitJob, u32)>,
1024-
pub master: Vec<(&'a CommitJob, u32)>,
1025-
pub release: Vec<(&'a CommitJob, String)>,
1026-
}
1027-
1028-
/// Given a vector of `CommitJobs` bucket them out into;
1029-
/// `try`, `master` and `release` (in that order)
1030-
pub fn split_queued_commit_jobs(commit_jobs: &[CommitJob]) -> CommitsByType<'_> {
1031-
// Split jobs by type as that determines what we enter into the database,
1032-
// `ToSql` is quite finiky about lifetimes. Moreover the column names
1033-
// change depending on the commit job type. `master` and `try` have
1034-
// a `pr` column whereas `release` has a `release_rag` column
1035-
let (try_commits, master_commits, release_commits) = commit_jobs.iter().fold(
1036-
(vec![], vec![], vec![]),
1037-
|(mut try_commits, mut master_commits, mut release_commits), job| {
1038-
let entity = job
1039-
.as_queued()
1040-
.expect("Can only enqueue jobs with a status of `queued`");
1041-
1042-
match &entity.job_type {
1043-
crate::CommitJobType::Try(pr) => try_commits.push((job, *pr)),
1044-
crate::CommitJobType::Master(pr) => master_commits.push((job, *pr)),
1045-
crate::CommitJobType::Release(release_tag) => {
1046-
release_commits.push((job, release_tag.clone()))
1047-
}
1048-
}
1049-
(try_commits, master_commits, release_commits)
1050-
},
1051-
);
950+
};
1052951

1053-
CommitsByType {
1054-
r#try: try_commits,
1055-
master: master_commits,
1056-
release: release_commits,
952+
CommitJob {
953+
sha,
954+
parent_sha,
955+
commit_time,
956+
target,
957+
include,
958+
exclude,
959+
runs,
960+
backends,
961+
job_type,
962+
state,
1057963
}
1058964
}

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)