这是indexloc提供的服务,不要输入任何密码
Skip to content

chore(signals): prefactor #9995

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 9 commits into from
Feb 27, 2025
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
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ turborepo-repository = { path = "crates/turborepo-repository" }
turborepo-ui = { path = "crates/turborepo-ui" }
turborepo-unescape = { path = "crates/turborepo-unescape" }
turborepo-scm = { path = "crates/turborepo-scm" }
turborepo-signals = { path = "crates/turborepo-signals" }
wax = { path = "crates/turborepo-wax" }
turborepo-vercel-api = { path = "crates/turborepo-vercel-api" }
turborepo-vercel-api-mock = { path = "crates/turborepo-vercel-api-mock" }
Expand Down
1 change: 1 addition & 0 deletions crates/turborepo-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ turborepo-lockfiles = { workspace = true }
turborepo-microfrontends = { workspace = true }
turborepo-repository = { path = "../turborepo-repository" }
turborepo-scm = { workspace = true }
turborepo-signals = { workspace = true }
turborepo-telemetry = { path = "../turborepo-telemetry" }
turborepo-ui = { workspace = true }
turborepo-unescape = { workspace = true }
Expand Down
6 changes: 3 additions & 3 deletions crates/turborepo-lib/src/cli/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ use itertools::Itertools;
use miette::Diagnostic;
use thiserror::Error;
use turborepo_repository::package_graph;
use turborepo_signals::{listeners::get_signal, SignalHandler};
use turborepo_telemetry::events::command::CommandEventBuilder;
use turborepo_ui::{color, BOLD, GREY};

use crate::{
commands::{bin, generate, link, login, ls, prune, run::get_signal, CommandBase},
commands::{bin, generate, link, login, ls, prune, CommandBase},
daemon::DaemonError,
query,
rewrite_json::RewriteError,
run,
run::{builder::RunBuilder, watch},
signal::SignalHandler,
};

