@@ -17,6 +17,7 @@ use tokio::sync::mpsc;
17
17
use tokio_util:: sync:: CancellationToken ;
18
18
19
19
const TASK_PRINT_INTERVAL_SECS : u64 = 20 ;
20
+ const PEER_PRINT_INTERVAL_SECS : u64 = 40 ;
20
21
21
22
pub struct DriaMonitorNode {
22
23
pub p2p : DriaP2PCommander ,
@@ -39,13 +40,20 @@ impl DriaMonitorNode {
39
40
results : HashMap :: new ( ) ,
40
41
}
41
42
}
43
+
44
+ /// Setup the monitor node.
45
+ ///
46
+ /// Subscribes to task topics.
42
47
pub async fn setup ( & self ) -> Result < ( ) > {
43
48
self . p2p . subscribe ( WorkflowHandler :: LISTEN_TOPIC ) . await ?;
44
49
self . p2p . subscribe ( WorkflowHandler :: RESPONSE_TOPIC ) . await ?;
45
50
46
51
Ok ( ( ) )
47
52
}
48
53
54
+ /// Shutdown the monitor node.
55
+ ///
56
+ /// Unsubscribes from task topics, closes channels.
49
57
pub async fn shutdown ( & mut self ) -> Result < ( ) > {
50
58
log:: info!( "Shutting down monitor" ) ;
51
59
self . p2p . unsubscribe ( WorkflowHandler :: LISTEN_TOPIC ) . await ?;
@@ -62,9 +70,16 @@ impl DriaMonitorNode {
62
70
Ok ( ( ) )
63
71
}
64
72
73
+ /// Run the monitor node.
65
74
pub async fn run ( & mut self , token : CancellationToken ) {
66
75
let mut task_print_interval =
67
76
tokio:: time:: interval ( tokio:: time:: Duration :: from_secs ( TASK_PRINT_INTERVAL_SECS ) ) ;
77
+ let mut peer_print_interval =
78
+ tokio:: time:: interval ( tokio:: time:: Duration :: from_secs ( PEER_PRINT_INTERVAL_SECS ) ) ;
79
+
80
+ // move one ticks
81
+ task_print_interval. tick ( ) . await ;
82
+ peer_print_interval. tick ( ) . await ;
68
83
69
84
loop {
70
85
tokio:: select! {
@@ -76,13 +91,27 @@ impl DriaMonitorNode {
76
91
}
77
92
None => break , // channel closed, we can return now
78
93
} ,
79
- // print task counts
80
94
_ = task_print_interval. tick( ) => self . handle_task_print( ) ,
95
+ _ = peer_print_interval. tick( ) => self . handle_peer_print( ) . await ,
81
96
_ = token. cancelled( ) => break ,
82
97
}
83
98
}
84
99
}
85
100
101
+ async fn handle_peer_print ( & self ) {
102
+ match self . p2p . peer_counts ( ) . await {
103
+ Ok ( ( mesh, all) ) => {
104
+ log:: info!( "Peer count: {} / {}" , mesh, all) ;
105
+ }
106
+ Err ( e) => {
107
+ log:: error!( "Error getting peer counts: {:?}" , e) ;
108
+ }
109
+ }
110
+ }
111
+
112
+ /// Handle incoming gossipsub message.
113
+ ///
114
+ /// Records the `task` and `result` messages only, does not respond to anything else.
86
115
async fn handle_message (
87
116
& mut self ,
88
117
( peer_id, message_id, gossipsub_message) : ( PeerId , MessageId , Message ) ,
@@ -120,6 +149,7 @@ impl DriaMonitorNode {
120
149
Ok ( ( ) )
121
150
}
122
151
152
+ /// Print the tasks (ids) that have not been responded to.
123
153
fn handle_task_print ( & self ) {
124
154
let seen_task_ids = self . tasks . keys ( ) . collect :: < Vec < _ > > ( ) ;
125
155
let seen_result_ids = self . results . keys ( ) . collect :: < Vec < _ > > ( ) ;
0 commit comments