Skip to content

Commit b371703

Browse files
committed
Fix SIGHUP reload order: apply migrations first before reloading config. #93
1 parent 3ce2ec7 commit b371703

File tree

1 file changed

+23
-21
lines changed
  • trailbase-core/src/server

1 file changed

+23
-21
lines changed

trailbase-core/src/server/mod.rs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -199,26 +199,11 @@ impl Server {
199199
loop {
200200
stream.recv().await;
201201

202-
info!("Received SIGHUP: re-loading config & re-applying migrations.");
202+
info!("Received SIGHUP: re-apply migations then re-load config.");
203203

204-
// Reload config:
205-
match crate::config::load_or_init_config_textproto(
206-
state.data_dir(),
207-
state.schema_metadata(),
208-
)
209-
.await
210-
{
211-
Ok(config) => {
212-
if let Err(err) = state.validate_and_update_config(config, None).await {
213-
error!("Failed to reload config: {err}");
214-
}
215-
}
216-
Err(err) => {
217-
error!("Failed to reload config: {err}");
218-
}
219-
}
220-
221-
// Re-apply migrations.
204+
// Re-apply migrations. This needs to happen before reloading the config, which is
205+
// consistent with the startup order. Otherwise, we may validate a configuration
206+
// against a stale database schema.
222207
let user_migrations_path = state.data_dir().migrations_path();
223208
match state
224209
.conn()
@@ -229,8 +214,8 @@ impl Server {
229214
.await
230215
{
231216
Err(err) => {
232-
// NOTE: it's not clear what the best error behavior here is. Should the server continue
233-
// to run when migrations fail?
217+
// NOTE: it's not clear what the best error behavior here is. Should the server
218+
// continue to run when migrations fail?
234219
error!("Failed to apply migrations: {err}");
235220
}
236221
Ok(_new_db) => {
@@ -246,6 +231,23 @@ impl Server {
246231
}
247232
}
248233
}
234+
235+
// Reload config:
236+
match crate::config::load_or_init_config_textproto(
237+
state.data_dir(),
238+
state.schema_metadata(),
239+
)
240+
.await
241+
{
242+
Ok(config) => {
243+
if let Err(err) = state.validate_and_update_config(config, None).await {
244+
error!("Failed to reload config: {err}");
245+
}
246+
}
247+
Err(err) => {
248+
error!("Failed to reload config: {err}");
249+
}
250+
}
249251
}
250252
});
251253
}

0 commit comments

Comments
 (0)