Skip to content

Commit

Permalink
chore: destroy dangling processes on uninstall (#945)
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-menlo authored Jul 31, 2024
1 parent 0511475 commit 979be8b
Showing 1 changed file with 29 additions and 21 deletions.
50 changes: 29 additions & 21 deletions cortex-js/uninstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,38 @@ const os = require('os');
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
const constants = require('./dist/src/infrastructure/constants/cortex.js')

const uninstall = async () => {
const cortexConfigPath = path.join(os.homedir(), '.cortexrc');
if (fs.existsSync(cortexConfigPath)) {
const content = await promises.readFile(cortexConfigPath, 'utf8');
const config = yaml.load(content);
if(!config) {
return;
}
const { dataFolderPath } = config;
const modelsFolderPath = path.join(dataFolderPath, 'models');
// remove all data in data folder path except models
const files = fs.readdirSync(dataFolderPath);
for (const file of files) {
const fileStat = fs.statSync(path.join(dataFolderPath, file));
if (file !== 'models') {
if (fileStat.isDirectory()) {
fs.rmSync(path.join(dataFolderPath, file), { recursive: true });
} else {
fs.unlinkSync(path.join(dataFolderPath, file));
}
}
const cortexConfigPath = path.join(os.homedir(), '.cortexrc');
if (fs.existsSync(cortexConfigPath)) {
const content = await promises.readFile(cortexConfigPath, 'utf8');
const config = yaml.load(content);
if(!config) {
return;
}
const { dataFolderPath, cortexCppHost, cortexCppPort, apiServerHost, apiServerPort } = config;

await fetch(constants.CORTEX_CPP_PROCESS_DESTROY_URL(cortexCppHost, cortexCppPort),
{ method: 'DELETE' })
.catch(() => {})
await fetch(constants.CORTEX_JS_SYSTEM_URL(apiServerHost, apiServerPort),
{ method: 'DELETE' })
.catch(() => {})
// remove all data in data folder path except models
const modelsFolderPath = path.join(dataFolderPath, 'models');
const files = fs.readdirSync(dataFolderPath);
for (const file of files) {
const fileStat = fs.statSync(path.join(dataFolderPath, file));
if (file !== 'models') {
if (fileStat.isDirectory()) {
fs.rmSync(path.join(dataFolderPath, file), { recursive: true });
} else {
fs.unlinkSync(path.join(dataFolderPath, file));
}
}
}
}
};

uninstall();
uninstall();

0 comments on commit 979be8b

Please sign in to comment.