Skip to content

Commit

Permalink
extract async-job-request-runner from http
Browse files Browse the repository at this point in the history
  • Loading branch information
Eevert Saukkokoski committed Apr 14, 2015
1 parent d71e1d3 commit 38641b1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 47 deletions.
48 changes: 1 addition & 47 deletions src/http.coffee
Original file line number Diff line number Diff line change
@@ -1,53 +1,7 @@
buildRequest = require './http/build-request'
extractResponseBody = require './http/extract-response-body'
jobs = require './http/jobs'

###
Allow the server to respond with an async job by enabling the corresponding feature header
###
allowAsyncJobResponse = (requestOptions) ->
requestOptions.headers ?= {}
requestOptions.headers[jobs.ASYNC_JOB_FEATURE_HEADER] = true

###
Check a response for the signature of an async job
###
isAsyncJobResponse = (response) ->
(response.status is jobs.JOB_HTTP_STATUS) and (response.body?[jobs.JOB_ROOT_KEY]?.id?)

###
Given an async job response, mark a request as a monitor on the async job by setting a header
###
markAsAsyncJobMonitorRequest = (asyncJobResponse, requestOptions) ->
requestOptions.headers ?= {}
requestOptions.headers[jobs.JOB_ID_HEADER] = asyncJobResponse.body[jobs.JOB_ROOT_KEY].id

module.exports = (Promise) ->
Transaction = require('ag-transaction')(Promise)
requestRunner = require('./http/request-runner')(Promise, Transaction)

asyncJobRequestRunner = do ->
(method, path, options = {}) ->
allowAsyncJobResponse options
###
(f: () ->
TransactionRunner ((superagent.Response & asyncJob) | superagent.Response)
) -> TransactionRunner superagent.Response
###
retryUntilComplete = (f) ->
f().flatMapDone (response) ->
if !isAsyncJobResponse response
Transaction.unit response
else
markAsAsyncJobMonitorRequest response, options
retryUntilComplete f

# NOTE: Node will emit faux 'socket hang up' errors if request is built
# but never ran. Avoid this by starting from an empty transaction and
# creating the request only when we're actually going to run.
Transaction.empty.flatMapDone ->
retryUntilComplete ->
requestRunner buildRequest(method, path, options)
asyncJobRequestRunner = require('./http/async-job-request-runner')(Promise)

runRequest = (args...) ->
asyncJobRequestRunner(args...).run (t) ->
Expand Down
49 changes: 49 additions & 0 deletions src/http/async-job-request-runner.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
jobs = require './jobs'
buildRequest = require './build-request'

###
Allow the server to respond with an async job by enabling the corresponding feature header
###
allowAsyncJobResponse = (requestOptions) ->
requestOptions.headers ?= {}
requestOptions.headers[jobs.ASYNC_JOB_FEATURE_HEADER] = true

###
Check a response for the signature of an async job
###
isAsyncJobResponse = (response) ->
(response.status is jobs.JOB_HTTP_STATUS) and (response.body?[jobs.JOB_ROOT_KEY]?.id?)

###
Given an async job response, mark a request as a monitor on the async job by setting a header
###
markAsAsyncJobMonitorRequest = (asyncJobResponse, requestOptions) ->
requestOptions.headers ?= {}
requestOptions.headers[jobs.JOB_ID_HEADER] = asyncJobResponse.body[jobs.JOB_ROOT_KEY].id

module.exports = (Promise) ->
Transaction = require('ag-transaction')(Promise)
requestRunner = require('./request-runner')(Promise, Transaction)

asyncJobRequestRunner = do ->
(method, path, options = {}) ->
allowAsyncJobResponse options
###
(f: () ->
TransactionRunner ((superagent.Response & asyncJob) | superagent.Response)
) -> TransactionRunner superagent.Response
###
retryUntilComplete = (f) ->
f().flatMapDone (response) ->
if !isAsyncJobResponse response
Transaction.unit response
else
markAsAsyncJobMonitorRequest response, options
retryUntilComplete f

# NOTE: Node will emit faux 'socket hang up' errors if request is built
# but never ran. Avoid this by starting from an empty transaction and
# creating the request only when we're actually going to run.
Transaction.empty.flatMapDone ->
retryUntilComplete ->
requestRunner buildRequest(method, path, options)

0 comments on commit 38641b1

Please sign in to comment.