From fb3f84235546a6bf2d07e87f43e1b4556e4de0f6 Mon Sep 17 00:00:00 2001 From: Ladd Hoffman Date: Tue, 30 Apr 2024 16:57:34 -0500 Subject: [PATCH] send http 202 accepted if import is taking a long time --- backend/src/api/import-from-ss.js | 11 ++++++++++- backend/src/api/index.js | 4 +++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/backend/src/api/import-from-ss.js b/backend/src/api/import-from-ss.js index 0f0ceb5..06c542f 100644 --- a/backend/src/api/import-from-ss.js +++ b/backend/src/api/import-from-ss.js @@ -201,11 +201,20 @@ module.exports = async (req, res) => { console.log(`importFromSS: author ${authorId}`); const papers = await fetchAuthorPapers(authorId); console.log('papers count:', papers.length); + + const earlyResponseTimeout = setTimeout(() => { + res.status(202).end(); + }); + const result = await Promise.mapSeries(papers, importPaper); + clearTimeout(earlyResponseTimeout); + if (result.length) { console.log(`Added posts for ${result.length} papers by author ${authorId}`); } - res.json(result); + if (!res.headersSent) { + res.json(result); + } } else { res.status(400).end(); } diff --git a/backend/src/api/index.js b/backend/src/api/index.js index 80096d2..536b7df 100644 --- a/backend/src/api/index.js +++ b/backend/src/api/index.js @@ -39,7 +39,9 @@ app.use((err, req, res, next) => { const status = err.response?.status ?? err.status ?? 500; const message = err.response?.data?.error ?? err.message; console.error(`error: ${message}`, err); - res.status(status).send(message); + if (!res.headersSent) { + res.status(status).send(message); + } next(); });