Background processing

Sometimes we have long API operations that takes minutes or even hours to be done. In that case CDN returns error 503 (Service unavailable) to the client. Which is not true because it’s just a long operation.

Solution

API continue processing of a request but responds instantly with request id so a user can periodically check the status of the request.

Implementation

Any API request can be converted to background one by addings bgrs=yes. In that case API instantly respond with background request id but continue processing of current request. E.g. for request https://api.bear2b.com/v2.13/users/me?bgrs=yes, we get something like:

{"request": "4ff7cc2d88b1de88e83cce3f1caa1509"}
To get general information about background request following call to be used: https://api.bear2b.com/v2.13/bgrs/4ff7cc2d88b1de88e83cce3f1caa1509

Response:

{
  "id": 267,
  "rid": "4ff7cc2d88b1de88e83cce3f1caa1509",
  "user_id": 1,
  "name": "/v2.13/users/me?bgrs=yes",
  "created_dt": "2017-12-26 18:21:52",
  "status_id": 4,
  "status_name": "finished",
  "progress": 100.00,
  "finished_dt": "2017-12-26 18:21:52",
  "status": 200,
  "content_type": "application/json; charset=utf-8",
  "result": "media/keep1m.bgrs.4ff7cc2d88b1de88e83cce3f1caa1509.result.json"
}
To get details of the request (it can be a lot of records for long API calls; it’s not the case for our example call, though) following call can be used: https://api.bear2b.com/v2.13/bgrs/4ff7cc2d88b1de88e83cce3f1caa1509/details

Response:

{
  "id": 5802,
  "rid": "4ff7cc2d88b1de88e83cce3f1caa1509",
  "dt": "2017-12-26 18:21:52",
  "details": "done"
}
Result of the background call can be taken using this call: https://api.bear2b.com/v2.13/bgrs/4ff7cc2d88b1de88e83cce3f1caa1509/result Actual result depends on the original call – so in our case it will be json. For call like https://api.bear2b.com/v2.13/users/me?t=csv&bgrs=yes it will be csv file: https://api.bear2b.com/v2.13/bgrs/d76e303325fd777039c7eabd6d25bc68/result

API doesn’t check rights to data provided by bgrs/BGR_ID/result. That means you can share results taken by you with anyone.