@@ -6,6 +6,95 @@ use async_trait::async_trait;
66pub use models:: * ;
77use std:: borrow:: Cow ;
88
9+ #[ cfg( feature = "diesel" ) ]
10+ use diesel:: {
11+ deserialize:: FromSql ,
12+ serialize:: { IsNull , Output , ToSql } ,
13+ sql_types:: Integer ,
14+ sqlite:: { Sqlite , SqliteValue } ,
15+ { AsExpression , FromSqlRow } ,
16+ } ;
17+ use serde:: { Deserialize , Serialize } ;
18+
19+ /// Represents the priority of an enqueued rebuild job. The job queue is sorted based on priority and
20+ /// time, so the lower this number is, the more prioritized the job is. It's a little backwards, but
21+ /// hey.
22+ ///
23+ /// There are some utility functions on the type for accessing default values for well-defined use
24+ /// cases. These map to constants in the same namespace as this type, and you can use either one.
25+ /// ```
26+ /// use rebuilderd_common::api::v1::Priority;
27+ ///
28+ /// assert_eq!(Priority::from(1), Priority::default());
29+ /// assert_eq!(Priority::from(2), Priority::retry());
30+ /// assert_eq!(Priority::from(0), Priority::manual());
31+ /// ```
32+ ///
33+ /// You can also set a completely custom priority. This is mostly useful for external API calls that
34+ /// orchestrate rebuilds.
35+ /// ```
36+ /// use rebuilderd_common::api::v1::Priority;
37+ ///
38+ /// let custom = Priority::from(10);
39+ /// assert_eq!(custom, Priority::from(10));
40+ ///
41+ /// ```
42+ #[ derive( Serialize , Deserialize , Debug , Eq , PartialEq , Copy , Clone ) ]
43+ #[ cfg_attr( feature = "diesel" , derive( FromSqlRow , AsExpression ) ) ]
44+ #[ cfg_attr( feature = "diesel" , diesel( sql_type = Integer ) ) ]
45+ #[ cfg_attr( feature = "diesel" , diesel( check_for_backend( diesel:: sqlite:: Sqlite ) ) ) ]
46+ pub struct Priority ( i32 ) ;
47+
48+ impl Priority {
49+ /// The default priority for enqueued rebuilds. The job queue is sorted based on priority and time,
50+ /// so the lower this number is, the more prioritized the job is. It's a little backwards, but hey.
51+ const DEFAULT_QUEUE_PRIORITY : i32 = 1 ;
52+
53+ /// The default priority used for automatically requeued jobs. This priority is lower than the one
54+ /// for untested packages.
55+ const DEFAULT_RETRY_PRIORITY : i32 = Self :: DEFAULT_QUEUE_PRIORITY + 1 ;
56+
57+ /// The default priority used for manually retried jobs. This priority is higher than the one for
58+ /// untested packages.
59+ const DEFAULT_MANUAL_PRIORITY : i32 = Self :: DEFAULT_QUEUE_PRIORITY - 1 ;
60+
61+ pub fn retry ( ) -> Self {
62+ Priority ( Self :: DEFAULT_RETRY_PRIORITY )
63+ }
64+
65+ pub fn manual ( ) -> Self {
66+ Priority ( Self :: DEFAULT_MANUAL_PRIORITY )
67+ }
68+ }
69+
70+ impl Default for Priority {
71+ fn default ( ) -> Self {
72+ Priority ( Self :: DEFAULT_QUEUE_PRIORITY )
73+ }
74+ }
75+
76+ #[ cfg( feature = "diesel" ) ]
77+ impl FromSql < Integer , Sqlite > for Priority {
78+ fn from_sql ( bytes : SqliteValue ) -> diesel:: deserialize:: Result < Self > {
79+ let value = <i32 as FromSql < Integer , Sqlite > >:: from_sql ( bytes) ?;
80+ Ok ( Priority ( value) )
81+ }
82+ }
83+
84+ #[ cfg( feature = "diesel" ) ]
85+ impl ToSql < Integer , Sqlite > for Priority {
86+ fn to_sql < ' b > ( & ' b self , out : & mut Output < ' b , ' _ , Sqlite > ) -> diesel:: serialize:: Result {
87+ out. set_value ( self . 0 ) ;
88+ Ok ( IsNull :: No )
89+ }
90+ }
91+
92+ impl From < i32 > for Priority {
93+ fn from ( value : i32 ) -> Self {
94+ Priority ( value)
95+ }
96+ }
97+
998#[ async_trait]
1099pub trait BuildRestApi {
11100 async fn get_builds (
0 commit comments