-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
113 lines (99 loc) · 3.79 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
106
107
108
109
110
111
112
113
'use strict';
const redis = require('redis');
const fsp = require('fs').promises;
const Executor = require('@runnerty/module-core').Executor;
class redisExecutor extends Executor {
constructor(process) {
super(process);
}
async exec(res) {
const self = this;
const endOptions = { end: 'end' };
function commandsFormat(command) {
const res = [];
const lines = command.split(/\r\n|\n/);
for (let i = 0; i < lines.length; i++) {
res.push(lines[i].trim().split(' '));
}
return res;
}
async function executeCommand(configValues) {
return new Promise(async (resolve, reject) => {
const options = {
useExtraValue: configValues.args || false,
useProcessValues: true,
useGlobalValues: true,
altValueReplace: 'null'
};
if (!configValues.options) configValues.options = {};
if (configValues.command_file) {
try {
// Load file:
await fsp.access(configValues.command_file, fsp.constants.F_OK | fsp.constants.W_OK);
res.command = await fsp.readFile(configValues.command_file, 'utf8');
} catch (err) {
this.logger.log('error', 'Loading redis command file: ' + err);
reject('Loading redis command file: ' + err);
}
}
const _query = await self.paramsReplace(res.command, options);
if (configValues.password && configValues.password !== '') {
configValues.options.password = configValues.password || '';
}
configValues.options.db = configValues.db || 0;
const redisClient = redis.createClient(configValues.port || '6379', configValues.host, configValues.options);
redisClient.on('error', err => {
this.logger.log('error', 'Could not connect to Redis: ' + err);
reject('Could not connect to Redis: ' + err);
});
redisClient.on('ready', () => {
const commands = commandsFormat(_query);
endOptions.command_executed = commands;
try {
redisClient.batch(commands).exec((err, replies) => {
redisClient.quit();
if (err) {
this.logger.log('error', `Error query Redis (${commands}): ` + err);
reject(`Error query Redis (${commands}): ` + err);
} else {
resolve(replies);
}
});
} catch (err) {
this.logger.log('error', 'Error query Redis, check commands: ' + commands, err);
reject('Error query Redis, check commands: ' + commands, err);
}
});
});
}
if (res.command || res.command_file) {
try {
const response = await executeCommand(res);
// STANDARD OUTPUT
endOptions.end = 'end';
if (response.length == 1 && response[0]) {
endOptions.data_output = response[0];
} else {
endOptions.data_output = response;
}
// EXTRA DATA OUTPUT
endOptions.extra_output = {};
if (response && response[0]) {
endOptions.extra_output.db_firstRow = JSON.stringify(response[0]);
}
this.end(endOptions);
} catch (err) {
endOptions.end = 'error';
endOptions.messageLog = `executeRedis executeCommand: ${err}`;
endOptions.err_output = `executeRedis executeCommand: ${err}`;
this.end(endOptions);
}
} else {
endOptions.end = 'error';
endOptions.messageLog = `executeRedis: command not set and command_file nor supported yet for ${this.processId}(${this.processUId}.`;
endOptions.err_output = `executeRedis: command not set and command_file nor supported yet for ${this.processId}(${this.processUId}.`;
this.end(endOptions);
}
}
}
module.exports = redisExecutor;