@@ -61,32 +61,40 @@ Metrics Server:
61
61
Note that there is an Oracle and Exporter for each network, but only one Local Store and Global Store.
62
62
63
63
################################################################################################################################## */
64
-
65
- pub mod legacy_schedule;
66
- pub mod market_schedule;
67
- pub mod metrics;
68
- pub mod pythd;
69
- pub mod remote_keypair_loader;
70
- pub mod solana;
71
- pub mod state;
72
- pub mod store;
73
64
use {
74
65
self :: {
75
66
config:: Config ,
76
- pythd :: api :: rpc,
67
+ pyth :: rpc,
77
68
solana:: network,
78
69
state:: notifier,
79
70
} ,
80
71
anyhow:: Result ,
81
72
futures_util:: future:: join_all,
82
- slog :: Logger ,
73
+ lazy_static :: lazy_static ,
83
74
std:: sync:: Arc ,
84
- tokio:: sync:: {
85
- broadcast,
86
- mpsc,
87
- } ,
75
+ tokio:: sync:: watch,
88
76
} ;
89
77
78
+ pub mod legacy_schedule;
79
+ pub mod market_schedule;
80
+ pub mod metrics;
81
+ pub mod pyth;
82
+ pub mod solana;
83
+ pub mod state;
84
+
85
+ lazy_static ! {
86
+ /// A static exit flag to indicate to running threads that we're shutting down. This is used to
87
+ /// gracefully shut down the application.
88
+ ///
89
+ /// We make this global based on the fact the:
90
+ /// - The `Sender` side does not rely on any async runtime.
91
+ /// - Exit logic doesn't really require carefully threading this value through the app.
92
+ /// - The `Receiver` side of a watch channel performs the detection based on if the change
93
+ /// happened after the subscribe, so it means all listeners should always be notified
94
+ /// correctly.
95
+ pub static ref EXIT : watch:: Sender <bool > = watch:: channel( false ) . 0 ;
96
+ }
97
+
90
98
pub struct Agent {
91
99
config : Config ,
92
100
}
@@ -96,86 +104,69 @@ impl Agent {
96
104
Agent { config }
97
105
}
98
106
99
- pub async fn start ( & self , logger : Logger ) {
100
- info ! ( logger, "Starting {}" , env!( "CARGO_PKG_NAME" ) ;
101
- "config" => format!( "{:?}" , & self . config) ,
102
- "version" => env!( "CARGO_PKG_VERSION" ) ,
103
- "cwd" => std:: env:: current_dir( ) . map( |p| format!( "{}" , p. display( ) ) ) . unwrap_or( "<could not get current directory>" . to_owned( ) )
107
+ pub async fn start ( & self ) {
108
+ tracing:: info!(
109
+ config = format!( "{:?}" , & self . config) ,
110
+ version = env!( "CARGO_PKG_VERSION" ) ,
111
+ cwd = std:: env:: current_dir( )
112
+ . map( |p| format!( "{}" , p. display( ) ) )
113
+ . unwrap_or( "<could not get current directory>" . to_owned( ) ) ,
114
+ "Starting {}" ,
115
+ env!( "CARGO_PKG_NAME" ) ,
104
116
) ;
105
117
106
- if let Err ( err) = self . spawn ( logger. clone ( ) ) . await {
107
- error ! ( logger, "{}" , err) ;
108
- debug ! ( logger, "error context" ; "context" => format!( "{:?}" , err) ) ;
118
+ if let Err ( err) = self . spawn ( ) . await {
119
+ tracing:: error!( err = ?err, "Agent spawn failed." ) ;
109
120
} ;
110
121
}
111
122
112
- async fn spawn ( & self , logger : Logger ) -> Result < ( ) > {
123
+ async fn spawn ( & self ) -> Result < ( ) > {
113
124
// job handles
114
125
let mut jhs = vec ! [ ] ;
115
126
116
- // Create the channels
117
- // TODO: make all components listen to shutdown signal
118
- let ( shutdown_tx, _) = broadcast:: channel ( self . config . channel_capacities . shutdown ) ;
119
- let ( primary_keypair_loader_tx, primary_keypair_loader_rx) = mpsc:: channel ( 10 ) ;
120
- let ( secondary_keypair_loader_tx, secondary_keypair_loader_rx) = mpsc:: channel ( 10 ) ;
121
-
122
- // Create the Pythd Adapter.
123
- let adapter =
124
- Arc :: new ( state:: State :: new ( self . config . pythd_adapter . clone ( ) , logger. clone ( ) ) . await ) ;
127
+ // Create the Application State.
128
+ let state = Arc :: new ( state:: State :: new ( self . config . state . clone ( ) ) . await ) ;
125
129
126
130
// Spawn the primary network
127
131
jhs. extend ( network:: spawn_network (
128
132
self . config . primary_network . clone ( ) ,
129
133
network:: Network :: Primary ,
130
- primary_keypair_loader_tx,
131
- logger. new ( o ! ( "primary" => true ) ) ,
132
- adapter. clone ( ) ,
134
+ state. clone ( ) ,
133
135
) ?) ;
134
136
135
137
// Spawn the secondary network, if needed
136
138
if let Some ( config) = & self . config . secondary_network {
137
139
jhs. extend ( network:: spawn_network (
138
140
config. clone ( ) ,
139
141
network:: Network :: Secondary ,
140
- secondary_keypair_loader_tx,
141
- logger. new ( o ! ( "primary" => false ) ) ,
142
- adapter. clone ( ) ,
142
+ state. clone ( ) ,
143
143
) ?) ;
144
144
}
145
145
146
146
// Create the Notifier task for the Pythd RPC.
147
- jhs. push ( tokio:: spawn ( notifier (
148
- adapter. clone ( ) ,
149
- shutdown_tx. subscribe ( ) ,
150
- ) ) ) ;
147
+ jhs. push ( tokio:: spawn ( notifier ( state. clone ( ) ) ) ) ;
151
148
152
149
// Spawn the Pythd API Server
153
150
jhs. push ( tokio:: spawn ( rpc:: run (
154
151
self . config . pythd_api_server . clone ( ) ,
155
- logger. clone ( ) ,
156
- adapter. clone ( ) ,
157
- shutdown_tx. subscribe ( ) ,
152
+ state. clone ( ) ,
158
153
) ) ) ;
159
154
160
155
// Spawn the metrics server
161
- jhs. push ( tokio:: spawn ( metrics:: MetricsServer :: spawn (
156
+ jhs. push ( tokio:: spawn ( metrics:: spawn (
162
157
self . config . metrics_server . bind_address ,
163
- logger. clone ( ) ,
164
- adapter,
165
158
) ) ) ;
166
159
167
160
// Spawn the remote keypair loader endpoint for both networks
168
161
jhs. append (
169
- & mut remote_keypair_loader:: RemoteKeypairLoader :: spawn (
170
- primary_keypair_loader_rx,
171
- secondary_keypair_loader_rx,
162
+ & mut state:: keypairs:: spawn (
172
163
self . config . primary_network . rpc_url . clone ( ) ,
173
164
self . config
174
165
. secondary_network
175
166
. as_ref ( )
176
167
. map ( |c| c. rpc_url . clone ( ) ) ,
177
168
self . config . remote_keypair_loader . clone ( ) ,
178
- logger ,
169
+ state ,
179
170
)
180
171
. await ,
181
172
) ;
@@ -191,8 +182,7 @@ pub mod config {
191
182
use {
192
183
super :: {
193
184
metrics,
194
- pythd,
195
- remote_keypair_loader,
185
+ pyth,
196
186
solana:: network,
197
187
state,
198
188
} ,
@@ -214,13 +204,14 @@ pub mod config {
214
204
pub primary_network : network:: Config ,
215
205
pub secondary_network : Option < network:: Config > ,
216
206
#[ serde( default ) ]
217
- pub pythd_adapter : state:: Config ,
207
+ #[ serde( rename = "pythd_adapter" ) ]
208
+ pub state : state:: Config ,
218
209
#[ serde( default ) ]
219
- pub pythd_api_server : pythd :: api :: rpc:: Config ,
210
+ pub pythd_api_server : pyth :: rpc:: Config ,
220
211
#[ serde( default ) ]
221
212
pub metrics_server : metrics:: Config ,
222
213
#[ serde( default ) ]
223
- pub remote_keypair_loader : remote_keypair_loader :: Config ,
214
+ pub remote_keypair_loader : state :: keypairs :: Config ,
224
215
}
225
216
226
217
impl Config {
0 commit comments