-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjobs-agent-delete.js
executable file
·105 lines (89 loc) · 2.74 KB
/
jobs-agent-delete.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { Command } from 'commander';
import { IoT } from '@aws-sdk/client-iot';
import signale from 'signale';
import Chain from 'middleware-chain-js';
import ora from 'ora';
import pThrottle from 'p-throttle';
// Middlewares.
import initializationRoutines from './lib/middlewares/initialization-routines.js';
import generateDevices from './lib/middlewares/generate-devices.js';
import count from './lib/middlewares/count.js';
// Instanciating the IoT Client.
const iotClient = new IoT({
maxAttempts: 10
});
// Instanciating the middleware chain.
const chain = new Chain();
// Creating the throttling function,
// limiting 50 calls per second.
const throttle = pThrottle({ limit: 50, interval: 1000 });
// Spinners.
const spinners = {
deletion: null
};
/**
* Command-line interface.
*/
const program = new Command()
.name('iot-jobs-agent delete')
.description('Deletes the selected amount of IoT things from the AWS IoT registry.')
.requiredOption('-n, --number <number>', 'Specifies the amount of the things to delete from AWS IoT.')
.parse(process.argv);
/**
* @return a text associated with the deletion spinner
* displaying the current deletion statistics.
* @param {*} current the current number of deleted things.
* @param {*} total the total number of devices to delete.
*/
const thingsStats = (current, total) => `Deleting ${total} thing(s) from AWS IoT (${current}/${total})`;
/**
* Deletes the given `thing` from the AWS IoT
* device registry.
* @param {*} thingName a thing name to delete
* from the AWS IoT device registry.
* @return a promise resolved when the given
* `thing` has been deleted.
*/
const deleteThing = throttle((thingName) => iotClient.deleteThing({ thingName }));
/**
* Injecting the middlewares into the `chain`.
*/
chain
.use(count(program.opts().number))
.use(initializationRoutines)
.use(generateDevices);
/**
* Creating the things on AWS IoT.
*/
chain.use(async (input, _, next) => {
let deleted = 0;
// Creating the deletion spinner.
spinners.deletion = ora(thingsStats(0, input.number)).start();
try {
// Deleting the things.
await Promise.all(
input.devices.map(async (device) => {
await deleteThing(device);
spinners.deletion.text = thingsStats(++deleted, input.number);
})
);
// Marking the spinner as having suceeded.
spinners.deletion.succeed().stop();
next();
} catch (e) {
next(e);
}
});
/**
* Signaling the success of the operation.
*/
chain.use((input) => signale.success(`All '${input.number}' thing(s) have been deleted from the AWS IoT device registry.`));
/**
* Error handler.
*/
chain.use((err, _1, output, next) => {
spinners.deletion.stop();
output.fail(err);
});
// Starting the middleware chain.
chain.handle({}, {});