From 9191e9d440dd44cd71ae53172eb843b16a584b0d Mon Sep 17 00:00:00 2001 From: huijieliu8 <91745551+metaphor987@users.noreply.github.com> Date: Tue, 10 Sep 2024 12:13:42 -0400 Subject: [PATCH 1/2] fix: send keep-alive messages at regular intervals --- src/controllers/timeEntryController.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/controllers/timeEntryController.js b/src/controllers/timeEntryController.js index 6e8d36596..37a0f362c 100644 --- a/src/controllers/timeEntryController.js +++ b/src/controllers/timeEntryController.js @@ -1373,8 +1373,15 @@ const timeEntrycontroller = function (TimeEntry) { const recalculateHoursByCategoryAllUsers = async function (req, res) { const session = await mongoose.startSession(); session.startTransaction(); + let keepAliveInterval; try { + res.setHeader('Content-Type', 'text/plain'); + res.setHeader('Transfer-Encoding', 'chunked'); + keepAliveInterval = setInterval(() => { + res.write('Processing... keep connection alive\n'); + }, 100 * 1000); // interval of 100 seconds + const userprofiles = await UserProfile.find({}, '_id').lean(); const recalculationPromises = userprofiles.map(async (userprofile) => { @@ -1385,13 +1392,21 @@ const timeEntrycontroller = function (TimeEntry) { await Promise.all(recalculationPromises); await session.commitTransaction(); - return res.status(200).send({ - message: 'finished the recalculation for hoursByCategory for all users', - }); + // return res.status(200).send({ + // message: 'finished the recalculation for hoursByCategory for all users', + // }); + clearInterval(keepAliveInterval); + res.write('finished the recalculation for hoursByCategory for all users\n'); + return res.end(); } catch (err) { await session.abortTransaction(); + if (keepAliveInterval) { + clearInterval(keepAliveInterval); + } logger.logException(err); - return res.status(500).send({ error: err.toString() }); + res.write(`error: ${err.toString()}\n`); + // return res.status(500).send({ error: err.toString() }); + return res.end(); } finally { session.endSession(); } From 50e6f193fb88e5f0447681c150e6cfb475c606ee Mon Sep 17 00:00:00 2001 From: huijieliu8 <91745551+metaphor987@users.noreply.github.com> Date: Tue, 10 Sep 2024 12:16:18 -0400 Subject: [PATCH 2/2] fix: update interval --- src/controllers/timeEntryController.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/controllers/timeEntryController.js b/src/controllers/timeEntryController.js index 37a0f362c..b8114633c 100644 --- a/src/controllers/timeEntryController.js +++ b/src/controllers/timeEntryController.js @@ -1380,7 +1380,7 @@ const timeEntrycontroller = function (TimeEntry) { res.setHeader('Transfer-Encoding', 'chunked'); keepAliveInterval = setInterval(() => { res.write('Processing... keep connection alive\n'); - }, 100 * 1000); // interval of 100 seconds + }, 150 * 1000); // interval of 150 seconds const userprofiles = await UserProfile.find({}, '_id').lean(); @@ -1392,9 +1392,6 @@ const timeEntrycontroller = function (TimeEntry) { await Promise.all(recalculationPromises); await session.commitTransaction(); - // return res.status(200).send({ - // message: 'finished the recalculation for hoursByCategory for all users', - // }); clearInterval(keepAliveInterval); res.write('finished the recalculation for hoursByCategory for all users\n'); return res.end(); @@ -1405,7 +1402,6 @@ const timeEntrycontroller = function (TimeEntry) { } logger.logException(err); res.write(`error: ${err.toString()}\n`); - // return res.status(500).send({ error: err.toString() }); return res.end(); } finally { session.endSession();