|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const BbPromise = require('bluebird'); |
| 4 | +const path = require('path'); |
| 5 | + |
| 6 | +module.exports = { |
| 7 | + getStateMachineArn() { |
| 8 | + const stateMachineOutputKey = |
| 9 | + this.getStateMachineOutputLogicalId(this.options.name); |
| 10 | + const stackName = this.provider.naming.getStackName(this.options.stage); |
| 11 | + |
| 12 | + return this.provider.request('CloudFormation', |
| 13 | + 'describeStacks', |
| 14 | + { StackName: stackName }, |
| 15 | + this.options.stage, |
| 16 | + this.options.region) |
| 17 | + .then((result) => { |
| 18 | + if (result) { |
| 19 | + result.Stacks[0].Outputs.forEach((output) => { |
| 20 | + if (output.OutputKey === stateMachineOutputKey) { |
| 21 | + this.stateMachineArn = output.OutputValue; |
| 22 | + } |
| 23 | + }); |
| 24 | + |
| 25 | + if (!this.stateMachineArn) { |
| 26 | + const errorMessage = [ |
| 27 | + `"${this.options.name}" stateMachine does not exists.`, |
| 28 | + ].join(''); |
| 29 | + throw new this.serverless.classes.Error(errorMessage); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + return BbPromise.resolve(); |
| 34 | + }); |
| 35 | + }, |
| 36 | + |
| 37 | + startExecution() { |
| 38 | + let inputData; |
| 39 | + |
| 40 | + if (this.options.data) { |
| 41 | + inputData = this.options.data; |
| 42 | + } else if (this.options.path) { |
| 43 | + const absolutePath = path.isAbsolute(this.options.path) ? |
| 44 | + this.options.path : |
| 45 | + path.join(this.serverless.config.servicePath, this.options.path); |
| 46 | + if (!this.serverless.utils.fileExistsSync(absolutePath)) { |
| 47 | + throw new this.serverless.classes.Error('The file you provided does not exist.'); |
| 48 | + } |
| 49 | + inputData = JSON.stringify(this.serverless.utils.readFileSync(absolutePath)); |
| 50 | + } |
| 51 | + |
| 52 | + return this.provider.request('StepFunctions', |
| 53 | + 'startExecution', |
| 54 | + { |
| 55 | + stateMachineArn: this.stateMachineArn, |
| 56 | + input: inputData, |
| 57 | + }, |
| 58 | + this.options.stage, |
| 59 | + this.options.region) |
| 60 | + .then((result) => { |
| 61 | + this.executionArn = result.executionArn; |
| 62 | + return BbPromise.resolve(); |
| 63 | + }).catch((error) => { |
| 64 | + throw new this.serverless.classes.Error(error.message); |
| 65 | + }); |
| 66 | + }, |
| 67 | + |
| 68 | + describeExecution() { |
| 69 | + return this.provider.request('StepFunctions', |
| 70 | + 'describeExecution', |
| 71 | + { |
| 72 | + executionArn: this.executionArn, |
| 73 | + }, |
| 74 | + this.options.stage, |
| 75 | + this.options.region) |
| 76 | + .then((result) => { |
| 77 | + if (result.status === 'RUNNING') { |
| 78 | + this.serverless.cli.printDot(); |
| 79 | + return this.setTimeout() |
| 80 | + .then(() => this.describeExecution()); |
| 81 | + } |
| 82 | + return BbPromise.resolve(result); |
| 83 | + }); |
| 84 | + }, |
| 85 | + |
| 86 | + getExecutionHistory() { |
| 87 | + return this.provider.request('StepFunctions', |
| 88 | + 'getExecutionHistory', |
| 89 | + { |
| 90 | + executionArn: this.executionArn, |
| 91 | + }, |
| 92 | + this.options.stage, |
| 93 | + this.options.region) |
| 94 | + .then((result) => BbPromise.resolve(result)); |
| 95 | + }, |
| 96 | + |
| 97 | + setTimeout() { |
| 98 | + return new BbPromise((resolve) => { |
| 99 | + setTimeout(resolve, 5000); |
| 100 | + }); |
| 101 | + }, |
| 102 | +}; |
0 commit comments