-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathasg_lambda-clean.js
61 lines (51 loc) · 1.98 KB
/
asg_lambda-clean.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
'use strict';
var redis = require('redis');
exports.handler = function (event, context) {
var endpoint = event.EndPoint;
var group = event.Group;
console.log("Endpoint: '"+ endpoint + "', Group: '" + group + "'");
console.log("Connecting to Redis at '" + endpoint + "'");
var client = new redis.createClient(endpoint, {
socket_keepalive: false,
enable_offline_queue: false,
connect_timeout: 2000,
retry_strategy: function (options) {
if (options.error.code === 'ECONNREFUSED')
return new Error('The server refused the connection');
if (options.total_retry_time > 1000 * 2)
return new Error('Retry time exhausted');
if (options.times_connected > 5)
return undefined;
return Math.min(options.attempt * 50, 100);
}
});
client.on("error", function (err) {
console.error("Error " + err);
context.done(err);
});
client.on('end', () => {
console.log('Connection closed.');
});
client.on('ready', function () {
console.log("Connected to Redis.");
console.log("Existing keys:");
client.keys(group ? group + ':*' : '*', function (err, keys) {
if (err) return console.log(err);
if (keys.length < 1) {
console.log("No existing keys.");
} else {
console.log(JSON.stringify(keys, null, 2));
keys.map(function(key) {
console.log("Trying to delete redis index data for key '" + key + "'");
client.del(key, function(err, reply) {
if (err || (reply === undefined))
console.error("Delete key returned an error: " + reply);
else
console.log("Deleted key successfully");
});
});
}
context.done(err);
});
});
};