@@ -5,7 +5,7 @@ use intern::intern;
5
5
use serde:: { Deserialize , Serialize } ;
6
6
use std:: fmt:: { self , Display , Formatter } ;
7
7
use std:: hash;
8
- use std:: ops:: { Add , Deref , Sub } ;
8
+ use std:: ops:: { Add , Sub } ;
9
9
use std:: sync:: Arc ;
10
10
use std:: time:: Duration ;
11
11
@@ -806,13 +806,24 @@ pub struct ArtifactCollection {
806
806
807
807
#[ derive( Debug , Clone , Serialize , Deserialize ) ]
808
808
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
+ }
812
823
}
813
824
814
825
#[ derive( Debug , Clone , Serialize , Deserialize ) ]
815
- pub struct CommitJobEntity {
826
+ pub struct CommitJob {
816
827
pub sha : String ,
817
828
pub parent_sha : String ,
818
829
pub commit_time : Date ,
@@ -822,113 +833,45 @@ pub struct CommitJobEntity {
822
833
pub runs : Option < i32 > ,
823
834
pub backends : Option < String > ,
824
835
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 ) ,
825
845
}
826
846
827
847
#[ derive( Debug , Clone , Serialize , Deserialize ) ]
828
848
pub struct CommitJobInProgress {
829
- pub commit_job : CommitJobEntity ,
830
849
pub machine_id : String ,
831
850
pub started_at : Date ,
832
851
}
833
852
834
853
#[ derive( Debug , Clone , Serialize , Deserialize ) ]
835
854
pub struct CommitJobFinished {
836
- pub commit_job : CommitJobEntity ,
837
855
pub machine_id : String ,
838
856
pub started_at : Date ,
839
857
pub finished_at : Date ,
840
858
}
841
859
842
860
#[ derive( Debug , Clone , Serialize , Deserialize ) ]
843
861
pub struct CommitJobFailed {
844
- pub commit_job : CommitJobEntity ,
845
862
pub machine_id : String ,
846
863
pub started_at : Date ,
847
864
pub finished_at : Date ,
848
865
}
849
866
850
- #[ derive( Debug , Clone , Serialize , Deserialize ) ]
851
- pub enum CommitJob {
852
- Queued ( CommitJobEntity ) ,
853
- InProgress ( CommitJobInProgress ) ,
854
- Finished ( CommitJobFinished ) ,
855
- Failed ( CommitJobFailed ) ,
856
- }
857
-
858
867
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
-
883
868
/// Get the status as a string
884
869
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" ,
932
875
}
933
876
}
934
877
}
@@ -953,39 +896,29 @@ fn commit_job_create(
953
896
backends : Option < String > ,
954
897
) -> CommitJob {
955
898
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
+ } ,
963
909
_ => panic ! ( "Unhandled commit_type {}" , commit_type) ,
964
910
} ;
965
911
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 ,
980
914
981
915
"in_progress" => {
982
916
let started_at =
983
917
started_at. expect ( "`started_at` must be Some for an `in_progress` job" ) ;
984
918
let machine_id =
985
919
machine_id. expect ( "`machine_id` must be Some for an `in_progress` job" ) ;
986
920
987
- CommitJob :: InProgress ( CommitJobInProgress {
988
- commit_job,
921
+ CommitJobState :: InProgress ( CommitJobInProgress {
989
922
started_at,
990
923
machine_id,
991
924
} )
@@ -1000,15 +933,13 @@ fn commit_job_create(
1000
933
machine_id. expect ( "`machine_id` must be Some for finished or failed a job" ) ;
1001
934
1002
935
if status == "finished" {
1003
- CommitJob :: Finished ( CommitJobFinished {
1004
- commit_job,
936
+ CommitJobState :: Finished ( CommitJobFinished {
1005
937
started_at,
1006
938
finished_at,
1007
939
machine_id,
1008
940
} )
1009
941
} else {
1010
- CommitJob :: Failed ( CommitJobFailed {
1011
- commit_job,
942
+ CommitJobState :: Failed ( CommitJobFailed {
1012
943
started_at,
1013
944
finished_at,
1014
945
machine_id,
@@ -1019,43 +950,18 @@ fn commit_job_create(
1019
950
other => {
1020
951
panic ! ( "unknown status `{other}` (expected `queued`, `in_progress`, `finished` or `failed`)" )
1021
952
}
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
+ } ;
1055
954
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,
1060
966
}
1061
967
}
0 commit comments