Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to create REST API for long lasting tasks?

Suppose I have 2 servers.

The first is a service that provides some computations, which can last long time (minutes to hours).

The second server will use this service to have some data computed.

I'm trying to design a REST API for the first server and so far so good. But I'd like to hear some opinion on how to model notifications when the long lasting task is finished.

I considered 2 approaches so far:

  1. Polling - the second server will ask every now and then about the result.
  2. Callback - Second server will setup an uri for the first one to call after it is done. But this smells a bit in REST API.

What do you think?

like image 249
Kugel Avatar asked Sep 06 '10 15:09

Kugel


People also ask

Which platform is best for REST API?

Postman. “It is probably the most popular tool for testing your REST API. If you take a look at their website, you'll find that Postman is used by 5 million developers and over 100,000 businesses to access 130 million APIs each month. It is also feature rich and supports all stages of the REST API life cycle.


2 Answers

For your situation I would choose polling. When the second server makes the initial request to create the job on the first server, it should get a response that has the url of the eventual status page. The second server then polls that url every 5-15 minutes to check the status of the job. If the first server makes that url an RSS or Atom feed, then users could also point their RSS readers at the same url and find out themselves if the job is done. It's a real win when both people and machines can get information out of a single source.

like image 53
Hitesh Avatar answered Oct 05 '22 08:10

Hitesh


In addition to what I've already answered in this similar question, I'd suggest using the Atom Publishing Protocol for the notification (you could publish to your second server).

like image 30
Bruno Avatar answered Oct 05 '22 08:10

Bruno