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

Shared exe-unit process module #3168

Merged
merged 6 commits into from
Apr 15, 2024
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
8 changes: 7 additions & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ ya-service-bus = { workspace = true }
ya-sgx = "0.2"
ya-utils-path = "0.1"
ya-utils-futures = "0.3"
ya-utils-process = { version = "0.2", features = ["lock"] }
ya-utils-process = { version = "0.3", features = ["lock"] }
ya-utils-networking = "0.2"
ya-fd-metrics = { path = "utils/fd-metrics" }
ya-version = "0.2"
Expand Down
4 changes: 2 additions & 2 deletions agent/provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ ya-file-logging = "0.1"
ya-utils-actix = "0.2"
ya-utils-cli = "0.1"
ya-utils-path = "0.1"
ya-utils-process = { version = "0.2", features = ['lock'] }
ya-utils-process = { version = "0.3", features = ['lock'] }
ya-std-utils = "0.1"
golem-certificate = "0.1.1"

Expand Down Expand Up @@ -94,4 +94,4 @@ ya-manifest-test-utils = "0.1"
ya-framework-basic = "0.1"

[lints]
workspace = true
workspace = true
2 changes: 1 addition & 1 deletion core/persistence/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ ya-client-model = { version = "0.6", features = ["with-diesel"] }
ya-core-model = { version = "0.9" }
ya-service-api = { version = "0.1", optional = true }
ya-service-api-interfaces = { version = "0.2", optional = true }
ya-utils-process = { version = "0.2", features = ["lock"], optional = true }
ya-utils-process = { version = "0.3", features = ["lock"], optional = true }

anyhow = "1.0.26"
bigdecimal = "0.2"
Expand Down
1 change: 1 addition & 0 deletions exe-unit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ ya-utils-networking = { version = "0.2", default-features = false, features = [
"dns",
"vpn",
] }
ya-utils-process = "0.3"
ya-packet-trace = { git = "https://github.com/golemfactory/ya-packet-trace" }
ya-gsb-http-proxy = { path = "../exe-unit/components/gsb-http-proxy" }
ya-counters = { path = "../exe-unit/components/counters", features = ["os"] }
Expand Down
3 changes: 2 additions & 1 deletion exe-unit/components/counters/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ description = "Runtime counters generating data used to collect activity metrics

[dependencies]
ya-agreement-utils = { workspace = true }
ya-utils-process = { version = "0.3", optional = true }

anyhow = "1.0"
actix = "0.13"
Expand Down Expand Up @@ -41,4 +42,4 @@ winapi = { version = "0.3.8", features = [
], optional = true }

[features]
os = ["dep:bytes", "dep:futures", "dep:lazy_static", "dep:nix", "dep:libproc", "dep:winapi"]
os = ["dep:ya-utils-process", "dep:bytes", "dep:futures", "dep:lazy_static", "dep:nix", "dep:libproc", "dep:winapi"]
10 changes: 9 additions & 1 deletion exe-unit/components/counters/src/os/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ mod unix;
#[cfg(windows)]
mod win;

pub mod process;
use ya_utils_process::SystemError;

use crate::error::CounterError;

#[cfg(unix)]
pub use self::unix::*;
Expand All @@ -12,3 +14,9 @@ pub use self::unix::*;
pub use self::win::*;

pub(super) mod counters;

impl From<SystemError> for CounterError {
fn from(error: SystemError) -> Self {
CounterError::Other(error.to_string())
}
}
9 changes: 0 additions & 9 deletions exe-unit/components/counters/src/os/process/mod.rs

This file was deleted.

8 changes: 1 addition & 7 deletions exe-unit/components/counters/src/os/unix.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::os::process::*;
use ya_utils_process::*;

use crate::error::CounterError;
use crate::Result;
Expand Down Expand Up @@ -104,9 +104,3 @@ impl Counters {
})
}
}

impl From<SystemError> for CounterError {
fn from(err: SystemError) -> Self {
Self::Other(err.to_string())
}
}
6 changes: 3 additions & 3 deletions exe-unit/components/counters/src/os/win.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::error::CounterError;
use crate::os::process::*;
use crate::Result;
use crate::{error::CounterError, Result};

use ya_utils_process::*;

use std::time::Duration;

Expand Down
2 changes: 1 addition & 1 deletion exe-unit/src/runtime/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ use tokio::process::Command;

use ya_agreement_utils::agreement::OfferTemplate;
use ya_client_model::activity::{CommandOutput, ExeScriptCommand};
use ya_counters::os::process::{kill, ProcessTree, SystemError};
use ya_manifest_utils::Feature;
use ya_runtime_api::server::{spawn, RunProcess, RuntimeControl, RuntimeService};
use ya_utils_process::{kill, ProcessTree, SystemError};

