Skip to content

Commit

Permalink
feat: add nodeamon test
Browse files Browse the repository at this point in the history
  • Loading branch information
akitaSummer committed Jan 26, 2024
1 parent ed2cf09 commit 8958ea8
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 33 deletions.
26 changes: 26 additions & 0 deletions integration/index.2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,32 @@ describe('test/index.v2.test.js', () => {
await setTimeoutPromise(20000);
assert.strictEqual(require(path.join(cwd, 'node_modules', 'esbuild/package.json')).version, '0.15.14');
});

it('should not work', async () => {
cwd = path.join(__dirname, './fixtures/esbuild');
await coffee
.fork(rapid, [
'install',
'--ignore-scripts',
'--nodaemon',
], {
cwd,
})
.debug()
.expect('code', 0)
.end();

const dirs = await fs.readdir(path.join(cwd, 'node_modules'));
assert.strictEqual(dirs.filter(dir => dir.includes('esbuild')).length, 2);
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 nodeModulesDir = path.join(cwd, 'node_modules');

await execa.command(`umount -f ${nodeModulesDir}`);
await setTimeoutPromise(20000);

await assert.rejects(fs.stat(path.join(cwd, 'node_modules', 'esbuild/package.json')));
});
});

});
19 changes: 0 additions & 19 deletions packages/deamon/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,25 +180,6 @@ impl Overlay {
}
}

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

for line in snapshot.clone().lines() {
if nfs_pattern.is_match(line) {
let fields: Vec<&str> = line.split_whitespace().collect();
if fields.len() >= 11 {
let _user = fields[0].to_string();
let pid = fields[1].parse::<u32>().unwrap_or(0);
let _cpu = fields[2].to_string();
let _command = fields[10].to_string();

pids.push(pid);
}
}
}

Ok(pids)
}

Expand Down
20 changes: 9 additions & 11 deletions packages/deamon/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[macro_use]
extern crate lazy_static;

use anyhow::Result;
use anyhow::{anyhow, Result};
use config::{process_json_files_in_folder, NydusConfig};
use homedir::get_my_home;
use log::error;
Expand All @@ -16,19 +16,17 @@ mod server;
mod utils;

fn setup_logger() -> Result<()> {
let log_dir = get_my_home()
.unwrap()
.unwrap()
let log_dir = get_my_home()?
.ok_or(anyhow!("get log_dir my_home fail"))?
.join(".rapid/cache/project/logs/");

create_folder_if_not_exists(log_dir.to_str().unwrap()).unwrap();
create_folder_if_not_exists(log_dir.to_str().ok_or(anyhow!("log_dir to str fail"))?).unwrap();

let log_config_path = get_my_home()
.unwrap()
.unwrap()
let log_config_path = get_my_home()?
.ok_or(anyhow!("get log_config_path my_home fail"))?
.join(".rapid/cache/project/log4rs.yaml");

log4rs::init_file(log_config_path, Default::default()).unwrap();
log4rs::init_file(log_config_path, Default::default())?;

Ok(())
}
Expand Down Expand Up @@ -70,10 +68,10 @@ async fn main() {

{
let project_tree = project_tree.clone();
std::thread::spawn(move || {
tokio::spawn(async move {
tokio::runtime::Runtime::new()
.unwrap()
.block_on(start_server(project_tree.clone(), sender, socket_path));
.block_on(start_server(project_tree, sender, socket_path));
});
}

Expand Down
14 changes: 12 additions & 2 deletions packages/deamon/src/pid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ impl ProjectInfo {
);
}
Err(err) => {
error!("Failed to kill process with PID {}. Error: {:?}", pid, err);
return Err(anyhow!(
"Failed to kill process with PID {}. Error: {:?}",
pid,
err
));
}
}
}
Expand Down Expand Up @@ -153,7 +157,13 @@ async fn check_project(project: &mut ProjectInfo) -> Result<()> {
}
if !is_alive(&project.pids, project.config.get_project_path()).await {
info!("{:?} will be check", project.config.get_project_path());
project.kill_pids()?;
if let Err(e) = project.kill_pids() {
error!(
"project {} kill fail {}",
project.config.get_project_path(),
e
)
}
project.restart().await?;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/deamon/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use tower::Service;

use crate::{
config::ProjectConfig,
pid::{add_project, kill_projects, ProjectInfo},
pid::{add_project, ProjectInfo},
};

#[derive(Serialize, Deserialize)]
Expand Down

0 comments on commit 8958ea8

Please sign in to comment.