Skip to content

Commit 5074bf1

Browse files
authored
Merge pull request #14 from TaniaPash/master
fix: add cleanup-states-script
2 parents d1f0b6f + 82ed3d2 commit 5074bf1

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"homepage": "https://github.com/piercus/step-function-worker#readme",
3737
"devDependencies": {
3838
"ava": "^0.25.0",
39+
"bluebird": "^3.5.3",
3940
"semantic-release": "^15.1.7",
4041
"winston": "^2.4.1",
4142
"xo": "^0.23.0"
@@ -44,4 +45,4 @@
4445
"aws-arn-parser": "^1.0.0",
4546
"aws-sdk": "^2.82.0"
4647
}
47-
}
48+
}

test/scripts/cleanup-states.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const AWS = require('aws-sdk');
2+
const PromiseBlue = require('bluebird');
3+
const winston = require('winston');
4+
5+
const logger = new winston.Logger({
6+
transports: [new winston.transports.Console({
7+
level: 'info'
8+
})]
9+
});
10+
11+
if (typeof (process.env.AWS_REGION) !== 'string') {
12+
throw (new TypeError('$AWS_REGION must be defined'));
13+
}
14+
15+
const stepfunctions = new AWS.StepFunctions({
16+
region: process.env.AWS_REGION
17+
});
18+
19+
const reg = new RegExp('stateMachine:test-state-machine');
20+
21+
const removeStateMachines = function (reg) {
22+
const params = {};
23+
return stepfunctions.listStateMachines(params).promise().then(data => {
24+
const stateMachinesArn = [];
25+
data.stateMachines.forEach(stm => {
26+
if (reg.test(stm.stateMachineArn)) {
27+
stateMachinesArn.push(stm.stateMachineArn);
28+
}
29+
});
30+
return stateMachinesArn;
31+
}).then(stateMachinesArn => {
32+
return PromiseBlue.map(stateMachinesArn, stateMachineArn => {
33+
const params = {
34+
stateMachineArn
35+
};
36+
return stepfunctions.describeStateMachine(params).promise().then(data => {
37+
const definition = JSON.parse(data.definition);
38+
const activityArn = definition.States.FirstState.Resource;
39+
const params = {
40+
activityArn
41+
};
42+
return stepfunctions.deleteActivity(params).promise().then(() => {
43+
logger.info('Activity ', params.activityArn, ' was deleted');
44+
});
45+
}).then(() => {
46+
return stepfunctions.deleteStateMachine(params).promise().then(() => {
47+
logger.info('StateMachine ', params.stateMachineArn, ' was deleted');
48+
});
49+
});
50+
}, {concurrency: 1});
51+
});
52+
};
53+
54+
removeStateMachines(reg).catch(err => {
55+
logger.error(err);
56+
});

0 commit comments

Comments
 (0)