0

I'm not sure if this is technically a call for threading, but strangely enough in my many years of coding, I really haven't had to do this.

Scenario:

  1. API Method is called by a user to kickoff the processing of an order.
  2. The processing method we need to call next can take a really long time, so start that but we don't need any reply or acknowledgement if it has been completed, error, etc. as our logging process takes care of all that.
  3. However, user just needs to know the process has started, so send back a positive response, but kick off the other methods.

I think this is threading, but for the life of me I'm a bit unsure.

Indy-Jones
  • 668
  • 1
  • 7
  • 19
  • 1
    Yes this is called threading. If we're talking about web api here; having long running task in separate thread is not a great idea (there are inner limits of web server - max time it can spend processing the request & the thread can get aborted - also it exposes web server to DoS attack); better way is to store "kickoff" request into queue (db/cloud/xml file) and have separate batch to process the queue - this is "multiprocessing". – Ondrej Svejdar Feb 25 '16 at 17:04
  • Funny enough I actually proposed something similar at the beginning of the project very similar to this. Glad I wasn't totally off my rocker. – Indy-Jones Feb 25 '16 at 21:35

1 Answers1

1

Long running task in WebAPI

It seems like this may be an issue running your process in the background after sending a response.

"ASP.NET (and most other servers) work on the assumption that it's safe to tear down your service once all requests have completed."

As for your situation, what I would do is handle things on the front end and using javascript, jquery, etc, create an on-click event for the order button, and allow it to display an order submitted text or something to that effect.

Community
  • 1
  • 1
jerellm
  • 54
  • 4
  • I think you and Ondrej had similar advice in this instance. I made a similar suggestion at the beginning of the project and suggested a table of queued method calls which take a bit to process and wanted to put a service into place which would run so often. I'll ask this to both you and Ondrej....if you have a service which calls a process to kickoff these other ones, how often do you run it and how do you avoid kicking off the same one twice? – Indy-Jones Feb 25 '16 at 21:39
  • I'm assuming you'd have a date field which gets populated right away stating it has been started, and to grab the next one in the queue where this date is null. Correct? – Indy-Jones Feb 25 '16 at 21:39
  • I think you could have a date entered column and a date executed column. And then as you said, populate the date executed column as they are called and finished. You could order by the date entered to get them in the order they were requested. – jerellm Feb 26 '16 at 19:57