0

So what I need is a way to let node calculate this huge thing I want it to calculate and immediately return 202 Accepted, and the status of the calculation is aksed via the Poller. Any ideas?

I have already such scenario in my Node/Express Endpoints:

module.export.poller = (req,res) ->
  res.status(200).send({
    percentFinished: precentFinished
  })

module.export.beginHugeCalculation = (req,res) ->
  async.waterfall [
    (next) ->
    # very, very, very, very time-intensive calculation here
    next null
  ], (err) ->
    # Success
    return

  # Immediately return HTTP 202 after Async Waterfall is triggered!
  # Status can be requested via Poller Route

  res.status(202).send({
    inProgress  : true
  })
  return

This works as expected with unit tests in the backend, the async is triggered, then a 202 is returned and via the Poller API I can ask about the progress (which is calculated somewhere else). Point is, as soon as I do this from the frontEnd in manual testing, the beginHugeCaluclation is triggered and returns 202, and the poller requests I am sending each second are queued and return all at once only when async is finished, giving back 100% in every request.

Stephan Kristyn
  • 15,015
  • 14
  • 88
  • 147
  • To me it sounds like a **synchronous** task is blocking your thread. (which supports the answer from @GiladBison). Something is preventing the `return` statement in a callback somewhere from happening until the calculation is complete. – Kevin B Nov 19 '15 at 17:01

1 Answers1

2

As far as I understand your question, the behavior of your app does make sense.

Because the large calculation you are describing is CPU-based, and node runs on a single thread, the event loop gets stuck and so, doesn`t handle new requests.

In order for other requests to be processed while a large calculation is taking place, you can either set up a cluster, or, if the calculation is recursive, use process.nexttick or setImmediate: setImmediate vs. nextTick

Please let me know if I`m missing anything.

Community
  • 1
  • 1
gibson
  • 1,066
  • 2
  • 10
  • 22
  • 1
    That`s actually a great question. In case we are missing something, can you upload the test and method code? – gibson Nov 19 '15 at 17:02
  • do u have a suggestion regarding polling solutions on MEAN? – Stephan Kristyn Nov 20 '15 at 08:56
  • Actually no, sorry, I`m not experienced in that field. If you want me to help you with this specific issue, you should attach some code. Otherwise, maybe post a different question about polling solutions in general. – gibson Nov 22 '15 at 11:02