Skip to content

Commit 5279b28

Browse files
committed
add invoke command
1 parent 7581965 commit 5279b28

File tree

3 files changed

+88
-14
lines changed

3 files changed

+88
-14
lines changed

index.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const BbPromise = require('bluebird');
33
const deploy = require('./lib/deploy');
44
const remove = require('./lib/remove');
5+
const invoke = require('./lib/invoke');
56

67
class ServerlessStepFunctions {
78
constructor(serverless, options) {
@@ -12,7 +13,8 @@ class ServerlessStepFunctions {
1213
Object.assign(
1314
this,
1415
deploy,
15-
remove
16+
remove,
17+
invoke
1618
);
1719

1820
this.commands = {
@@ -24,9 +26,9 @@ class ServerlessStepFunctions {
2426
'deploy',
2527
],
2628
options: {
27-
statemachine: {
29+
state: {
2830
usage: 'Name of the State Machine',
29-
shortcut: 'sm',
31+
shortcut: 's',
3032
required: true,
3133
},
3234
},
@@ -41,9 +43,26 @@ class ServerlessStepFunctions {
4143
'remove',
4244
],
4345
options: {
44-
statemachine: {
46+
state: {
4547
usage: 'Name of the State Machine',
46-
shortcut: 'sm',
48+
shortcut: 's',
49+
required: true,
50+
},
51+
},
52+
},
53+
},
54+
},
55+
invoke: {
56+
commands: {
57+
stepf: {
58+
usage: 'Remove Step functions',
59+
lifecycleEvents: [
60+
'invoke',
61+
],
62+
options: {
63+
state: {
64+
usage: 'Name of the State Machine',
65+
shortcut: 's',
4766
required: true,
4867
},
4968
},
@@ -57,6 +76,8 @@ class ServerlessStepFunctions {
5776
.then(this.deploy),
5877
'remove:stepf:remove': () => BbPromise.bind(this)
5978
.then(this.remove),
79+
'invoke:stepf:invoke': () => BbPromise.bind(this)
80+
.then(this.invoke),
6081
};
6182
}
6283
}

lib/deploy.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ module.exports = {
9292
.then((result) => {
9393
const region = this.options.region || 'us-east-1';
9494
this.stateMachineArn =
95-
`arn:aws:states:${region}:${result.Account}:stateMachine:${this.options.statemachine}`;
95+
`arn:aws:states:${region}:${result.Account}:stateMachine:${this.options.state}`;
9696
return BbPromise.resolve();
9797
});
9898
},
@@ -123,22 +123,22 @@ module.exports = {
123123
return BbPromise.resolve();
124124
}
125125

126-
if (typeof this.stepFunctions[this.options.statemachine] === 'undefined') {
126+
if (typeof this.stepFunctions[this.options.state] === 'undefined') {
127127
const errorMessage = [
128-
`Step function "${this.options.statemachine}" is not exists`,
128+
`Step function "${this.options.state}" is not exists`,
129129
].join('');
130130
throw new this.serverless.classes.Error(errorMessage);
131131
}
132132

133-
_.forEach(this.stepFunctions[this.options.statemachine].States, (value, key) => {
133+
_.forEach(this.stepFunctions[this.options.state].States, (value, key) => {
134134
if (value.Resource && !value.Resource.match(/arn:aws:lambda/)) {
135-
this.stepFunctions[this.options.statemachine].States[key].Resource
135+
this.stepFunctions[this.options.state].States[key].Resource
136136
= this.functionArns[value.Resource];
137137
}
138138
});
139139

140-
this.awsStateLanguage[this.options.statemachine] =
141-
JSON.stringify(this.stepFunctions[this.options.statemachine]);
140+
this.awsStateLanguage[this.options.state] =
141+
JSON.stringify(this.stepFunctions[this.options.state]);
142142
return BbPromise.resolve();
143143
},
144144

@@ -157,16 +157,19 @@ module.exports = {
157157
return this.provider.request('StepFunctions',
158158
'createStateMachine',
159159
{
160-
definition: this.awsStateLanguage[this.options.statemachine],
161-
name: this.options.statemachine,
160+
definition: this.awsStateLanguage[this.options.state],
161+
name: this.options.state,
162162
roleArn: this.iamRoleArn,
163163
},
164164
this.options.stage,
165165
this.options.region)
166166
.then(() => BbPromise.resolve())
167167
.catch((error) => {
168+
console.log(error)
168169
if (error.message.match(/State Machine is being deleted/)) {
169170
setTimeout(this.createStateMachine.bind(this), 5000);
171+
} else {
172+
throw new this.serverless.classes.Error(error.message);
170173
}
171174
});
172175
},

lib/invoke.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'use strict';
2+
const BbPromise = require('bluebird');
3+
4+
module.exports = {
5+
invoke() {
6+
BbPromise.bind(this)
7+
.then(this.getStateMachineArn)
8+
.then(this.startExecution)
9+
.then(this.describeExecution);
10+
11+
return BbPromise.resolve();
12+
},
13+
14+
startExecution() {
15+
return this.provider.request('StepFunctions',
16+
'startExecution',
17+
{
18+
stateMachineArn: this.stateMachineArn,
19+
},
20+
this.options.stage,
21+
this.options.region)
22+
.then((result) => {
23+
this.executionArn = result.executionArn
24+
return BbPromise.resolve();
25+
}).catch((error) => {
26+
console.log(error);
27+
});
28+
},
29+
30+
describeExecution() {
31+
return this.provider.request('StepFunctions',
32+
'describeExecution',
33+
{
34+
executionArn: this.executionArn,
35+
},
36+
this.options.stage,
37+
this.options.region)
38+
.then((result) => {
39+
40+
if (result.status === 'RUNNING') {
41+
setTimeout(this.describeExecution.bind(this), 5000);
42+
} else {
43+
44+
}
45+
return BbPromise.resolve();
46+
}).catch((error) => {
47+
console.log(error);
48+
});
49+
}
50+
};

0 commit comments

Comments
 (0)