use crate::acl::Acl;
use crate::error::Error;
Expand Down
2 changes: 1 addition & 1 deletion golem_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ya-compile-time-utils = "0.2"
ya-core-model = { version = "^0.9", features=["payment", "version"] }
ya-provider = "0.3"
ya-utils-path = "0.1.0"
ya-utils-process = { version = "0.2", features = ["lock"] }
ya-utils-process = { version = "0.3", features = ["lock"] }

actix-rt="2.7"
ansi_term="0.12.1"
Expand Down
2 changes: 1 addition & 1 deletion test-utils/test-framework/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ serial_test = { git = "https://github.com/golemfactory/serial_test.git", branch
tokio = { version = "1", features = ["rt"] }
url = "2.3"

ya-utils-process = "0.2"
ya-utils-process = "0.3"
ya-framework-macro = "0.1"

[target.'cfg(target_family = "unix")'.dependencies]
Expand Down
18 changes: 14 additions & 4 deletions utils/process/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
name = "ya-utils-process"
version = "0.2.0"
version = "0.3.0"
authors = ["Golem Factory <contact@golem.network>"]
edition = "2018"
edition = "2021"
homepage = "https://github.com/golemfactory/yagna"
repository = "https://github.com/golemfactory/yagna"
license = "LGPL-3.0"
Expand All @@ -19,14 +19,24 @@ anyhow = "1.0"
derive_more = { workspace = true }
futures = "0.3"
futures-util = "0.3.4"
lazy_static = "1.4"
libc = "0.2"
log = "0.4"
shared_child = "0.3.4"
tokio = { version = "1", features = ["process", "signal"] }
thiserror = "1.0"

fs2 = { version = "0.4.3", optional = true }

[target.'cfg(target_family = "unix")'.dependencies]
nix = "0.22.0"
nix = "0.22"

[target.'cfg(target_family = "windows")'.dependencies]
winapi = "0.3"
winapi = { version = "0.3", features = [
"jobapi2",
"processthreadsapi",
] }


[target.'cfg(target_os = "macos")'.dependencies]
libproc = { version = "0.7" }
31 changes: 30 additions & 1 deletion utils/process/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ use std::time::Duration;
#[cfg(feature = "lock")]
pub mod lock;

#[cfg(unix)]
mod unix;
#[cfg(unix)]
pub use unix::*;

#[cfg(windows)]
mod win;
#[cfg(windows)]
pub use win::*;

#[cfg(unix)]
use shared_child::unix::SharedChildExt;

Expand All @@ -20,6 +30,16 @@ use winapi::um::{
wincon::{GenerateConsoleCtrlEvent, CTRL_BREAK_EVENT},
};

#[derive(Clone, Debug, thiserror::Error)]
pub enum ProcessError {
#[error("Unsupported: {0}")]
Unsupported(String),
#[error("Usage limit exceeded: {0}")]
UsageLimitExceeded(String),
#[error("Other error: {0}")]
Other(String),
}

pub trait ProcessGroupExt<T> {
fn new_process_group(&mut self) -> &mut T;
}
Expand Down Expand Up @@ -80,12 +100,21 @@ pub enum ExeUnitExitStatus {
#[derive(Clone)]
pub struct ProcessHandle {
process: Arc<SharedChild>,
// Windows process will be killed once `job_object` is dropped
#[allow(unused)]
#[cfg(windows)]
job_object: JobObject,
Comment on lines +104 to +106
Copy link
Contributor

Choose a reason for hiding this comment

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

Having conditionally existing field is hard to maintain. I'm wondering if we could have empty job object implementation for unix?
In general all these conditional compilation in process handling is hard to maintain, because we need to compile with different features to be sure it will compile... But it was already done like this before, so we can leave it as is if it would require to much work.

}

impl ProcessHandle {
pub fn new(command: &mut Command) -> Result<ProcessHandle> {
let process = Arc::new(SharedChild::spawn(command)?);
#[cfg(windows)]
let job_object = JobObject::try_new_current()?;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I left new_process_group unimplemented for Windows. process_group is roughly equivalent of Windows JobObject, but new_process_group is implemented for Command and I needed to store job_object somewhere, I could not extend Command, so instead I just crate job_object while creating ProcessHandle.

Ok(ProcessHandle {
process: Arc::new(SharedChild::spawn(command)?),
process,
#[cfg(windows)]
job_object,
})
}

Expand Down
Loading
Loading