From 9cf74ba0a108305a357858733956567f17d018b5 Mon Sep 17 00:00:00 2001 From: Louis Date: Wed, 31 Jul 2024 00:04:25 +0700 Subject: [PATCH] chore: destroy dangling processes on uninstall --- cortex-js/uninstall.js | 50 ++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/cortex-js/uninstall.js b/cortex-js/uninstall.js index 1df056570..2c5b04807 100644 --- a/cortex-js/uninstall.js +++ b/cortex-js/uninstall.js @@ -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(); \ No newline at end of file +uninstall();