Skip to content

Boot nginx from the backend app to fix graceful shutdown in production #3417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
release: bin/diesel migration run
web: ./script/start-web.sh
web: ./target/release/server
background_worker: ./target/release/background-worker
7 changes: 5 additions & 2 deletions script/start-web.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#! /bin/bash
set -ue

# Since this script is launched from our app, we tell the nginx
# buildpack (`bin/start-nginx`) that `cat` is our server.

if [[ -z "${USE_FASTBOOT-}" ]]; then
unset USE_FASTBOOT
bin/start-nginx ./target/release/server
bin/start-nginx cat
else
export USE_FASTBOOT
node --optimize_for_size --max_old_space_size=200 fastboot.js &
bin/start-nginx ./target/release/server &
bin/start-nginx cat &
wait -n
fi
12 changes: 8 additions & 4 deletions src/bin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use cargo_registry::{boot, App, Env};
use std::{
borrow::Cow,
fs::File,
process::Command,
sync::{mpsc::channel, Arc, Mutex},
thread,
time::Duration,
};

Expand Down Expand Up @@ -139,9 +139,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

println!("listening on port {}", port);

// Give tokio a chance to spawn the first worker thread
thread::sleep(Duration::from_millis(10));

// Creating this file tells heroku to tell nginx that the application is ready
// to receive traffic.
if heroku {
Expand All @@ -152,6 +149,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
};
println!("Writing to {}", path);
File::create(path).unwrap();

// Launch nginx via the Heroku nginx buildpack
// `wait()` is never called on the child process, but it should be okay to leave a zombie
// process around on shutdown when Heroku is tearing down the entire container anyway.
Command::new("./script/start-web.sh")
.spawn()
.expect("Couldn't spawn nginx");
}

// Block the main thread until the server has shutdown
Expand Down
4 changes: 2 additions & 2 deletions src/downloads_counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl DownloadsCounter {
}

println!(
"download_counter all_shards counted_versions={} counted_downloads={} pending_downloads={}",
"downloads_counter all_shards counted_versions={} counted_downloads={} pending_downloads={}",
counted_versions,
counted_downloads,
pending_downloads,
Expand All @@ -101,7 +101,7 @@ impl DownloadsCounter {

let stats = self.persist_shard(&conn, shard)?;
println!(
"download_counter shard={} counted_versions={} counted_downloads={} pending_downloads={}",
"downloads_counter shard={} counted_versions={} counted_downloads={} pending_downloads={}",
idx,
stats.counted_versions,
stats.counted_downloads,
Expand Down