Skip to content

Commit 29f83a8

Browse files
authored
Fix rover dev startup (#2309)
1 parent 7d09315 commit 29f83a8

File tree

2 files changed

+47
-25
lines changed

2 files changed

+47
-25
lines changed

src/command/dev/next/mod.rs

+23-19
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use anyhow::anyhow;
44
use apollo_federation_types::config::RouterVersion;
55
use camino::Utf8PathBuf;
6+
use futures::StreamExt;
67
use houston::{Config, Profile};
78
use router::{
89
install::InstallRouter,
@@ -27,7 +28,10 @@ use crate::{
2728
RoverError, RoverOutput, RoverResult,
2829
};
2930

30-
use self::router::config::{RouterAddress, RunRouterConfig};
31+
use self::router::{
32+
binary::RouterLog,
33+
config::{RouterAddress, RunRouterConfig},
34+
};
3135

3236
mod router;
3337

@@ -63,35 +67,22 @@ impl Dev {
6367
let federation_version = self.opts.supergraph_opts.federation_version.clone();
6468
let profile = self.opts.plugin_opts.profile.clone();
6569
let graph_ref = self.opts.supergraph_opts.graph_ref.clone();
66-
let composition_output = tmp_config_dir_path.join("supergraph.graphql");
67-
write_file_impl
68-
.ready()
69-
.await?
70-
.call(
71-
WriteFileRequest::builder()
72-
.path(composition_output.clone())
73-
.build(),
74-
)
75-
.await?;
7670

7771
let one_shot_composition = OneShotComposition::builder()
7872
.client_config(client_config.clone())
7973
.profile(profile.clone())
8074
.elv2_license_accepter(elv2_license_accepter)
8175
.skip_update(skip_update)
82-
.output_file(composition_output)
8376
.and_federation_version(federation_version)
8477
.and_graph_ref(graph_ref.clone())
8578
.and_supergraph_yaml(supergraph_yaml)
8679
.and_override_install_path(override_install_path.clone())
8780
.build();
8881

89-
// The router binary will know where to find the composition result; we compose initially
90-
// for the router to have a properly composed schema when starting
91-
// TODO: produce a filepath instead and pass that along to make this clearer
92-
one_shot_composition
82+
let supergraph_schema = one_shot_composition
9383
.compose(&read_file_impl, &write_file_impl, &exec_command_impl)
94-
.await?;
84+
.await?
85+
.supergraph_sdl;
9586

9687
// TODO: figure out how to actually get this; maybe based on fed version? didn't see a cli
9788
// opt
@@ -105,7 +96,7 @@ impl Dev {
10596
.service()?;
10697
let service = WhoAmI::new(service);
10798

108-
RunRouter::default()
99+
let mut run_router = RunRouter::default()
109100
.install::<InstallRouter>(
110101
router_version,
111102
client_config.clone(),
@@ -123,12 +114,25 @@ impl Dev {
123114
TokioSpawn::default(),
124115
&tmp_config_dir_path,
125116
client_config,
117+
supergraph_schema,
126118
)
127119
.await?
128120
.watch_for_changes(write_file_impl)
129121
.await;
130122

131-
// TODO: more stuff with dev, the router is alive
123+
while let Some(router_log) = run_router.router_logs().next().await {
124+
match router_log {
125+
Ok(RouterLog::Stdout(router_log)) => {
126+
tracing::info!("{}", router_log);
127+
}
128+
Ok(RouterLog::Stderr(router_log)) => {
129+
tracing::error!("{:?}", router_log);
130+
}
131+
Err(err) => {
132+
tracing::error!("{:?}", err);
133+
}
134+
}
135+
}
132136

133137
Ok(RoverOutput::EmptySuccess)
134138
}

src/command/dev/next/router/run.rs

+24-6
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ impl RunRouter<state::Run> {
131131
spawn: Spawn,
132132
temp_router_dir: &Utf8Path,
133133
studio_client_config: StudioClientConfig,
134+
supergraph_schema: String,
134135
) -> Result<RunRouter<state::Watch>, RunRouterBinaryError>
135136
where
136137
Spawn: Service<ExecCommandConfig, Response = Child> + Send + Clone + 'static,
@@ -160,15 +161,17 @@ impl RunRouter<state::Run> {
160161
path: hot_reload_config_path.clone(),
161162
err: Box::new(err),
162163
})?;
164+
163165
let hot_reload_schema_path = temp_router_dir.join("supergraph.graphql");
164166
tracing::debug!(
165-
"Creating temporary schema path at {}",
167+
"Creating temporary supergraph schema path at {}",
166168
hot_reload_schema_path
167169
);
168170
write_file
169171
.call(
170172
WriteFileRequest::builder()
171173
.path(hot_reload_schema_path.clone())
174+
.contents(supergraph_schema.into_bytes())
172175
.build(),
173176
)
174177
.await
@@ -185,7 +188,7 @@ impl RunRouter<state::Run> {
185188
.spawn(spawn)
186189
.build();
187190

188-
let (_router_log_events, run_router_binary_subtask): (
191+
let (router_logs, run_router_binary_subtask): (
189192
UnboundedReceiverStream<Result<RouterLog, RunRouterBinaryError>>,
190193
_,
191194
) = Subtask::new(run_router_binary);
@@ -200,6 +203,7 @@ impl RunRouter<state::Run> {
200203
config_path: self.state.config_path,
201204
hot_reload_config_path,
202205
hot_reload_schema_path,
206+
router_logs,
203207
},
204208
})
205209
}
@@ -277,7 +281,7 @@ impl RunRouter<state::Watch> {
277281
.write_file_impl(write_file_impl)
278282
.build();
279283

280-
let (_hot_reload_events, hot_reload_subtask): (UnboundedReceiverStream<HotReloadEvent>, _) =
284+
let (hot_reload_events, hot_reload_subtask): (UnboundedReceiverStream<HotReloadEvent>, _) =
281285
Subtask::new(hot_reload_watcher);
282286

283287
let router_config_updates = router_config_updates
@@ -286,26 +290,37 @@ impl RunRouter<state::Watch> {
286290

287291
let abort_hot_reload = SubtaskRunStream::run(hot_reload_subtask, router_config_updates);
288292

289-
let abort_config_watcher = config_watcher_subtask
290-
.map(|config_watcher_subtask| SubtaskRunUnit::run(config_watcher_subtask));
293+
let abort_config_watcher = config_watcher_subtask.map(SubtaskRunUnit::run);
291294

292295
RunRouter {
293296
state: state::Abort {
294297
abort_router: self.state.abort_router,
295298
abort_hot_reload,
296299
abort_config_watcher,
300+
hot_reload_events,
301+
router_logs: self.state.router_logs,
297302
},
298303
}
299304
}
300305
}
301306

307+
impl RunRouter<state::Abort> {
308+
pub fn router_logs(
309+
&mut self,
310+
) -> &mut UnboundedReceiverStream<Result<RouterLog, RunRouterBinaryError>> {
311+
&mut self.state.router_logs
312+
}
313+
}
314+
302315
mod state {
303316
use camino::Utf8PathBuf;
304317
use tokio::task::AbortHandle;
318+
use tokio_stream::wrappers::UnboundedReceiverStream;
305319

306320
use crate::command::dev::next::router::{
307-
binary::RouterBinary,
321+
binary::{RouterBinary, RouterLog, RunRouterBinaryError},
308322
config::{remote::RemoteRouterConfig, RouterConfigFinal},
323+
hot_reload::HotReloadEvent,
309324
};
310325

311326
#[derive(Default)]
@@ -329,8 +344,11 @@ mod state {
329344
pub config_path: Option<Utf8PathBuf>,
330345
pub hot_reload_config_path: Utf8PathBuf,
331346
pub hot_reload_schema_path: Utf8PathBuf,
347+
pub router_logs: UnboundedReceiverStream<Result<RouterLog, RunRouterBinaryError>>,
332348
}
333349
pub struct Abort {
350+
pub router_logs: UnboundedReceiverStream<Result<RouterLog, RunRouterBinaryError>>,
351+
pub hot_reload_events: UnboundedReceiverStream<HotReloadEvent>,
334352
pub abort_router: AbortHandle,
335353
pub abort_config_watcher: Option<AbortHandle>,
336354
pub abort_hot_reload: AbortHandle,

0 commit comments

Comments
 (0)