Skip to content

Commit

Permalink
Merge pull request #64 from andyquinterom/T63
Browse files Browse the repository at this point in the history
feat: Adds FAUCET_WORKER_ID environment variable
  • Loading branch information
pierina-ixpantia authored Jun 19, 2024
2 parents 007185f + d998f02 commit 96c3376
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/client/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ fn spawn_child_rscript_process(
rscript: impl AsRef<OsStr>,
workdir: impl AsRef<Path>,
command: impl AsRef<str>,
worker_id: usize,
) -> FaucetResult<Child> {
tokio::process::Command::new(rscript)
// Set the current directory to the directory containing the entrypoint
Expand All @@ -69,7 +70,7 @@ fn spawn_child_rscript_process(
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
// Set the port environment variable `PORT` to the port we want to use
.env("FAUCET_WORKER_ID", worker_id.to_string())
// This is needed to make sure the child process is killed when the parent is dropped
.kill_on_drop(true)
.spawn()
Expand All @@ -81,14 +82,15 @@ fn spawn_plumber_worker(
workdir: impl AsRef<Path>,
port: u16,
target: &'static str,
worker_id: usize,
) -> FaucetResult<Child> {
let command = format!(
r#"
options("plumber.port" = {port})
plumber::pr_run(plumber::plumb())
"#,
);
let child = spawn_child_rscript_process(rscript, workdir, command)?;
let child = spawn_child_rscript_process(rscript, workdir, command, worker_id)?;

log_stdio(child, target)
}
Expand All @@ -98,14 +100,15 @@ fn spawn_shiny_worker(
workdir: impl AsRef<Path>,
port: u16,
target: &'static str,
worker_id: usize,
) -> FaucetResult<Child> {
let command = format!(
r#"
options("shiny.port" = {port})
shiny::runApp()
"#,
);
let child = spawn_child_rscript_process(rscript, workdir, command)?;
let child = spawn_child_rscript_process(rscript, workdir, command, worker_id)?;

log_stdio(child, target)
}
Expand All @@ -117,10 +120,11 @@ impl WorkerType {
workdir: impl AsRef<Path>,
port: u16,
target: &'static str,
worker_id: usize,
) -> Child {
let child_result = match self {
WorkerType::Plumber => spawn_plumber_worker(rscript, workdir, port, target),
WorkerType::Shiny => spawn_shiny_worker(rscript, workdir, port, target),
WorkerType::Plumber => spawn_plumber_worker(rscript, workdir, port, target, worker_id),
WorkerType::Shiny => spawn_shiny_worker(rscript, workdir, port, target, worker_id),
};
match child_result {
Ok(child) => child,
Expand Down Expand Up @@ -158,11 +162,12 @@ fn spawn_worker_task(
workdir: Arc<Path>,
is_online: Arc<AtomicBool>,
target: &'static str,
id: usize,
) -> JoinHandle<FaucetResult<()>> {
tokio::spawn(async move {
let port = addr.port();
loop {
let mut child = worker_type.spawn_process(&rscript, &workdir, port, target);
let mut child = worker_type.spawn_process(&rscript, &workdir, port, target, id);
let pid = child.id().expect("Failed to get plumber worker PID");
log::info!(target: "faucet", "Starting process {pid} for {target} on port {port}");
loop {
Expand Down Expand Up @@ -207,6 +212,7 @@ impl Worker {
workdir,
is_online.clone(),
target,
id,
);
Ok(Self {
_worker_task: worker_task,
Expand Down

0 comments on commit 96c3376

Please sign in to comment.