Skip to content

Commit

Permalink
fix: fix mutli deamon run
Browse files Browse the repository at this point in the history
  • Loading branch information
akitaSummer committed Apr 22, 2024
1 parent aa05edb commit e28ef48
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,5 @@
},
"engines": {
"node": ">=14.19.1"
},
"rapidVersion": "0.0.1"
}
}
26 changes: 23 additions & 3 deletions packages/cli/lib/deamon.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ const runDeamon = async () => {
while (count < 10) {
const res = await checkDeamonAlive();
if (res) {
console.info('[rapid] rapid daemon is running.');
return true;
}
count++;
Expand All @@ -161,6 +162,20 @@ const killDeamon = async () => {
}
};

const clearMetadata = async () => {
try {
await fs.stat(metadataDir);
} catch (error) {
debug('delProject error: ', error);
return false;
}
const files = await fs.readdir(metadataDir);
for (let i = 0; i < files.length; i++) {
const projectName = files[i].split('.')[0];
await delProject(projectName);
}
};

const registerDeamon = async () => {
try {
await execa.command('killall -9 rapid_deamon');
Expand All @@ -176,6 +191,8 @@ const registerDeamon = async () => {

await fs.mkdir(deamonDir, { recursive: true });

await fs.mkdir(nydusdMnt, { recursive: true });

await fs.copyFile(path.join(__dirname, '../package.json'), path.join(deamonDir, 'package.json'));

const nydusConfigPath = path.join(deamonDir, 'nydus_config.json');
Expand Down Expand Up @@ -219,6 +236,8 @@ root:

deamonAutoLauncher.enable();

console.info('[rapid] register rapid daemon end.');

try {
const isEnabled = deamonAutoLauncher.isEnabled();
if (isEnabled) return;
Expand All @@ -230,8 +249,8 @@ root:

const initDeamon = async () => {
try {
const rapidVersion = require(path.join(__dirname, '../package.json')).deamonVersion;
const deamonVersion = require(path.join(deamonDir, './package.json')).deamonVersion;
const rapidVersion = require(path.join(__dirname, '../package.json')).rapidVersion;
const deamonVersion = require(path.join(deamonDir, './package.json')).rapidVersion;

if (compareVersions(deamonVersion, rapidVersion)) {
const err = '[rapid] rapid and deamon version not match';
Expand All @@ -249,9 +268,9 @@ const initDeamon = async () => {
await fs.mkdir(nydusdMnt, { recursive: true });

await fs.stat(destinationFilePath);
await runDeamon();
} catch (e) {
await registerDeamon();
} finally {
await runDeamon();
}
};
Expand All @@ -260,3 +279,4 @@ exports.initDeamon = initDeamon;
exports.delProject = delProject;
exports.addProject = addProject;
exports.killDeamon = killDeamon;
exports.clearMetadata = clearMetadata;
4 changes: 3 additions & 1 deletion packages/cli/lib/nydusd/nydusd_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const {
tarBucketsDir,
} = require('../constants');
const { wrapSudo, getWorkdir } = require('../util');
const { killDeamon } = require('../deamon');
const { killDeamon, clearMetadata } = require('../deamon');

// see all APIs at: https://github.com/dragonflyoss/image-service/blob/master/api/openapi/nydus-rs.yaml
const endpoint = 'http://unix/api/v1';
Expand Down Expand Up @@ -140,6 +140,7 @@ async function checkDaemon() {
// 优雅退出 nydusd daemon
async function exitDaemon() {
try {
await clearMetadata();
await killDeamon();
await urllib.request(`${daemonUrl}/exit`, {
method: 'PUT',
Expand All @@ -157,6 +158,7 @@ async function exitDaemon() {
// 强制杀掉进程
async function forceExitDaemon() {
try {
await clearMetadata();
await killDeamon();
await execa.command(`umount -f ${nydusdMnt}`);
await execa.command('killall -9 nydusd');
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@
"rapid",
"npm"
],
"author": "gemwuu <[email protected]> (https://github.com/gemwuu)"
"author": "gemwuu <[email protected]> (https://github.com/gemwuu)",
"rapidVersion": "0.0.1"
}

0 comments on commit e28ef48

Please sign in to comment.