@@ -4,18 +4,33 @@ use graph::prelude::{DeploymentHash, SubgraphName};
4
4
use graph:: slog:: { self , error, info, Logger } ;
5
5
use graph:: tokio:: sync:: mpsc:: Sender ;
6
6
use notify:: { recommended_watcher, Event , RecursiveMode , Watcher } ;
7
+ use std:: collections:: HashMap ;
7
8
use std:: path:: { Path , PathBuf } ;
8
9
use std:: sync:: mpsc;
9
10
use std:: time:: Duration ;
10
11
11
- use super :: helpers:: parse_manifest_arg;
12
+ use super :: helpers:: { parse_alias , parse_manifest_arg} ;
12
13
13
14
const WATCH_DELAY : Duration = Duration :: from_secs ( 5 ) ;
14
15
const DEFAULT_BUILD_DIR : & str = "build" ;
15
16
16
17
// Parses manifest arguments and returns a vector of paths to the manifest files
17
- pub fn parse_manifest_args ( manifests : Vec < String > , logger : & Logger ) -> Result < Vec < PathBuf > > {
18
+ pub fn parse_manifest_args (
19
+ manifests : Vec < String > ,
20
+ subgraph_sources : Vec < String > ,
21
+ logger : & Logger ,
22
+ ) -> Result < ( Vec < PathBuf > , HashMap < String , PathBuf > ) > {
18
23
let mut manifests_paths = Vec :: new ( ) ;
24
+ let mut source_subgraph_aliases = HashMap :: new ( ) ;
25
+
26
+ for subgraph_source in subgraph_sources {
27
+ let ( alias_name, manifest_path_str, build_dir_opt) = parse_alias ( & subgraph_source) ?;
28
+ let manifest_path =
29
+ process_manifest ( build_dir_opt, & manifest_path_str, Some ( & alias_name) , logger) ?;
30
+
31
+ manifests_paths. push ( manifest_path. clone ( ) ) ;
32
+ source_subgraph_aliases. insert ( alias_name, manifest_path) ;
33
+ }
19
34
20
35
for manifest_str in manifests {
21
36
let ( manifest_path_str, build_dir_opt) = parse_manifest_arg ( & manifest_str)
@@ -27,7 +42,7 @@ pub fn parse_manifest_args(manifests: Vec<String>, logger: &Logger) -> Result<Ve
27
42
manifests_paths. push ( built_manifest_path) ;
28
43
}
29
44
30
- Ok ( manifests_paths)
45
+ Ok ( ( manifests_paths, source_subgraph_aliases ) )
31
46
}
32
47
33
48
/// Helper function to process a manifest
@@ -103,12 +118,20 @@ fn process_manifest(
103
118
pub async fn watch_subgraphs (
104
119
logger : & Logger ,
105
120
manifests_paths : Vec < PathBuf > ,
121
+ source_subgraph_aliases : HashMap < String , PathBuf > ,
106
122
exclusions : Vec < String > ,
107
123
sender : Sender < ( DeploymentHash , SubgraphName ) > ,
108
124
) -> Result < ( ) > {
109
125
let logger = logger. new ( slog:: o!( "component" => "Watcher" ) ) ;
110
126
111
- watch_subgraph_dirs ( & logger, manifests_paths, exclusions, sender) . await ?;
127
+ watch_subgraph_dirs (
128
+ & logger,
129
+ manifests_paths,
130
+ source_subgraph_aliases,
131
+ exclusions,
132
+ sender,
133
+ )
134
+ . await ?;
112
135
Ok ( ( ) )
113
136
}
114
137
@@ -117,6 +140,7 @@ pub async fn watch_subgraphs(
117
140
pub async fn watch_subgraph_dirs (
118
141
logger : & Logger ,
119
142
manifests_paths : Vec < PathBuf > ,
143
+ source_subgraph_aliases : HashMap < String , PathBuf > ,
120
144
exclusions : Vec < String > ,
121
145
sender : Sender < ( DeploymentHash , SubgraphName ) > ,
122
146
) -> Result < ( ) > {
@@ -159,7 +183,15 @@ pub async fn watch_subgraph_dirs(
159
183
}
160
184
161
185
// Process file change events
162
- process_file_events ( logger, rx, & exclusion_set, & manifests_paths, sender) . await
186
+ process_file_events (
187
+ logger,
188
+ rx,
189
+ & exclusion_set,
190
+ & manifests_paths,
191
+ & source_subgraph_aliases,
192
+ sender,
193
+ )
194
+ . await
163
195
}
164
196
165
197
/// Processes file change events and triggers redeployments
@@ -168,6 +200,7 @@ async fn process_file_events(
168
200
rx : mpsc:: Receiver < Result < Event , notify:: Error > > ,
169
201
exclusion_set : & GlobSet ,
170
202
manifests_paths : & Vec < PathBuf > ,
203
+ source_subgraph_aliases : & HashMap < String , PathBuf > ,
171
204
sender : Sender < ( DeploymentHash , SubgraphName ) > ,
172
205
) -> Result < ( ) > {
173
206
loop {
@@ -203,7 +236,7 @@ async fn process_file_events(
203
236
}
204
237
205
238
// Redeploy all subgraphs
206
- redeploy_all_subgraphs ( logger, manifests_paths, & sender) . await ?;
239
+ redeploy_all_subgraphs ( logger, manifests_paths, source_subgraph_aliases , & sender) . await ?;
207
240
}
208
241
}
209
242
@@ -223,15 +256,24 @@ fn is_relevant_event(event: &Event, watched_dirs: Vec<PathBuf>, exclusion_set: &
223
256
async fn redeploy_all_subgraphs (
224
257
logger : & Logger ,
225
258
manifests_paths : & Vec < PathBuf > ,
259
+ source_subgraph_aliases : & HashMap < String , PathBuf > ,
226
260
sender : & Sender < ( DeploymentHash , SubgraphName ) > ,
227
261
) -> Result < ( ) > {
228
262
info ! ( logger, "File change detected, redeploying all subgraphs" ) ;
229
263
let mut count = 0 ;
230
264
for manifest_path in manifests_paths {
265
+ let alias_name = source_subgraph_aliases
266
+ . iter ( )
267
+ . find ( |( _, path) | path == & manifest_path)
268
+ . map ( |( name, _) | name) ;
269
+
270
+ let id = alias_name
271
+ . map ( |s| s. to_owned ( ) )
272
+ . unwrap_or_else ( || manifest_path. display ( ) . to_string ( ) ) ;
273
+
231
274
let _ = sender
232
275
. send ( (
233
- DeploymentHash :: new ( manifest_path. display ( ) . to_string ( ) )
234
- . map_err ( |_| anyhow ! ( "Failed to create deployment hash" ) ) ?,
276
+ DeploymentHash :: new ( id) . map_err ( |_| anyhow ! ( "Failed to create deployment hash" ) ) ?,
235
277
SubgraphName :: new ( format ! ( "subgraph-{}" , count) )
236
278
. map_err ( |_| anyhow ! ( "Failed to create subgraph name" ) ) ?,
237
279
) )
0 commit comments