Skip to content
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

feat: gracefully shutdown command when shutdown signal is received #493

Closed
Changes from 4 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
33 changes: 26 additions & 7 deletions crates/tuono/src/watch.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use miette::{IntoDiagnostic, Result};
use std::path::Path;
use std::sync::Arc;
use watchexec_supervisor::command::{Command, Program};

use miette::{IntoDiagnostic, Result};
use tokio::sync::mpsc;
use watchexec::Watchexec;
use watchexec_signals::Signal;
use watchexec_supervisor::command::{Command, Program};
use watchexec_supervisor::job::{start_job, Job};

use crate::mode::Mode;
Expand All @@ -19,7 +19,6 @@ const DEV_SSR_BIN_SRC: &str = "node_modules\\.bin\\tuono-dev-ssr.cmd";
const DEV_WATCH_BIN_SRC: &str = "node_modules/.bin/tuono-dev-watch";
#[cfg(not(target_os = "windows"))]
const DEV_SSR_BIN_SRC: &str = "node_modules/.bin/tuono-dev-ssr";

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add this new line back in

fn watch_react_src() -> Job {
if !Path::new(DEV_SSR_BIN_SRC).exists() {
eprintln!("Failed to find script to run dev watch. Please run `npm install`");
Expand Down Expand Up @@ -63,6 +62,8 @@ fn build_react_ssr_src() -> Job {

#[tokio::main]
pub async fn watch() -> Result<()> {
let (sender, mut receiver_shutdown) = mpsc::channel(1);

watch_react_src().start().await;

let run_server = build_rust_src();
Expand All @@ -76,6 +77,8 @@ pub async fn watch() -> Result<()> {
build_ssr_bundle.to_wait().await;

let wx = Watchexec::new(move |mut action| {
let sender = sender.clone();

let mut should_reload_ssr_bundle = false;
let mut should_reload_rust_server = false;

Expand Down Expand Up @@ -105,9 +108,15 @@ pub async fn watch() -> Result<()> {
build_ssr_bundle.start();
}

// if Ctrl-C is received, quit
if action.signals().any(|sig| sig == Signal::Interrupt) {
action.quit();
let build_ssr_bundle = build_ssr_bundle.clone();
let run_server = run_server.clone();
tokio::spawn(async move {
let _ = sender.send(()).await;
build_ssr_bundle.stop().await;
run_server.stop().await;
});
action.quit_gracefully(Signal::Interrupt, std::time::Duration::from_secs(9999));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should have a shorter delay maybe like ~30s.

}

action
Expand All @@ -116,6 +125,16 @@ pub async fn watch() -> Result<()> {
// watch the current directory
wx.config.pathset(["./src"]);

let _ = wx.main().await.into_diagnostic()?;
tokio::select! {
_ = wx.main() => {
println!("Main Recived.");
let _ = wx.main().await.into_diagnostic();

},
_ = receiver_shutdown.recv() => {
println!("Tuono gracefully shutting down...");
},
}

Ok(())
}
Loading