Skip to content
This repository was archived by the owner on Jan 27, 2026. It is now read-only.

Commit d0a17e7

Browse files
authored
add ability to customize the socket path env var (#467)
* add ability to customize the socket path env var
1 parent 0437299 commit d0a17e7

File tree

1 file changed

+79
-6
lines changed

1 file changed

+79
-6
lines changed

crates/worker/src/docker/service.rs

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,19 +194,20 @@ impl DockerService {
194194
let cmd = match payload.cmd {
195195
Some(cmd_vec) => {
196196
cmd_vec.into_iter().map(|arg| {
197-
if let Some(seed) = p2p_seed {
198-
arg.replace("${WORKER_P2P_SEED}", &seed.to_string())
199-
} else {
200-
arg
201-
}
197+
// Replace ${SOCKET_PATH}
198+
arg.replace("${SOCKET_PATH}", &task_bridge_socket_path)
202199
}).collect()
203200
}
204201
None => vec!["sleep".to_string(), "infinity".to_string()],
205202
};
206203

207204
let mut env_vars: HashMap<String, String> = HashMap::new();
208205
if let Some(env) = &payload.env_vars {
209-
env_vars.extend(env.clone());
206+
// Clone env vars and replace ${SOCKET_PATH} in values
207+
for (key, value) in env.iter() {
208+
let processed_value = value.replace("${SOCKET_PATH}", &task_bridge_socket_path);
209+
env_vars.insert(key.clone(), processed_value);
210+
}
210211
}
211212

212213
env_vars.insert("NODE_ADDRESS".to_string(), node_address);
@@ -417,4 +418,76 @@ mod tests {
417418
tokio::time::sleep(Duration::from_secs(2)).await;
418419
cancellation_token.cancel();
419420
}
421+
422+
#[tokio::test]
423+
#[serial_test::serial]
424+
async fn test_socket_path_variable_replacement() {
425+
let cancellation_token = CancellationToken::new();
426+
let test_socket_path = "/custom/socket/path.sock";
427+
let docker_service = DockerService::new(
428+
cancellation_token.clone(),
429+
None,
430+
Some(1024),
431+
test_socket_path.to_string(),
432+
"/tmp/test-storage".to_string(),
433+
Address::ZERO.to_string(),
434+
Some(12345), // p2p_seed for testing
435+
);
436+
437+
// Test command argument replacement
438+
let task_with_cmd = Task {
439+
image: "ubuntu:latest".to_string(),
440+
name: "test_cmd_replacement".to_string(),
441+
id: Uuid::new_v4(),
442+
cmd: Some(vec!["echo".to_string(), "${SOCKET_PATH}".to_string()]),
443+
env_vars: None,
444+
entrypoint: None,
445+
state: TaskState::PENDING,
446+
created_at: Utc::now().timestamp_millis(),
447+
..Default::default()
448+
};
449+
450+
// Test environment variable replacement
451+
let task_with_env = Task {
452+
image: "ubuntu:latest".to_string(),
453+
name: "test_env_replacement".to_string(),
454+
id: Uuid::new_v4(),
455+
cmd: None,
456+
env_vars: Some(HashMap::from([
457+
("MY_SOCKET_PATH".to_string(), "${SOCKET_PATH}".to_string()),
458+
(
459+
"CUSTOM_PATH".to_string(),
460+
"prefix_${SOCKET_PATH}_suffix".to_string(),
461+
),
462+
("NORMAL_VAR".to_string(), "no_replacement".to_string()),
463+
])),
464+
entrypoint: None,
465+
state: TaskState::PENDING,
466+
created_at: Utc::now().timestamp_millis(),
467+
..Default::default()
468+
};
469+
470+
// Set tasks and verify state
471+
docker_service
472+
.state
473+
.set_current_task(Some(task_with_cmd.clone()))
474+
.await;
475+
assert_eq!(
476+
docker_service.state.get_current_task().await.unwrap().name,
477+
task_with_cmd.name
478+
);
479+
480+
docker_service
481+
.state
482+
.set_current_task(Some(task_with_env.clone()))
483+
.await;
484+
assert_eq!(
485+
docker_service.state.get_current_task().await.unwrap().name,
486+
task_with_env.name
487+
);
488+
489+
// Note: We can't easily test the actual replacement in container start
490+
// without mocking DockerManager, but we've verified the logic visually
491+
cancellation_token.cancel();
492+
}
420493
}

0 commit comments

Comments
 (0)