diff --git a/lib/pooler.js b/lib/pooler.js index 0bb5c8b..de08e94 100644 --- a/lib/pooler.js +++ b/lib/pooler.js @@ -1,10 +1,7 @@ const util = require('util'); const EventEmitter = require('events').EventEmitter; -const AWS = require('aws-sdk'); const Task = require('./task.js'); -const stepfunction = new AWS.StepFunctions(); - /** * @class Pooler * @param {object} options @@ -82,7 +79,7 @@ Pooler.prototype.pool = function () { Pooler.prototype.getActivityTask = function () { this.logger.debug(this.workerName + ' getActivityTask ' + this.activityArn); - this._request = stepfunction.getActivityTask({ + this._request = this.worker.stepfunction.getActivityTask({ activityArn: this.activityArn, workerName: this.workerName }, (err, data) => { diff --git a/lib/worker.js b/lib/worker.js index 25bc919..5229ab6 100644 --- a/lib/worker.js +++ b/lib/worker.js @@ -2,7 +2,6 @@ const EventEmitter = require('events').EventEmitter; const util = require('util'); const AWS = require('aws-sdk'); -const stepfunction = new AWS.StepFunctions(); const Pooler = require('./pooler.js'); const replaceError = require('./replace-error.js'); @@ -15,10 +14,16 @@ const replaceError = require('./replace-error.js'); * @param {boolean} [options.autoStart=true] * @param {boolean} [options.logger=null] winston-like logger * @param {string} [options.concurrency=1] +* @param {AWSConfig} [options.awsConfig={}] * */ +/** +* @typedef {Object} AWSConfig see https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html +*/ function Worker(options) { EventEmitter.call(this); + const awsConfig = options.awsConfig || {}; + this.stepfunction = new AWS.StepFunctions(awsConfig); this.autoStart = typeof (options.autoStart) === 'boolean' ? options.autoStart : true; @@ -126,7 +131,7 @@ Worker.prototype.execute = function (input, cb, heartbeat) { Worker.prototype.succeed = function (res) { const params = Object.assign({}, res, {output: JSON.stringify(res.output)}); delete params.workerName; - stepfunction.sendTaskSuccess(params, err => { + this.stepfunction.sendTaskSuccess(params, err => { if (err) { this.emit('error', err); } else { @@ -146,7 +151,7 @@ Worker.prototype.fail = function (res) { const params = Object.assign({}, res, {error}); delete params.workerName; this.logger.debug('sendTaskFailure', res.error); - stepfunction.sendTaskFailure(params, err => { + this.stepfunction.sendTaskFailure(params, err => { if (err) { this.emit('error', err); } else { @@ -160,7 +165,7 @@ Worker.prototype.heartbeat = function (res) { delete params.workerName; this.logger.debug('sendTaskHeartbeat'); - stepfunction.sendTaskHeartbeat(params, err => { + this.stepfunction.sendTaskHeartbeat(params, err => { if (err) { this.emit('error', err); } else {