-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
105 lines (93 loc) · 3.55 KB
/
index.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
'use strict'
const redis = require('ioredis');
const Promise = require('bluebird');
const SCAN_COUNT = 500;
const redisFlush = async (params, callback) => {
try {
if(Object.keys(params).length == 0)
callback(`Please provide required parameters.`);
const {
redisKey = '',
redisInfo = {},
order = 'pre'
} = params;
if(redisKey == '')
callback(`Please provide redis key prefix, middle or suffix.`);
if(Object.keys(redisInfo).length == 0)
callback(`Please provide required redis information.`);
const redisInstance = await getredisInstance(redisInfo);
if (!redisInstance)
callback(`Not able to connect to redis.`);
let stream = {};
if (order === 'pre') {
stream = await redisInstance.scanStream({ match: `${redisKey}*`, count: SCAN_COUNT });
} else if (order === 'middle') {
stream = await redisInstance.scanStream({ match: `*${redisKey}*`, count: SCAN_COUNT });
} else if (order === 'post') {
stream = await redisInstance.scanStream({ match: `*${redisKey}`, count: SCAN_COUNT });
}
let pipeline = redisInstance.pipeline();
let localKeys = [];
stream.on('data', async (resultKeys) => {
await stream.pause();
const { length } = resultKeys;
for (let i = 0; i < length; i++) {
const key = resultKeys[i];
localKeys.push(key);
pipeline.del(key);
}
if (localKeys.length > SCAN_COUNT) {
pipeline.exec(() => { });
localKeys = [];
pipeline = redisInstance.pipeline();
await stream.resume();
} else {
await stream.resume();
}
}).on('end', (data) => {
pipeline.exec((err, results) => {
if (err)
callback(err);
callback(null, { success: true, keysDeleted : results.length });
});
}).on('error', (e) => {
callback(e.stack, false);
});
} catch (e) {
callback(e.stack, false);
}
}
const getredisInstance = async (redisInfo) => {
try {
const client = Promise.promisifyAll(redis.createClient({
sentinels: redisInfo.host,
pass: redisInfo.password,
name: "mymaster",
role: 'master',
retry_strategy: function (options) {
if (options.error.code === 'ECONNREFUSED') {
// End reconnecting on a specific error and flush all commands with a individual error
return new Error('The server refused the connection');
}
if (options.total_retry_time > 1000 * 60 * 60) {
// End reconnecting after a specific timeout and flush all commands with a individual error
return new Error('Retry time exhausted');
}
if (options.times_connected > 10) {
// End reconnecting with built in error
return undefined;
}
// reconnect after
return Math.max(options.attempt * 100, 500);
}
})),
sessionAge = 60000;
client.auth(redisInfo.password);
client.select(redisInfo.database);
return client;
} catch (e) {
throw e;
}
}
process.on('unhandledRejection', (err) => {});
module.exports = redisFlush