Skip to content

Commit 6d22a37

Browse files
committed
this should fix the orphan folders bug when manual storage check is disabled and the non writing sessions file
1 parent 16a0ed9 commit 6d22a37

2 files changed

Lines changed: 35 additions & 38 deletions

File tree

backend/src/server/api/services/service.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ module.exports = {
574574
logger.debug(decodeOptions)
575575
logger.debug(map)
576576
res.dmm.outputFile = await decodeHandler.handleDecode(source, map, dataModel, schema, NGSI_entity, minioObj, config, res, decodeOptions, id)
577-
utils.printFinalReportAndSendResponse(logger, null, config, res)//TODO test this
577+
await utils.printFinalReportAndSendResponse(logger, null, config, res)//TODO test this
578578
//res.dmm.outputFile = res.dmm.outputFile[0]
579579
//res.dmm.deleteSession()
580580
}

backend/src/utils/utils.js

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const fs = require("fs");
3434
const Session = require('../server/api/models/session');
3535
const Output = require('../server/api/models/output.js')
3636
const mongoose = require("mongoose");
37+
const { JsonStreamStringify } = require('json-stream-stringify').default || require('json-stream-stringify');
3738

3839
function readDirRecursive(dir) {
3940
let results = [];
@@ -115,7 +116,7 @@ function dropOutput(id) {
115116
logger.debug(`Collection output${id} dropped successfully.`)
116117
}
117118

118-
async function checkMaximumSpaceOverflow() {
119+
async function checkMaximumSpaceOverflow(ignoringOutputId) {
119120

120121
let collections = await mongoose.connection.db.listCollections().toArray();
121122
const db = mongoose.connection.db;
@@ -248,25 +249,28 @@ async function checkMaximumSpaceOverflow() {
248249
folder = file.path
249250
else
250251
folder = getFolderFromSession(file.path)
251-
logger.debug(`Deleting file ${file.path} and folder ${folder} for cleanup...`)
252-
fs.rmSync(folder, { recursive: true, force: true });
253-
logger.info(`File ${file.path} and folder ${folder} deleted.`);
254-
let orphanSession //"./output/" + folder.split("/").join("") + ".json"
255-
if (pathIsOutput(file.path))
256-
orphanSession = getSessionFromOutputPath(file.path)
257-
else if (file.type === "folder")
258-
orphanSession = getSessionFromOutputFolderPath(file.path)
259-
else
260-
orphanSession = file.path
261-
logger.debug(`Checking for orphan session with id ${orphanSession} linked to deleted file...`)
262-
if (fs.existsSync(orphanSession) && orphanSession !== "./output/results.json")
263-
try {
264-
fs.unlinkSync(orphanSession);
265-
} catch (error) {
266-
logger.error(`Error deleting orphan session file ${orphanSession}:`, error)
267-
}
268-
else
269-
logger.debug(`No orphan session file ${orphanSession} found for deleted file.`)
252+
if (!ignoringOutputId || (ignoringOutputId && !folder.includes(ignoringOutputId))) {
253+
logger.debug(`Deleting file ${file.path} and folder ${folder} for cleanup...`)
254+
fs.rmSync(folder, { recursive: true, force: true });
255+
logger.info(`File ${file.path} and folder ${folder} deleted.`);
256+
257+
let orphanSession //"./output/" + folder.split("/").join("") + ".json"
258+
if (pathIsOutput(file.path))
259+
orphanSession = getSessionFromOutputPath(file.path)
260+
else if (file.type === "folder")
261+
orphanSession = getSessionFromOutputFolderPath(file.path)
262+
else
263+
orphanSession = file.path
264+
logger.debug(`Checking for orphan session with id ${orphanSession} linked to deleted file...`)
265+
if (fs.existsSync(orphanSession) && orphanSession !== "./output/results.json")
266+
try {
267+
fs.unlinkSync(orphanSession);
268+
} catch (error) {
269+
logger.error(`Error deleting orphan session file ${orphanSession}:`, error)
270+
}
271+
else
272+
logger.debug(`No orphan session file ${orphanSession} found for deleted file.`)
273+
}
270274
//fs.unlinkSync(filePath);
271275
//logger.info(`File ${file.file} deleted.`);
272276
} catch (err) {
@@ -711,20 +715,13 @@ const sendOutput = async (config, res) => {
711715
if (res.dmm.source.data && res.dmm.source.url)
712716
res.dmm.source.data = undefined
713717
if (config.sessionLocation.filesystem) {
714-
const JsonStreamStringify = require('json-stream-stringify');
715-
716-
const writeStream = fs.createWriteStream('./output/output' + outputId + '.json');
717-
const jsonStream = new JsonStreamStringify(res.dmm);
718-
719-
jsonStream.pipe(writeStream);
720-
721-
writeStream.on('finish', () => {
722-
logger.debug('File output is created successfully.');
723-
outputDataTempWriting.value = 'File output is created successfully.';
724-
});
725-
726-
writeStream.on('error', (err) => {
727-
throw err;
718+
await new Promise((resolve, reject) => {
719+
const writeStream = fs.createWriteStream('./output/output' + outputId + '.json');
720+
const jsonStream = new JsonStreamStringify(res.dmm);
721+
jsonStream.pipe(writeStream);
722+
writeStream.on('finish', resolve);
723+
writeStream.on('error', reject);
724+
jsonStream.on('error', reject);
728725
});
729726
}
730727
if (config.sessionLocation.mongo)
@@ -735,10 +732,10 @@ const sendOutput = async (config, res) => {
735732
//logger.error(res.dmm)
736733
outputDataTempWriting.value = 'Error during output file creation.'
737734
}
738-
if (config.sessionLocation.filesystem)
739-
await finish(outputDataTempWriting)
735+
//if (config.sessionLocation.filesystem)
736+
// await finish(outputDataTempWriting)
740737
if (!config.manualCheckMaximumSpaceOverflow)
741-
await checkMaximumSpaceOverflow()
738+
await checkMaximumSpaceOverflow(outputId)
742739
//const deleteSession =
743740
logger.debug(res.dmm.outputFile[res.dmm.outputFile.length - 1])
744741
res.dmm.deleteSession()

0 commit comments

Comments
 (0)