Skip to content

Commit

Permalink
Refactor createAndDeleteTempFile function and update logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubmieszczak committed May 20, 2024
1 parent 7a91d6a commit 891bbc6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
4 changes: 1 addition & 3 deletions api/server/routes/convos.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ router.post('/fork', async (req, res) => {
res.status(500).send('Error forking conversation');
}
});
//router.post('/export', importIpLimiter, importUserLimiter, async (req, res) => {
router.post('/export', async (req, res) => {
router.post('/export', importIpLimiter, importUserLimiter, async (req, res) => {
try {
const job = await jobScheduler.now(EXPORT_CONVERSATION_JOB_NAME, '', req.user.id);
res.status(200).json({ message: 'Export started', jobId: job.id });
Expand All @@ -184,7 +183,6 @@ router.post('/export', async (req, res) => {
}
});

// put this in a function
const jobStatusHandler = async (req, res) => {
try {
const { jobId } = req.params;
Expand Down
49 changes: 30 additions & 19 deletions api/server/utils/import/jobDefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,55 +40,66 @@ const importConversationJob = async (job, done) => {
}
};

async function createAndDeleteTempFile(content, delay, jobId) {
/**
* Create a temporary file and delete it after a delay.
* @param {object} content - The content to write to the file.
* @param {number} delay - The delay in milliseconds to delete the file.
* @param {string} job - The job object.
* @returns {Promise<string>} The temporary file path.
*/
async function createAndDeleteTempFile(content, delay, job) {
const { requestUserId } = job.attrs.data;
const tempDir = os.tmpdir();
const tempFilePath = path.join(tempDir, `export-${jobId}`);

const tempFilePath = path.join(tempDir, `export-${job.attrs._id}`);
try {
// Write content to the temporary file using fs.promises API
await fs.writeFile(tempFilePath, JSON.stringify(content));
console.log(`Temporary file created at: ${tempFilePath}`);

// Schedule the deletion of the temporary file
logger.debug(`user: ${requestUserId} | Created temporary file at: ${tempFilePath}`);
setTimeout(async () => {
try {
// Delete the file using fs.promises API
await fs.unlink(tempFilePath);
console.log(`Temporary file deleted: ${tempFilePath}`);
logger.debug(
`user: ${requestUserId} | Automatically deleted temporary file at: ${tempFilePath}`,
);
} catch (error) {
console.error('Error deleting the temporary file:', error);
logger.error(
`user: ${requestUserId} | Failed to automatically delete temporary file at: ${tempFilePath}`,
error,
);
}
}, delay);

return tempFilePath;
} catch (error) {
console.error('Error handling the temporary file:', error);
logger.error(
`user: ${requestUserId} | Error handling the temporary file: ${tempFilePath}`,
error,
);
}
}

// Define the export job function
/**
* Job definition for exporting all conversations.
* @param {import('agenda').Job} job - The job object.
* @param {Function} done - The done function.
*/
const exportConversationJob = async (job, done) => {
const { requestUserId } = job.attrs.data;
try {
const convos = await getAllConvos(requestUserId);
//const content = req.file.buffer.toString();
//const job = await jobScheduler.now(EXPORT_CONVERSATION_JOB_NAME, content, req.user.id);

logger.info('Convos: ' + JSON.stringify(convos));

for (let i = 0; i < convos.conversations.length; i++) {
const conversationId = convos.conversations[i].conversationId;
convos.conversations[i].messages = await getMessages({ conversationId });
}
createAndDeleteTempFile(convos, 5 * 60 * 1000, job.attrs._id);
// Temporary file will be deleted from server after 5 minutes
createAndDeleteTempFile(convos, 5 * 60 * 1000, job);
done();
} catch (error) {
logger.error('Failed to export conversation: ', error);
done(error);
}
};

// Call the jobScheduler.define function at startup
// Call the jobScheduler.define functions at startup
jobScheduler.define(IMPORT_CONVERSATION_JOB_NAME, importConversationJob);
jobScheduler.define(EXPORT_CONVERSATION_JOB_NAME, exportConversationJob);

Expand Down

0 comments on commit 891bbc6

Please sign in to comment.