@@ -18,9 +18,10 @@ use std::sync::Arc;
18
18
19
19
pub use awaited_action:: { AwaitedAction , AwaitedActionSortKey } ;
20
20
use futures:: { Future , Stream } ;
21
- use nativelink_error:: Error ;
21
+ use nativelink_error:: { make_input_err , Error , ResultExt } ;
22
22
use nativelink_metric:: MetricsComponent ;
23
- use nativelink_util:: action_messages:: { ActionInfo , OperationId } ;
23
+ use nativelink_util:: action_messages:: { ActionInfo , ActionStage , OperationId } ;
24
+ use serde:: { Deserialize , Serialize } ;
24
25
25
26
mod awaited_action;
26
27
@@ -33,8 +34,38 @@ pub enum SortedAwaitedActionState {
33
34
Completed ,
34
35
}
35
36
37
+ impl TryFrom < & ActionStage > for SortedAwaitedActionState {
38
+ type Error = Error ;
39
+ fn try_from ( value : & ActionStage ) -> Result < Self , Error > {
40
+ match value {
41
+ ActionStage :: CacheCheck => Ok ( Self :: CacheCheck ) ,
42
+ ActionStage :: Executing => Ok ( Self :: Executing ) ,
43
+ ActionStage :: Completed ( _) => Ok ( Self :: Completed ) ,
44
+ ActionStage :: Queued => Ok ( Self :: Queued ) ,
45
+ _ => Err ( make_input_err ! ( "Invalid State" ) ) ,
46
+ }
47
+ }
48
+ }
49
+
50
+ impl TryFrom < ActionStage > for SortedAwaitedActionState {
51
+ type Error = Error ;
52
+ fn try_from ( value : ActionStage ) -> Result < Self , Error > {
53
+ Self :: try_from ( & value)
54
+ }
55
+ }
56
+
57
+ impl std:: fmt:: Display for SortedAwaitedActionState {
58
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
59
+ match self {
60
+ SortedAwaitedActionState :: CacheCheck => write ! ( f, "CacheCheck" ) ,
61
+ SortedAwaitedActionState :: Queued => write ! ( f, "Queued" ) ,
62
+ SortedAwaitedActionState :: Executing => write ! ( f, "Executing" ) ,
63
+ SortedAwaitedActionState :: Completed => write ! ( f, "Completed" ) ,
64
+ }
65
+ }
66
+ }
36
67
/// A struct pointing to an AwaitedAction that can be sorted.
37
- #[ derive( Debug , Clone , MetricsComponent ) ]
68
+ #[ derive( Debug , Clone , Serialize , Deserialize , MetricsComponent ) ]
38
69
pub struct SortedAwaitedAction {
39
70
#[ metric( help = "The sort key of the AwaitedAction" ) ]
40
71
pub sort_key : AwaitedActionSortKey ,
@@ -64,6 +95,48 @@ impl Ord for SortedAwaitedAction {
64
95
}
65
96
}
66
97
98
+ impl std:: fmt:: Display for SortedAwaitedAction {
99
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
100
+ std:: fmt:: write (
101
+ f,
102
+ format_args ! ( "{}-{}" , self . sort_key. as_u64( ) , self . operation_id) ,
103
+ )
104
+ }
105
+ }
106
+
107
+ impl From < & AwaitedAction > for SortedAwaitedAction {
108
+ fn from ( value : & AwaitedAction ) -> Self {
109
+ Self {
110
+ operation_id : value. operation_id ( ) . clone ( ) ,
111
+ sort_key : value. sort_key ( ) ,
112
+ }
113
+ }
114
+ }
115
+
116
+ impl From < AwaitedAction > for SortedAwaitedAction {
117
+ fn from ( value : AwaitedAction ) -> Self {
118
+ Self :: from ( & value)
119
+ }
120
+ }
121
+
122
+ impl TryInto < Vec < u8 > > for SortedAwaitedAction {
123
+ type Error = Error ;
124
+ fn try_into ( self ) -> Result < Vec < u8 > , Self :: Error > {
125
+ serde_json:: to_vec ( & self )
126
+ . map_err ( |e| make_input_err ! ( "{}" , e. to_string( ) ) )
127
+ . err_tip ( || "In SortedAwaitedAction::TryInto::<Vec<u8>>" )
128
+ }
129
+ }
130
+
131
+ impl TryFrom < & [ u8 ] > for SortedAwaitedAction {
132
+ type Error = Error ;
133
+ fn try_from ( value : & [ u8 ] ) -> Result < Self , Self :: Error > {
134
+ serde_json:: from_slice ( value)
135
+ . map_err ( |e| make_input_err ! ( "{}" , e. to_string( ) ) )
136
+ . err_tip ( || "In AwaitedAction::TryFrom::&[u8]" )
137
+ }
138
+ }
139
+
67
140
/// Subscriber that can be used to monitor when AwaitedActions change.
68
141
pub trait AwaitedActionSubscriber : Send + Sync + Sized + ' static {
69
142
/// Wait for AwaitedAction to change.
0 commit comments