1. Home
  2. API
  3. Background processing

Background processing

Problem:

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

Solution:

We have 2 options to fix those problems: background processing and asynchronous processing. In case of asynchronous processing the request sent to the queue for further processing. This is not implemented yet.

Background processing means API continue processing of this request but respond instantly with request id and user can periodically check status of this request.

Implementation:

Any API request can be converted to background one by addings bgrs parameter equal to ‘yes’ or 1. In that case API instantly respond with background request id but continue processing of current request. E.g. for request https://apidev.bear2b.com/v2.5/users/me?bgrs=yes, we get:

{"request": "4ff7cc2d88b1de88e83cce3f1caa1509"}

To get general information about background request following call to be used: https://apidev.bear2b.com/bgrs/4ff7cc2d88b1de88e83cce3f1caa1509

[{
  "id": "267",
  "rid": "4ff7cc2d88b1de88e83cce3f1caa1509",
  "user_id": "1",
  "name": "/v2.5/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 to be used: https://apidev.bear2b.com/bgrs/4ff7cc2d88b1de88e83cce3f1caa1509/details

[{
  "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://apidev.bear2b.com/bgrs/4ff7cc2d88b1de88e83cce3f1caa1509/result Actual result depends on the original call – so in our case it will be json. For call like https://apidev.bear2b.com/v2.5/users/me?t=csv&bgrs=yes it will be csv file: https://apidev.bear2b.com/bgrs/d76e303325fd777039c7eabd6d25bc68/result

API doesn’t check rights to data provided by bgrs/BGR_ID/result. Caller only should provide ANY valid BEAR API credentials. That means you can share results taken by you with any BEAR user.

Was this article helpful to you? Yes No

How can we help?