Skip to content

Commit

Permalink
fix: update uds
Browse files Browse the repository at this point in the history
  • Loading branch information
akitaSummer committed Jan 24, 2024
1 parent ddd89fd commit dbc2726
Show file tree
Hide file tree
Showing 9 changed files with 267 additions and 98 deletions.
3 changes: 1 addition & 2 deletions integration/fixtures/utils/pids.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class Pids {
`/usr/local/bin/go-nfsv4.*?${this.nodeModulesDir}`, 'i'
);
}
console.log('snapshot', snapshot);

for (const line of snapshot.split('\n')) {
if (overlayPattern.test(line)) {
Expand Down Expand Up @@ -61,4 +60,4 @@ class Pids {
}
}

exports.Pids = Pids;
exports.Pids = Pids;
36 changes: 24 additions & 12 deletions integration/index.2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,19 +225,31 @@ describe('test/index.v2.test.js', () => {
await assert.doesNotReject(fs.stat(path.join(cwd, 'node_modules/esbuild')));
assert.strictEqual(require(path.join(cwd, 'node_modules', 'esbuild/package.json')).version, '0.15.14');

const pidsInstance = new Pids('esbuild/node_modules');
let pids = await pidsInstance.getPids();
assert(pids.length > 0);
for (const pid of pids) {
await execa.command(`kill -9 ${pid}`);
if (process.platform === 'linux') {
await execa.command(`umount -f ${dirs}`);
await new Promise(resolve => {
setTimeout(() => {
resolve();
}, 10000);
});
}
await new Promise(resolve => {
setTimeout(() => {
resolve();
}, 10000);
});
pids = await pidsInstance.getPids();
assert(pids.length > 0);

if (process.platform === 'darwin') {
const pidsInstance = new Pids('esbuild/node_modules');
let pids = await pidsInstance.getPids();
assert(pids.length > 0);
for (const pid of pids) {
await execa.command(`kill -9 ${pid}`);
}
await new Promise(resolve => {
setTimeout(() => {
resolve();
}, 10000);
});
pids = await pidsInstance.getPids();
assert(pids.length > 0);
}
assert.strictEqual(require(path.join(cwd, 'node_modules', 'esbuild/package.json')).version, '0.15.14');
});
});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"version": "lerna version --conventional-commits --sign-git-commit --sign-git-tag --no-push --no-private"
},
"devDependencies": {
"lerna": "^7.1.4",
"@eggjs/tsconfig": "^1.0.0",
"@types/mocha": "^8.2.0",
"@types/node": "^18.16.3",
Expand All @@ -38,6 +37,7 @@
"eslint-config-egg": "^12.0.0",
"espower-typescript": "^9.0.2",
"intelli-espower-loader": "^1.0.1",
"lerna": "^7.1.4",
"mm": "^2.2.0",
"mocha": "^8.2.1",
"nyc": "^15.1.0",
Expand Down
10 changes: 5 additions & 5 deletions packages/cli/lib/deamon.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const deamonDir = path.join(baseRapidModeDir(), 'project');

const metadataDir = path.join(deamonDir, 'metadata');

// const deamonSocketPath = path.join(deamonDir, 'socket_path');
const deamonSocketPath = path.join(deamonDir, 'socket_path');

const rapidDeamon = rsBindingPath
? path.join(rsBindingPath, 'rapid_deamon')
Expand All @@ -38,7 +38,7 @@ const checkDeamonAlive = async () => {
try {
const result = await urllib.request(`${aliveUrl}`, {
method: 'GET',
// socketPath: deamonSocketPath,
socketPath: deamonSocketPath,
timeout: 1000,
});
return result.status === 200;
Expand Down Expand Up @@ -73,7 +73,7 @@ const delProject = async projectName => {
data: { projectPath: config.projectPath },
dataType: 'json',
contentType: 'json',
// socketPath: deamonSocketPath,
socketPath: deamonSocketPath,
});
return result.status === 200 && result.data?.code === 0;
} catch (error) {
Expand All @@ -91,7 +91,7 @@ const addProject = async config => {
data: config,
dataType: 'json',
contentType: 'json',
// socketPath: deamonSocketPath,
socketPath: deamonSocketPath,
});
return result.status === 200 && result.data?.code === 0;
} catch (_) {
Expand Down Expand Up @@ -124,7 +124,7 @@ const killDeamon = async () => {
try {
const result = await urllib.request(`${killUrl}`, {
method: 'GET',
// socketPath: deamonSocketPath,
socketPath: deamonSocketPath,
});
return result.status === 200;
} catch (_) {
Expand Down
6 changes: 4 additions & 2 deletions packages/deamon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ log4rs = "1"
axum = "0.7"
http-body-util = "0.1"
hyper = { version = "0.14", features = ["full"] }
hyper_v1 = { package = "hyper", version = "1", features = ["full"] }
hyperlocal = "0.8"
hyper-util = { version = "0.1.2", features = [
hyper-util = { version = "0.1", features = [
"client",
"client-legacy",
"http1",
"server-auto",
"tokio",
] }
httparse = "1.8"
tower = { version = "0.4", features = ["util"] }

[dev-dependencies]
tower = { version = "0.4", features = ["util"] }
mime = "0.3"
102 changes: 84 additions & 18 deletions packages/deamon/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,12 @@ pub struct Overlay {
}

impl Overlay {
#[cfg(target_os = "macos")]
pub fn get_pids(&self) -> Result<Vec<u32>> {
let mut pids = vec![];

let snapshot = get_ps_snapshot()?;

#[cfg(target_os = "linux")]
let overlay_pattern = Regex::new(&format!(r#"(?i)overlay.*?{}"#, self.node_modules_dir))?;

#[cfg(target_os = "macos")]
let unionfs = match &self.unionfs {
Some(s) => s,
Expand All @@ -165,7 +163,7 @@ impl Overlay {
))
}
};
#[cfg(target_os = "macos")]

let overlay_pattern =
Regex::new(&format!(r#"(?i){}.*?{}"#, unionfs, self.node_modules_dir))?;

Expand All @@ -183,13 +181,11 @@ impl Overlay {
}
}

#[cfg(target_os = "macos")]
let nfs_pattern = Regex::new(&format!(
r#"(?i)/usr/local/bin/go-nfsv4.*?{}"#,
self.node_modules_dir
))?;

#[cfg(target_os = "macos")]
for line in snapshot.clone().lines() {
if nfs_pattern.is_match(line) {
let fields: Vec<&str> = line.split_whitespace().collect();
Expand All @@ -206,14 +202,18 @@ impl Overlay {

Ok(pids)
}

#[cfg(target_os = "linux")]
pub fn restart(&self) -> Result<Vec<u32>> {
#[cfg(target_os = "linux")]
let unmount_modules_str = format!(r#"umount -f {}"#, self.node_modules_dir);

let _ = start_command(&unmount_modules_str);

let unmount_overlay_str = format!(r#"umount -f {}"#, self.overlay);

let _ = start_command(&unmount_overlay_str);

let tmp_str = format!(r#"mount -t tmpfs tmpfs {}"#, self.overlay);
#[cfg(target_os = "macos")]
let tmp_str = format!(
r#"hdiutil attach -nobrowse -mountpoint {} {}"#,
self.overlay, self.tmp_dmg
);

match start_command(&tmp_str) {
Ok(output) => {
Expand Down Expand Up @@ -241,7 +241,6 @@ impl Overlay {
}
}

#[cfg(target_os = "linux")]
let workdir = match &self.workdir {
Some(s) => s,
None => {
Expand All @@ -251,12 +250,72 @@ impl Overlay {
))
}
};
#[cfg(target_os = "linux")]

let mount_str = format!(
r#"mount -t overlay overlay -o lowerdir={},upperdir={},workdir={} {}"#,
self.mnt, self.upper, workdir, self.node_modules_dir
);
#[cfg(target_os = "macos")]
match start_command(&mount_str) {
Ok(output) => {
if output.status.success() {
info!(
"Overlay restart executed successfully, mountpoint: {:?}",
self.node_modules_dir
);
} else {
return Err(anyhow!(
"Error executing Overlay restart: {:?}, mountpoint: {:?}",
output.status,
self.node_modules_dir
));
}
}
Err(e) => {
return Err(anyhow!(
"Error executing Overlay restart command: {:?}, mountpoint: {:?}",
e,
self.node_modules_dir
));
}
}

let res = vec![];
Ok(res)
}

#[cfg(target_os = "macos")]
pub fn restart(&self) -> Result<Vec<u32>> {
let tmp_str = format!(
r#"hdiutil attach -nobrowse -mountpoint {} {}"#,
self.overlay, self.tmp_dmg
);

match start_command(&tmp_str) {
Ok(output) => {
if output.status.success() {
info!(
"Overlay restart executed successfully, mountpoint: {:?}, tmp_str: {:?}",
self.node_modules_dir, tmp_str
);
} else {
return Err(anyhow!(
"Error executing Overlay restart: {:?}, mountpoint: {:?}, tmp_str: {:?}",
output.status,
self.node_modules_dir,
tmp_str
));
}
}
Err(e) => {
return Err(anyhow!(
"Error executing Overlay restart command: {:?}, mountpoint: {:?}, tmp_str: {:?}",
e,
self.node_modules_dir,
tmp_str
));
}
}

let unionfs = match &self.unionfs {
Some(s) => s,
None => {
Expand All @@ -266,7 +325,7 @@ impl Overlay {
))
}
};
#[cfg(target_os = "macos")]

let mount_str = format!(
r#"{} -o cow,max_files=32768 -o allow_other,use_ino,suid,dev,nobrowse {}=RW:{}=RO {}"#,
unionfs, self.upper, self.mnt, self.node_modules_dir
Expand Down Expand Up @@ -296,7 +355,8 @@ impl Overlay {
}
}

Ok(self.get_pids()?)
let res = self.get_pids()?;
Ok(res)
}
}

Expand Down Expand Up @@ -373,6 +433,7 @@ impl ProjectConfig {
pub fn get_pids(&self) -> Result<Vec<u32>> {
let mut pids = vec![];

#[cfg(target_os = "macos")]
for overlay in self.overlays.iter() {
let ps = overlay.get_pids()?;
pids.extend(ps);
Expand Down Expand Up @@ -472,7 +533,12 @@ impl NydusConfig {
info!("init_daemon executed successfully");
return Ok(());
} else {
error!("Error executing init_daemon: {:?}", output.status);
error!(
"Error executing init_daemon, status: {:?}, stdout: {:?}, stderr: {:?}",
output.status,
std::str::from_utf8(&output.stdout)?,
std::str::from_utf8(&output.stderr)?,
);
}
}
Err(e) => error!("Error executing init_daemon: {:?}", e),
Expand Down
23 changes: 16 additions & 7 deletions packages/deamon/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extern crate lazy_static;
use anyhow::Result;
use config::{process_json_files_in_folder, NydusConfig};
use homedir::get_my_home;
use log::error;
use pid::{check_projects, init_projects};
use server::start_server;
use tokio::{select, sync::mpsc};
Expand Down Expand Up @@ -46,15 +47,21 @@ async fn main() {
.unwrap()
.join(".rapid/cache/project/metadata/");

let _ = setup_logger();

create_folder_if_not_exists(metadata_dir.to_str().unwrap()).unwrap();

let socket_path = get_my_home()
.unwrap()
.unwrap()
.join(".rapid/cache/project/socket_path");

let _ = setup_logger();

let configs = process_json_files_in_folder(metadata_dir.to_str().unwrap())
.await
.unwrap();

let nydus = NydusConfig::new().await;

let _ = nydus.init_daemon();

let project_tree = init_projects(configs).await.unwrap();
Expand All @@ -66,20 +73,22 @@ async fn main() {
std::thread::spawn(move || {
tokio::runtime::Runtime::new()
.unwrap()
.block_on(start_server(project_tree.clone(), sender));
.block_on(start_server(project_tree.clone(), sender, socket_path));
});
}

println!("deamon main is ready");

loop {
select! {
_ = receiver.recv() => {
return;
}
_ = tokio::time::sleep(tokio::time::Duration::from_secs(5)) => {
let _ = nydus.init_daemon();
let _ = check_projects(project_tree.clone()).await;
if let Err(e) = nydus.init_daemon() {
error!("init_daemon err: {}", e);
};
if let Err(e) = check_projects(project_tree.clone()).await {
error!("check_projects err: {}", e);
}
}
}
}
Expand Down
Loading

0 comments on commit dbc2726

Please sign in to comment.