-
Notifications
You must be signed in to change notification settings - Fork 0
/
relayerDAO.js
130 lines (126 loc) · 4.11 KB
/
relayerDAO.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* Created by JetBrains WebStorm.
* User: mru
* Date: 04/01/12
* Time: 09:01
* To change this template use File | Settings | File Templates.
*/
//REDIS DAO - node module
var
redis_module = require('redis-client'),
util = require('util'),
logger = require('./simpleLogger').log,
MyGlobal = require('./constantModule').Global,
log = logger.log,
redis,
redis_key_prefix;
exports.ini = function _init(use_host, use_port, use_prefix) {
"use strict";
var
port = use_port || redis_module.DEFAULT_PORT,
host = use_host || redis_module.DEFAULT_HOST;
redis_key_prefix = use_prefix || "HR:";
try {
redis = redis_module.createClient(port, host);
}
catch (excp) {
log("ERROR CONNECTING to REDIS:" + host + ":" + port);
MyGlobal.inspection_str = util.inspect(excp);
log(MyGlobal.inspection_str);
}
};
exports.store_data = function _store_data(id, str_header, str_status, content, callback) {
"use strict";
redis.hmset(redis_key_prefix + id,
MyGlobal.RD_STATE, MyGlobal.STATE_COMPLETED,
MyGlobal.RD_HEADER, str_header,
MyGlobal.RD_STATUSCODE, str_status,
MyGlobal.RD_DATA, content,
function (err) {
if (err) {
MyGlobal.inspection_str = util.inspect(err);
log("DB error (Can not insert):" + MyGlobal.inspection_str);
}
else {
log("Kept response for:" + id);
}
if (callback) {
callback(err);
}
}
);
};
exports.update_retry_fail = function _update_retry_fail(id, callback) {
"use strict";
redis.hset(redis_key_prefix + id,
MyGlobal.RD_STATE, MyGlobal.STATE_RETRY_FAIL,
function (err) {
if (err) {
MyGlobal.inspection_str = util.inspect(err);
log("DB error (Can not insert-retryfail):" + MyGlobal.inspection_str);
}
else {
log("Retry-fail added for:" + id);
}
if (callback) {
callback(err);
}
});
};
exports.update_pending = function _update_pending(id, relayer_host, method, postdata, callback) {
"use strict";
redis.hmset(redis_key_prefix + id,
MyGlobal.RD_STATE, MyGlobal.STATE_PENDING,
MyGlobal.RD_RELAYED_REQUEST, relayer_host,
MyGlobal.RD_METHOD, method,
MyGlobal.RD_POSTDATA, postdata,
function (err) {
if (err) {
MyGlobal.inspection_str = util.inspect(err);
log('WARN -(go) DB Can not insert:' + MyGlobal.inspection_str);
}
if (callback) {
callback(err);
}
});
};
exports.get_all = function _get_all(id, callback) {
"use strict";
redis.hgetall(redis_key_prefix + id, function (err, redis_data) {
var ret_data = {};
if (err) {
MyGlobal.inspection_str = util.inspect(err);
log('DB can\'t retrieve your data' + MyGlobal.inspection_str);
}
else {
ret_data.res_header = redis_data[MyGlobal.RD_HEADER] || "";
ret_data.res_header = ret_data.res_header.toString();
ret_data.res_header = JSON.parse(ret_data.res_header);
ret_data.res_data = redis_data[MyGlobal.RD_DATA] || "";
ret_data.res_data = ret_data.res_data.toString();
ret_data.res_state = redis_data[MyGlobal.RD_STATE] || "";
ret_data.res_state = ret_data.res_state.toString();
ret_data.res_status = redis_data[MyGlobal.RD_STATUSCODE] || "";
ret_data.res_status = ret_data.res_status.toString();
}
if (callback) {
callback(err, ret_data);
}
});
};
/*
exports.get_id = function _get_id(callback) {
"use strict";
redis.incr(redis_key_prefix + 'GLOBAL_ID_SEQ', function (err, idsec) {
if (err) {
log("Problems getting ID_SEC: 'HR:GLOBAL_ID_SEQ'");
}
else {
log("ID_SEC: 'HR:GLOBAL_ID_SEQ:'" + idsec);
}
if (callback) {
callback(err, idsec);
}
});
};
*/