@@ -61,32 +61,40 @@ Metrics Server:
6161Note that there is an Oracle and Exporter for each network, but only one Local Store and Global Store.
6262
6363################################################################################################################################## */
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;
7364use {
7465 self :: {
7566 config:: Config ,
76- pythd :: api :: rpc,
67+ pyth :: rpc,
7768 solana:: network,
7869 state:: notifier,
7970 } ,
8071 anyhow:: Result ,
8172 futures_util:: future:: join_all,
82- slog :: Logger ,
73+ lazy_static :: lazy_static ,
8374 std:: sync:: Arc ,
84- tokio:: sync:: {
85- broadcast,
86- mpsc,
87- } ,
75+ tokio:: sync:: watch,
8876} ;
8977
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+
9098pub struct Agent {
9199 config : Config ,
92100}
@@ -96,86 +104,69 @@ impl Agent {
96104 Agent { config }
97105 }
98106
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" ) ,
104116 ) ;
105117
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." ) ;
109120 } ;
110121 }
111122
112- async fn spawn ( & self , logger : Logger ) -> Result < ( ) > {
123+ async fn spawn ( & self ) -> Result < ( ) > {
113124 // job handles
114125 let mut jhs = vec ! [ ] ;
115126
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 ) ;
125129
126130 // Spawn the primary network
127131 jhs. extend ( network:: spawn_network (
128132 self . config . primary_network . clone ( ) ,
129133 network:: Network :: Primary ,
130- primary_keypair_loader_tx,
131- logger. new ( o ! ( "primary" => true ) ) ,
132- adapter. clone ( ) ,
134+ state. clone ( ) ,
133135 ) ?) ;
134136
135137 // Spawn the secondary network, if needed
136138 if let Some ( config) = & self . config . secondary_network {
137139 jhs. extend ( network:: spawn_network (
138140 config. clone ( ) ,
139141 network:: Network :: Secondary ,
140- secondary_keypair_loader_tx,
141- logger. new ( o ! ( "primary" => false ) ) ,
142- adapter. clone ( ) ,
142+ state. clone ( ) ,
143143 ) ?) ;
144144 }
145145
146146 // 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 ( ) ) ) ) ;
151148
152149 // Spawn the Pythd API Server
153150 jhs. push ( tokio:: spawn ( rpc:: run (
154151 self . config . pythd_api_server . clone ( ) ,
155- logger. clone ( ) ,
156- adapter. clone ( ) ,
157- shutdown_tx. subscribe ( ) ,
152+ state. clone ( ) ,
158153 ) ) ) ;
159154
160155 // Spawn the metrics server
161- jhs. push ( tokio:: spawn ( metrics:: MetricsServer :: spawn (
156+ jhs. push ( tokio:: spawn ( metrics:: spawn (
162157 self . config . metrics_server . bind_address ,
163- logger. clone ( ) ,
164- adapter,
165158 ) ) ) ;
166159
167160 // Spawn the remote keypair loader endpoint for both networks
168161 jhs. append (
169- & mut remote_keypair_loader:: RemoteKeypairLoader :: spawn (
170- primary_keypair_loader_rx,
171- secondary_keypair_loader_rx,
162+ & mut state:: keypairs:: spawn (
172163 self . config . primary_network . rpc_url . clone ( ) ,
173164 self . config
174165 . secondary_network
175166 . as_ref ( )
176167 . map ( |c| c. rpc_url . clone ( ) ) ,
177168 self . config . remote_keypair_loader . clone ( ) ,
178- logger ,
169+ state ,
179170 )
180171 . await ,
181172 ) ;
@@ -191,8 +182,7 @@ pub mod config {
191182 use {
192183 super :: {
193184 metrics,
194- pythd,
195- remote_keypair_loader,
185+ pyth,
196186 solana:: network,
197187 state,
198188 } ,
@@ -214,13 +204,14 @@ pub mod config {
214204 pub primary_network : network:: Config ,
215205 pub secondary_network : Option < network:: Config > ,
216206 #[ serde( default ) ]
217- pub pythd_adapter : state:: Config ,
207+ #[ serde( rename = "pythd_adapter" ) ]
208+ pub state : state:: Config ,
218209 #[ serde( default ) ]
219- pub pythd_api_server : pythd :: api :: rpc:: Config ,
210+ pub pythd_api_server : pyth :: rpc:: Config ,
220211 #[ serde( default ) ]
221212 pub metrics_server : metrics:: Config ,
222213 #[ serde( default ) ]
223- pub remote_keypair_loader : remote_keypair_loader :: Config ,
214+ pub remote_keypair_loader : state :: keypairs :: Config ,
224215 }
225216
226217 impl Config {
0 commit comments