workflow-manager
Advanced tools
Comparing version 0.1.0 to 0.1.1
280
index.js
@@ -211,3 +211,3 @@ const async = require("async"); | ||
default: | ||
rejecter(new Error("Recieved unexpected statusCode " + response.statusCode)); | ||
rejecter(new Error("Received unexpected statusCode " + response.statusCode)); | ||
return; | ||
@@ -266,12 +266,10 @@ } | ||
const headers = {}; | ||
if (!params.workflowName) { | ||
rejecter(new Error("workflowName must be non-empty because it's a path parameter")); | ||
return; | ||
} | ||
const query = {}; | ||
query["workflowName"] = params.workflowName; | ||
if (span) { | ||
opentracing.inject(span, opentracing.FORMAT_TEXT_MAP, headers); | ||
span.logEvent("GET /jobs/{workflowName}"); | ||
span.logEvent("GET /jobs"); | ||
span.setTag("span.kind", "client"); | ||
@@ -282,3 +280,3 @@ } | ||
method: "GET", | ||
uri: this.address + "/jobs/" + params.workflowName + "", | ||
uri: this.address + "/jobs", | ||
json: true, | ||
@@ -327,3 +325,3 @@ timeout, | ||
default: | ||
rejecter(new Error("Recieved unexpected statusCode " + response.statusCode)); | ||
rejecter(new Error("Received unexpected statusCode " + response.statusCode)); | ||
return; | ||
@@ -337,5 +335,3 @@ } | ||
/** | ||
* @param {Object} params | ||
* @param {string} params.workflowName | ||
* @param [params.input] | ||
* @param input | ||
* @param {object} [options] | ||
@@ -353,3 +349,6 @@ * @param {number} [options.timeout] - A request specific timeout | ||
*/ | ||
startJobForWorkflow(params, options, cb) { | ||
startJobForWorkflow(input, options, cb) { | ||
const params = {}; | ||
params["input"] = input; | ||
if (!cb && typeof options === "function") { | ||
@@ -383,6 +382,2 @@ cb = options; | ||
const headers = {}; | ||
if (!params.workflowName) { | ||
rejecter(new Error("workflowName must be non-empty because it's a path parameter")); | ||
return; | ||
} | ||
@@ -393,3 +388,3 @@ const query = {}; | ||
opentracing.inject(span, opentracing.FORMAT_TEXT_MAP, headers); | ||
span.logEvent("PUT /jobs/{workflowName}"); | ||
span.logEvent("POST /jobs"); | ||
span.setTag("span.kind", "client"); | ||
@@ -399,4 +394,4 @@ } | ||
const requestOptions = { | ||
method: "PUT", | ||
uri: this.address + "/jobs/" + params.workflowName + "", | ||
method: "POST", | ||
uri: this.address + "/jobs", | ||
json: true, | ||
@@ -447,3 +442,3 @@ timeout, | ||
default: | ||
rejecter(new Error("Recieved unexpected statusCode " + response.statusCode)); | ||
rejecter(new Error("Received unexpected statusCode " + response.statusCode)); | ||
return; | ||
@@ -458,4 +453,4 @@ } | ||
* @param {Object} params | ||
* @param {string} params.workflowName | ||
* @param {string} params.jobId | ||
* @param params.reason | ||
* @param {object} [options] | ||
@@ -467,3 +462,3 @@ * @param {number} [options.timeout] - A request specific timeout | ||
* @returns {Promise} | ||
* @fulfill {Object} | ||
* @fulfill {undefined} | ||
* @reject {module:workflow-manager.Errors.BadRequest} | ||
@@ -474,3 +469,3 @@ * @reject {module:workflow-manager.Errors.NotFound} | ||
*/ | ||
GetJob(params, options, cb) { | ||
CancelJob(params, options, cb) { | ||
if (!cb && typeof options === "function") { | ||
@@ -504,6 +499,117 @@ cb = options; | ||
const headers = {}; | ||
if (!params.workflowName) { | ||
rejecter(new Error("workflowName must be non-empty because it's a path parameter")); | ||
if (!params.jobId) { | ||
rejecter(new Error("jobId must be non-empty because it's a path parameter")); | ||
return; | ||
} | ||
const query = {}; | ||
if (span) { | ||
opentracing.inject(span, opentracing.FORMAT_TEXT_MAP, headers); | ||
span.logEvent("DELETE /jobs/{jobId}"); | ||
span.setTag("span.kind", "client"); | ||
} | ||
const requestOptions = { | ||
method: "DELETE", | ||
uri: this.address + "/jobs/" + params.jobId + "", | ||
json: true, | ||
timeout, | ||
headers, | ||
qs: query, | ||
useQuerystring: true, | ||
}; | ||
requestOptions.body = params.reason; | ||
const retryPolicy = options.retryPolicy || this.retryPolicy || singleRetryPolicy; | ||
const backoffs = retryPolicy.backoffs(); | ||
let retries = 0; | ||
(function requestOnce() { | ||
request(requestOptions, (err, response, body) => { | ||
if (retries < backoffs.length && retryPolicy.retry(requestOptions, err, response, body)) { | ||
const backoff = backoffs[retries]; | ||
retries += 1; | ||
setTimeout(requestOnce, backoff); | ||
return; | ||
} | ||
if (err) { | ||
rejecter(err); | ||
return; | ||
} | ||
switch (response.statusCode) { | ||
case 200: | ||
resolver(); | ||
break; | ||
case 400: | ||
rejecter(new Errors.BadRequest(body || {})); | ||
return; | ||
case 404: | ||
rejecter(new Errors.NotFound(body || {})); | ||
return; | ||
case 500: | ||
rejecter(new Errors.InternalError(body || {})); | ||
return; | ||
default: | ||
rejecter(new Error("Received unexpected statusCode " + response.statusCode)); | ||
return; | ||
} | ||
}); | ||
}()); | ||
}); | ||
} | ||
/** | ||
* @param {string} jobId | ||
* @param {object} [options] | ||
* @param {number} [options.timeout] - A request specific timeout | ||
* @param {external:Span} [options.span] - An OpenTracing span - For example from the parent request | ||
* @param {module:workflow-manager.RetryPolicies} [options.retryPolicy] - A request specific retryPolicy | ||
* @param {function} [cb] | ||
* @returns {Promise} | ||
* @fulfill {Object} | ||
* @reject {module:workflow-manager.Errors.BadRequest} | ||
* @reject {module:workflow-manager.Errors.NotFound} | ||
* @reject {module:workflow-manager.Errors.InternalError} | ||
* @reject {Error} | ||
*/ | ||
GetJob(jobId, options, cb) { | ||
const params = {}; | ||
params["jobId"] = jobId; | ||
if (!cb && typeof options === "function") { | ||
cb = options; | ||
options = undefined; | ||
} | ||
return new Promise((resolve, reject) => { | ||
const rejecter = (err) => { | ||
reject(err); | ||
if (cb) { | ||
cb(err); | ||
} | ||
}; | ||
const resolver = (data) => { | ||
resolve(data); | ||
if (cb) { | ||
cb(null, data); | ||
} | ||
}; | ||
if (!options) { | ||
options = {}; | ||
} | ||
const timeout = options.timeout || this.timeout; | ||
const span = options.span; | ||
const headers = {}; | ||
if (!params.jobId) { | ||
@@ -518,3 +624,3 @@ rejecter(new Error("jobId must be non-empty because it's a path parameter")); | ||
opentracing.inject(span, opentracing.FORMAT_TEXT_MAP, headers); | ||
span.logEvent("GET /jobs/{workflowName}/{jobId}"); | ||
span.logEvent("GET /jobs/{jobId}"); | ||
span.setTag("span.kind", "client"); | ||
@@ -525,3 +631,3 @@ } | ||
method: "GET", | ||
uri: this.address + "/jobs/" + params.workflowName + "/" + params.jobId + "", | ||
uri: this.address + "/jobs/" + params.jobId + "", | ||
json: true, | ||
@@ -570,3 +676,3 @@ timeout, | ||
default: | ||
rejecter(new Error("Recieved unexpected statusCode " + response.statusCode)); | ||
rejecter(new Error("Received unexpected statusCode " + response.statusCode)); | ||
return; | ||
@@ -677,3 +783,3 @@ } | ||
default: | ||
rejecter(new Error("Recieved unexpected statusCode " + response.statusCode)); | ||
rejecter(new Error("Received unexpected statusCode " + response.statusCode)); | ||
return; | ||
@@ -791,3 +897,3 @@ } | ||
default: | ||
rejecter(new Error("Recieved unexpected statusCode " + response.statusCode)); | ||
rejecter(new Error("Received unexpected statusCode " + response.statusCode)); | ||
return; | ||
@@ -799,2 +905,116 @@ } | ||
} | ||
/** | ||
* @param {Object} params | ||
* @param [params.NewWorkflowRequest] | ||
* @param {string} params.name | ||
* @param {object} [options] | ||
* @param {number} [options.timeout] - A request specific timeout | ||
* @param {external:Span} [options.span] - An OpenTracing span - For example from the parent request | ||
* @param {module:workflow-manager.RetryPolicies} [options.retryPolicy] - A request specific retryPolicy | ||
* @param {function} [cb] | ||
* @returns {Promise} | ||
* @fulfill {Object} | ||
* @reject {module:workflow-manager.Errors.BadRequest} | ||
* @reject {module:workflow-manager.Errors.NotFound} | ||
* @reject {module:workflow-manager.Errors.InternalError} | ||
* @reject {Error} | ||
*/ | ||
updateWorkflow(params, options, cb) { | ||
if (!cb && typeof options === "function") { | ||
cb = options; | ||
options = undefined; | ||
} | ||
return new Promise((resolve, reject) => { | ||
const rejecter = (err) => { | ||
reject(err); | ||
if (cb) { | ||
cb(err); | ||
} | ||
}; | ||
const resolver = (data) => { | ||
resolve(data); | ||
if (cb) { | ||
cb(null, data); | ||
} | ||
}; | ||
if (!options) { | ||
options = {}; | ||
} | ||
const timeout = options.timeout || this.timeout; | ||
const span = options.span; | ||
const headers = {}; | ||
if (!params.name) { | ||
rejecter(new Error("name must be non-empty because it's a path parameter")); | ||
return; | ||
} | ||
const query = {}; | ||
if (span) { | ||
opentracing.inject(span, opentracing.FORMAT_TEXT_MAP, headers); | ||
span.logEvent("PUT /workflows/{name}"); | ||
span.setTag("span.kind", "client"); | ||
} | ||
const requestOptions = { | ||
method: "PUT", | ||
uri: this.address + "/workflows/" + params.name + "", | ||
json: true, | ||
timeout, | ||
headers, | ||
qs: query, | ||
useQuerystring: true, | ||
}; | ||
requestOptions.body = params.NewWorkflowRequest; | ||
const retryPolicy = options.retryPolicy || this.retryPolicy || singleRetryPolicy; | ||
const backoffs = retryPolicy.backoffs(); | ||
let retries = 0; | ||
(function requestOnce() { | ||
request(requestOptions, (err, response, body) => { | ||
if (retries < backoffs.length && retryPolicy.retry(requestOptions, err, response, body)) { | ||
const backoff = backoffs[retries]; | ||
retries += 1; | ||
setTimeout(requestOnce, backoff); | ||
return; | ||
} | ||
if (err) { | ||
rejecter(err); | ||
return; | ||
} | ||
switch (response.statusCode) { | ||
case 201: | ||
resolver(body); | ||
break; | ||
case 400: | ||
rejecter(new Errors.BadRequest(body || {})); | ||
return; | ||
case 404: | ||
rejecter(new Errors.NotFound(body || {})); | ||
return; | ||
case 500: | ||
rejecter(new Errors.InternalError(body || {})); | ||
return; | ||
default: | ||
rejecter(new Error("Received unexpected statusCode " + response.statusCode)); | ||
return; | ||
} | ||
}); | ||
}()); | ||
}); | ||
} | ||
}; | ||
@@ -801,0 +1021,0 @@ |
{ | ||
"name": "workflow-manager", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Minimal Workflow orchestrator for AWS Batch", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -13,6 +13,8 @@ <a name="module_workflow-manager"></a> | ||
* [.getJobsForWorkflow(workflowName, [options], [cb])](#module_workflow-manager--WorkflowManager+getJobsForWorkflow) ⇒ <code>Promise</code> | ||
* [.startJobForWorkflow(params, [options], [cb])](#module_workflow-manager--WorkflowManager+startJobForWorkflow) ⇒ <code>Promise</code> | ||
* [.GetJob(params, [options], [cb])](#module_workflow-manager--WorkflowManager+GetJob) ⇒ <code>Promise</code> | ||
* [.startJobForWorkflow(input, [options], [cb])](#module_workflow-manager--WorkflowManager+startJobForWorkflow) ⇒ <code>Promise</code> | ||
* [.CancelJob(params, [options], [cb])](#module_workflow-manager--WorkflowManager+CancelJob) ⇒ <code>Promise</code> | ||
* [.GetJob(jobId, [options], [cb])](#module_workflow-manager--WorkflowManager+GetJob) ⇒ <code>Promise</code> | ||
* [.newWorkflow(NewWorkflowRequest, [options], [cb])](#module_workflow-manager--WorkflowManager+newWorkflow) ⇒ <code>Promise</code> | ||
* [.getWorkflowByName(name, [options], [cb])](#module_workflow-manager--WorkflowManager+getWorkflowByName) ⇒ <code>Promise</code> | ||
* [.updateWorkflow(params, [options], [cb])](#module_workflow-manager--WorkflowManager+updateWorkflow) ⇒ <code>Promise</code> | ||
* _static_ | ||
@@ -88,3 +90,3 @@ * [.RetryPolicies](#module_workflow-manager--WorkflowManager.RetryPolicies) | ||
#### workflowManager.startJobForWorkflow(params, [options], [cb]) ⇒ <code>Promise</code> | ||
#### workflowManager.startJobForWorkflow(input, [options], [cb]) ⇒ <code>Promise</code> | ||
**Kind**: instance method of <code>[WorkflowManager](#exp_module_workflow-manager--WorkflowManager)</code> | ||
@@ -99,5 +101,24 @@ **Fulfill**: <code>Object</code> | ||
| --- | --- | --- | | ||
| input | | | | ||
| [options] | <code>object</code> | | | ||
| [options.timeout] | <code>number</code> | A request specific timeout | | ||
| [options.span] | <code>[Span](https://doc.esdoc.org/github.com/opentracing/opentracing-javascript/class/src/span.js~Span.html)</code> | An OpenTracing span - For example from the parent request | | ||
| [options.retryPolicy] | <code>[RetryPolicies](#module_workflow-manager--WorkflowManager.RetryPolicies)</code> | A request specific retryPolicy | | ||
| [cb] | <code>function</code> | | | ||
<a name="module_workflow-manager--WorkflowManager+CancelJob"></a> | ||
#### workflowManager.CancelJob(params, [options], [cb]) ⇒ <code>Promise</code> | ||
**Kind**: instance method of <code>[WorkflowManager](#exp_module_workflow-manager--WorkflowManager)</code> | ||
**Fulfill**: <code>undefined</code> | ||
**Reject**: <code>[BadRequest](#module_workflow-manager--WorkflowManager.Errors.BadRequest)</code> | ||
**Reject**: <code>[NotFound](#module_workflow-manager--WorkflowManager.Errors.NotFound)</code> | ||
**Reject**: <code>[InternalError](#module_workflow-manager--WorkflowManager.Errors.InternalError)</code> | ||
**Reject**: <code>Error</code> | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| params | <code>Object</code> | | | ||
| params.workflowName | <code>string</code> | | | ||
| [params.input] | | | | ||
| params.jobId | <code>string</code> | | | ||
| params.reason | | | | ||
| [options] | <code>object</code> | | | ||
@@ -111,3 +132,3 @@ | [options.timeout] | <code>number</code> | A request specific timeout | | ||
#### workflowManager.GetJob(params, [options], [cb]) ⇒ <code>Promise</code> | ||
#### workflowManager.GetJob(jobId, [options], [cb]) ⇒ <code>Promise</code> | ||
**Kind**: instance method of <code>[WorkflowManager](#exp_module_workflow-manager--WorkflowManager)</code> | ||
@@ -122,5 +143,3 @@ **Fulfill**: <code>Object</code> | ||
| --- | --- | --- | | ||
| params | <code>Object</code> | | | ||
| params.workflowName | <code>string</code> | | | ||
| params.jobId | <code>string</code> | | | ||
| jobId | <code>string</code> | | | ||
| [options] | <code>object</code> | | | ||
@@ -169,2 +188,23 @@ | [options.timeout] | <code>number</code> | A request specific timeout | | ||
<a name="module_workflow-manager--WorkflowManager+updateWorkflow"></a> | ||
#### workflowManager.updateWorkflow(params, [options], [cb]) ⇒ <code>Promise</code> | ||
**Kind**: instance method of <code>[WorkflowManager](#exp_module_workflow-manager--WorkflowManager)</code> | ||
**Fulfill**: <code>Object</code> | ||
**Reject**: <code>[BadRequest](#module_workflow-manager--WorkflowManager.Errors.BadRequest)</code> | ||
**Reject**: <code>[NotFound](#module_workflow-manager--WorkflowManager.Errors.NotFound)</code> | ||
**Reject**: <code>[InternalError](#module_workflow-manager--WorkflowManager.Errors.InternalError)</code> | ||
**Reject**: <code>Error</code> | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| params | <code>Object</code> | | | ||
| [params.NewWorkflowRequest] | | | | ||
| params.name | <code>string</code> | | | ||
| [options] | <code>object</code> | | | ||
| [options.timeout] | <code>number</code> | A request specific timeout | | ||
| [options.span] | <code>[Span](https://doc.esdoc.org/github.com/opentracing/opentracing-javascript/class/src/span.js~Span.html)</code> | An OpenTracing span - For example from the parent request | | ||
| [options.retryPolicy] | <code>[RetryPolicies](#module_workflow-manager--WorkflowManager.RetryPolicies)</code> | A request specific retryPolicy | | ||
| [cb] | <code>function</code> | | | ||
<a name="module_workflow-manager--WorkflowManager.RetryPolicies"></a> | ||
@@ -171,0 +211,0 @@ |
44941
907
285