Arthur Le Calvez
Arthur Le Calvez

Reputation: 433

How to notify HTTP client of the completion of a long task

I have a Node.js system that uploads a large number of objects to MongoDB and creates folders in dropbox for each object. This takes around 0.5 seconds per object. In situations therefore where i have many objects this could take up to around a minute. What i currently do is notify the client that the array of objects has been accepted using a 202 response code. However how do i then notify the client of completion a minute later.

app.post('/BulkAdd', function (req, res) {
    issues = []
    console.log(req.body)
    res.status(202).send({response:"Processing"});
    api_functions.bulkAdd(req.body).then( (failed, issues, success) => {
        console.log('done')
    })

});


bulkAdd: async function (req, callback) {
  let failed = []
  let issues = []
  let success = []
  i = 1

  await req.reduce((promise, audit) => {
    // return promise.then(_ => dropbox_functions.createFolder(audit.scanner_ui)
    let globalData;
  return promise.then(_ => this.add(audit)
      .then((data)=> {globalData = data; return dropbox_functions.createFolder(data.ui, data)}, (error)=> {failed.push({audit: audit, error: 'There was an error adding this case to the database'}); console.log(error)})
        .then((data)=>{console.log(data, globalData);return dropbox_functions.checkScannerFolderExists(audit.scanner_ui)},(error)=>{issues.push({audit: globalData, error: 'There was an error creating the case folder in dropbox'})})
         .then((data)=>{return dropbox_functions.moveFolder(audit.scanner_ui, globalData.ui)},(error)=>{issues.push({audit: globalData, error: 'No data folder was found so an empty one was created'}); return dropbox_functions.createDataFolder(globalData.ui)})
          .then(()=>success.push({audit:globalData}), issues.push({audit: globalData, error: 'Scanner folder found but items not moved'}))
    );
  }, Promise.resolve()).catch(error => {console.log(error)});
  return(failed, issues, success)

},

Upvotes: 1

Views: 542

Answers (1)

Atul Sharma
Atul Sharma

Reputation: 10740

Well the problem with making client request wait, is it will timeout after certain period or sometimes will show error with no response received.

What you can do is

 - Make client request to server to initiate the task, and return 200OK and keep doing your task on server. 
 - Now write a file on server after insertion of every object as status. 
 - Read the file from client every 5-10 sec to check if server has completed creating objects or not. 
 - Mean while your task is not completed on server, show status with completion percentage or some animation.

Or simply implement WebHook or WebSockets to maintain communication.

Upvotes: 1

Related Questions