@@ -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
@@ -803,13 +803,24 @@ pub struct ArtifactCollection {
803
803
804
804
#[ derive( Debug , Clone , Serialize , Deserialize ) ]
805
805
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
+ }
809
820
}
810
821
811
822
#[ derive( Debug , Clone , Serialize , Deserialize ) ]
812
- pub struct CommitJobEntity {
823
+ pub struct CommitJob {
813
824
pub sha : String ,
814
825
pub parent_sha : String ,
815
826
pub commit_time : Date ,
@@ -819,113 +830,45 @@ pub struct CommitJobEntity {
819
830
pub runs : Option < i32 > ,
820
831
pub backends : Option < String > ,
821
832
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 ) ,
822
842
}
823
843
824
844
#[ derive( Debug , Clone , Serialize , Deserialize ) ]
825
845
pub struct CommitJobInProgress {
826
- pub commit_job : CommitJobEntity ,
827
846
pub machine_id : String ,
828
847
pub started_at : Date ,
829
848
}
830
849
831
850
#[ derive( Debug , Clone , Serialize , Deserialize ) ]
832
851
pub struct CommitJobFinished {
833
- pub commit_job : CommitJobEntity ,
834
852
pub machine_id : String ,
835
853
pub started_at : Date ,
836
854
pub finished_at : Date ,
837
855
}
838
856
839
857
#[ derive( Debug , Clone , Serialize , Deserialize ) ]
840
858
pub struct CommitJobFailed {
841
- pub commit_job : CommitJobEntity ,
842
859
pub machine_id : String ,
843
860
pub started_at : Date ,
844
861
pub finished_at : Date ,
845
862
}
846
863
847
- #[ derive( Debug , Clone , Serialize , Deserialize ) ]
848
- pub enum CommitJob {
849
- Queued ( CommitJobEntity ) ,
850
- InProgress ( CommitJobInProgress ) ,
851
- Finished ( CommitJobFinished ) ,
852
- Failed ( CommitJobFailed ) ,
853
- }
854
-
855
864
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
-
880
865
/// Get the status as a string
881
866
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" ,
929
872
}
930
873
}
931
874
}
@@ -950,39 +893,29 @@ fn commit_job_create(
950
893
backends : Option < String > ,
951
894
) -> CommitJob {
952
895
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
+ } ,
960
906
_ => panic ! ( "Unhandled commit_type {}" , commit_type) ,
961
907
} ;
962
908
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 ,
977
911
978
912
"in_progress" => {
979
913
let started_at =
980
914
started_at. expect ( "`started_at` must be Some for an `in_progress` job" ) ;
981
915
let machine_id =
982
916
machine_id. expect ( "`machine_id` must be Some for an `in_progress` job" ) ;
983
917
984
- CommitJob :: InProgress ( CommitJobInProgress {
985
- commit_job,
918
+ CommitJobState :: InProgress ( CommitJobInProgress {
986
919
started_at,
987
920
machine_id,
988
921
} )
@@ -997,15 +930,13 @@ fn commit_job_create(
997
930
machine_id. expect ( "`machine_id` must be Some for finished or failed a job" ) ;
998
931
999
932
if status == "finished" {
1000
- CommitJob :: Finished ( CommitJobFinished {
1001
- commit_job,
933
+ CommitJobState :: Finished ( CommitJobFinished {
1002
934
started_at,
1003
935
finished_at,
1004
936
machine_id,
1005
937
} )
1006
938
} else {
1007
- CommitJob :: Failed ( CommitJobFailed {
1008
- commit_job,
939
+ CommitJobState :: Failed ( CommitJobFailed {
1009
940
started_at,
1010
941
finished_at,
1011
942
machine_id,
@@ -1016,43 +947,18 @@ fn commit_job_create(
1016
947
other => {
1017
948
panic ! ( "unknown status `{other}` (expected `queued`, `in_progress`, `finished` or `failed`)" )
1018
949
}
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
+ } ;
1052
951
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,
1057
963
}
1058
964
}
0 commit comments