#[derive(Debug, Error, Diagnostic)]
Expand Down Expand Up @@ -78,7 +78,7 @@ pub async fn print_potential_tasks(
base: CommandBase,
telemetry: CommandEventBuilder,
) -> Result<(), Error> {
let signal = get_signal()?;
let signal = get_signal().map_err(run::Error::from)?;
let handler = SignalHandler::new(signal);
let color_config = base.color_config;

Expand Down
10 changes: 3 additions & 7 deletions crates/turborepo-lib/src/commands/boundaries.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
use turborepo_signals::{listeners::get_signal, SignalHandler};
use turborepo_telemetry::events::command::CommandEventBuilder;

use crate::{
cli,
commands::{run::get_signal, CommandBase},
run::builder::RunBuilder,
signal::SignalHandler,
};
use crate::{cli, commands::CommandBase, run::builder::RunBuilder};

pub async fn run(base: CommandBase, telemetry: CommandEventBuilder) -> Result<i32, cli::Error> {
let signal = get_signal()?;
let signal = get_signal().map_err(crate::run::Error::from)?;
let handler = SignalHandler::new(signal);

let run = RunBuilder::new(base)?
Expand Down
6 changes: 3 additions & 3 deletions crates/turborepo-lib/src/commands/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ use serde::Serialize;
use thiserror::Error;
use turbopath::AnchoredSystemPath;
use turborepo_repository::package_graph::{PackageName, PackageNode};
use turborepo_signals::{listeners::get_signal, SignalHandler};
use turborepo_telemetry::events::command::CommandEventBuilder;
use turborepo_ui::{color, cprint, cprintln, ColorConfig, BOLD, BOLD_GREEN, GREY};

use crate::{
cli,
cli::OutputFormat,
commands::{run::get_signal, CommandBase},
commands::CommandBase,
run::{builder::RunBuilder, Run},
signal::SignalHandler,
};

#[derive(Debug, Error, Diagnostic)]
Expand Down Expand Up @@ -115,7 +115,7 @@ pub async fn run(
telemetry: CommandEventBuilder,
output: Option<OutputFormat>,
) -> Result<(), cli::Error> {
let signal = get_signal()?;
let signal = get_signal().map_err(crate::run::Error::from)?;
let handler = SignalHandler::new(signal);

let run_builder = RunBuilder::new(base)?;
Expand Down
6 changes: 3 additions & 3 deletions crates/turborepo-lib/src/commands/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use camino::Utf8Path;
use miette::{Diagnostic, Report, SourceSpan};
use thiserror::Error;
use turbopath::AbsoluteSystemPathBuf;
use turborepo_signals::{listeners::get_signal, SignalHandler};
use turborepo_telemetry::events::command::CommandEventBuilder;

use crate::{
commands::{run::get_signal, CommandBase},
commands::CommandBase,
query,
query::{Error, RepositoryQuery},
run::builder::RunBuilder,
signal::SignalHandler,
};

const SCHEMA_QUERY: &str = "query IntrospectionQuery {
Expand Down Expand Up @@ -161,7 +161,7 @@ pub async fn run(
variables_path: Option<&Utf8Path>,
include_schema: bool,
) -> Result<i32, Error> {
let signal = get_signal()?;
let signal = get_signal().map_err(crate::run::Error::from)?;
let handler = SignalHandler::new(signal);

let run_builder = RunBuilder::new(base)?
Expand Down
31 changes: 3 additions & 28 deletions crates/turborepo-lib/src/commands/run.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,11 @@
use std::{future::Future, sync::Arc};
use std::sync::Arc;

use tracing::error;
use turborepo_signals::{listeners::get_signal, SignalHandler};
use turborepo_telemetry::events::command::CommandEventBuilder;
use turborepo_ui::sender::UISender;

use crate::{commands::CommandBase, run, run::builder::RunBuilder, signal::SignalHandler};

#[cfg(windows)]
pub fn get_signal() -> Result<impl Future<Output = Option<()>>, run::Error> {
let mut ctrl_c = tokio::signal::windows::ctrl_c().map_err(run::Error::SignalHandler)?;
Ok(async move { ctrl_c.recv().await })
}

#[cfg(not(windows))]
pub fn get_signal() -> Result<impl Future<Output = Option<()>>, run::Error> {
use tokio::signal::unix;
let mut sigint =
unix::signal(unix::SignalKind::interrupt()).map_err(run::Error::SignalHandler)?;
let mut sigterm =
unix::signal(unix::SignalKind::terminate()).map_err(run::Error::SignalHandler)?;

Ok(async move {
tokio::select! {
res = sigint.recv() => {
res
}
res = sigterm.recv() => {
res
}
}
})
}
use crate::{commands::CommandBase, run, run::builder::RunBuilder};

pub async fn run(base: CommandBase, telemetry: CommandEventBuilder) -> Result<i32, run::Error> {
let signal = get_signal()?;
Expand Down
1 change: 0 additions & 1 deletion crates/turborepo-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ mod query;
mod rewrite_json;
mod run;
mod shim;
mod signal;
mod task_graph;
mod task_hash;
mod tracing;
Expand Down
2 changes: 1 addition & 1 deletion crates/turborepo-lib/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ use tokio::select;
use turbo_trace::TraceError;
use turbopath::AbsoluteSystemPathBuf;
use turborepo_repository::{change_mapper::AllPackageChangeReason, package_graph::PackageName};
use turborepo_signals::SignalHandler;

use crate::{
get_version,
query::{file::File, task::RepositoryTask},
run::{builder::RunBuilder, Run},
signal::SignalHandler,
};

#[derive(Error, Debug, miette::Diagnostic)]
Expand Down
2 changes: 1 addition & 1 deletion crates/turborepo-lib/src/run/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use turborepo_repository::{
package_json::PackageJson,
};
use turborepo_scm::SCM;
use turborepo_signals::{SignalHandler, SignalSubscriber};
use turborepo_telemetry::events::{
command::CommandEventBuilder,
generic::{DaemonInitStatus, GenericEventBuilder},
Expand All @@ -46,7 +47,6 @@ use crate::{
process::ProcessManager,
run::{scope, task_access::TaskAccess, task_id::TaskName, Error, Run, RunCache},
shim::TurboState,
signal::{SignalHandler, SignalSubscriber},
turbo_json::{TurboJson, TurboJsonLoader, UIMode},
DaemonConnector,
};
Expand Down
4 changes: 2 additions & 2 deletions crates/turborepo-lib/src/run/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ pub enum Error {
#[error(transparent)]
#[diagnostic(transparent)]
Visitor(#[from] task_graph::VisitorError),
#[error("Failed to register signal handler: {0}")]
SignalHandler(std::io::Error),
#[error(transparent)]
SignalHandler(#[from] turborepo_signals::listeners::Error),
#[error(transparent)]
Daemon(#[from] daemon::DaemonError),
#[error(transparent)]
Expand Down
10 changes: 6 additions & 4 deletions crates/turborepo-lib/src/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ use std::{

pub use cache::{CacheOutput, ConfigCache, Error as CacheError, RunCache, TaskCache};
use chrono::{DateTime, Local};
use futures::StreamExt;
use rayon::iter::ParallelBridge;
use tokio::{select, task::JoinHandle};
use tokio::{pin, select, task::JoinHandle};
use tracing::{debug, instrument};
use turbopath::{AbsoluteSystemPath, AbsoluteSystemPathBuf};
use turborepo_api_client::{APIAuth, APIClient};
use turborepo_ci::Vendor;
use turborepo_env::EnvironmentVariableMap;
use turborepo_repository::package_graph::{PackageGraph, PackageName, PackageNode};
use turborepo_scm::SCM;
use turborepo_signals::{listeners::get_signal, SignalHandler};
use turborepo_telemetry::events::generic::GenericEventBuilder;
use turborepo_ui::{
cprint, cprintln, sender::UISender, tui, tui::TuiSender, wui::sender::WebUISender, ColorConfig,
Expand All @@ -45,7 +47,6 @@ use crate::{
opts::Opts,
process::ProcessManager,
run::{global_hash::get_global_hash_inputs, summary::RunTracker, task_access::TaskAccess},
signal::SignalHandler,
task_graph::Visitor,
task_hash::{get_external_deps_hash, get_internal_deps_hash, PackageInputsHashes},
turbo_json::{TurboJson, TurboJsonLoader, UIMode},
Expand Down Expand Up @@ -336,8 +337,9 @@ impl Run {
};

let interrupt = async {
if let Ok(fut) = crate::commands::run::get_signal() {
fut.await;
if let Ok(fut) = get_signal() {
pin!(fut);
fut.next().await;
} else {
tracing::warn!("could not register ctrl-c handler");
// wait forever
Expand Down
6 changes: 3 additions & 3 deletions crates/turborepo-lib/src/run/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ use thiserror::Error;
use tokio::{select, sync::Notify, task::JoinHandle};
use tracing::{instrument, trace, warn};
use turborepo_repository::package_graph::PackageName;
use turborepo_signals::{listeners::get_signal, SignalHandler};
use turborepo_telemetry::events::command::CommandEventBuilder;
use turborepo_ui::sender::UISender;

use crate::{
commands::{self, CommandBase},
commands::CommandBase,
daemon::{proto, DaemonConnectorError, DaemonError},
get_version, opts,
run::{self, builder::RunBuilder, scope::target_selector::InvalidSelectorError, Run},
signal::SignalHandler,
turbo_json::CONFIG_FILE,
DaemonConnector, DaemonPaths,
};
Expand Down Expand Up @@ -115,7 +115,7 @@ impl WatchClient {
experimental_write_cache: bool,
telemetry: CommandEventBuilder,
) -> Result<Self, Error> {
let signal = commands::run::get_signal()?;
let signal = get_signal().map_err(crate::run::Error::from)?;
let handler = SignalHandler::new(signal);

if base.opts.repo_opts.root_turbo_json_path != base.repo_root.join_component(CONFIG_FILE) {
Expand Down
15 changes: 15 additions & 0 deletions crates/turborepo-signals/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "turborepo-signals"
version = "0.1.0"
edition = "2021"
license = "MIT"

[dependencies]
futures = "0.3.30"
Copy link
Contributor

Choose a reason for hiding this comment

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

Any particular reason we're using a different version for futures instead of the workspace root version?

Copy link
Member Author

Choose a reason for hiding this comment

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

I was matching the version used in turborepo-lib. I can update both to use workspace version in a followup PR.

thiserror = { workspace = true }
tokio = { workspace = true, features = ["full", "time"] }

[dev-dependencies]

[lints]
workspace = true
Loading
Loading