-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathevPersistence.js
148 lines (120 loc) · 4.58 KB
/
evPersistence.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//Copyright 2012 Telefonica Investigación y Desarrollo, S.A.U
//
//This file is part of RUSH.
//
// RUSH is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
// RUSH is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License along with RUSH
// . If not, seehttp://www.gnu.org/licenses/.
//
//For those usages not covered by the GNU Affero General Public License please contact with::dtc_support@tid.es
var MG = require('./myGlobals').C;
var db = require('./dbRelayer');
var config_global = require('./configBase.js');
var path = require('path');
var log = require('./logger');
var logger = log.newLogger();
logger.prefix = path.basename(module.filename, '.js');
var TYPE_STATE = 'STATE', TYPE_ERROR = 'ERROR';
function init(emitter) {
'use strict';
logger.debug('Initializing Persistence Listener', { op: 'INIT PERSISTENCE LISTENER', arguments: [ emitter]});
return function(callback) {
emitter.on(MG.EVENT_NEWSTATE, function onNewEvent(data) {
if (data.state === MG.STATE_ERROR || data.state === MG.STATE_COMPLETED ||
//State info can only be stored when X-Relayer-Persistece is set
(data.task.headers[MG.HEAD_RELAYER_PERSISTENCE] &&
(data.state === MG.STATE_QUEUED || data.state === MG.STATE_PROCESSING))) {
var type, respObj;
if (data.state === MG.STATE_QUEUED || data.state === MG.STATE_PROCESSING) {
type = TYPE_STATE; //Notice the difference bt. 'STATE' and 'STATUS'
respObj = { id: data.task.id, state : data.state };
} else {
type = data.state === MG.STATE_ERROR ? TYPE_ERROR : data.task.headers[MG.HEAD_RELAYER_PERSISTENCE];
respObj = data.result || data.err || {};
respObj.state = data.state;
}
doPersistence(data.task, respObj, type, function(error, result) {
if (error || result) {
var st = {
id: data.task.id,
traceID: data.task.traceID,
state: MG.STATE_PERSISTENCE,
date: new Date(),
task: data.task,
err: error,
result: result
};
emitter.emit(MG.EVENT_NEWSTATE, st);
}
});
}
});
callback(null, 'ev_persistence OK');
};
}
function doPersistence(task, respObj, type, callback) {
'use strict';
var validValues = MG.ACCEPTED_PERSISTENCE.slice(); //A copy is needed because the array will be modified
validValues.push(TYPE_ERROR); //Type Error (Internal Value)
validValues.push(TYPE_STATE); //Type State (Internal Value)
if (validValues.indexOf(type) >= 0) {
task.traceID = task.traceID;
setObject(task, respObj, type, callback);
} else {
if (!type && callback) {
callback(null, null);
} else {
if (callback) {
//Error
var errMsg = type + ' is not a valid value for ' + MG.HEAD_RELAYER_PERSISTENCE;
logger.warning(errMsg, { op: 'PERSISTENCE', userID: task.user, correlator: task.traceID,
transid: task.id });
callback(errMsg);
}
}
}
}
function setObject(task, respObj, type, callback) {
'use strict';
//remove from response what is not needed
var errMsg,
traceID = task.traceID,
setObj = {};
type = type.toUpperCase();
setObj = copyObj(respObj);
switch (type) {
case 'STATUS':
delete setObj.headers;
/* fall-through */
case 'HEADER':
delete setObj.body;
delete setObj.encoding;
break;
}
if (!setObj.traceID) {
delete setObj.traceID;
}
db.update(task.id, setObj, traceID, task.user, function onUpdated(err) {
if (err) {
logger.warning('Redis Error', { op: 'PERSISTENCE', userID: task.user, correlator: traceID,
transid: task.id, error: err });
} else {
logger.info('Persistence Completed', { op: 'PERSISTENCE', userID: task.user, correlator: traceID,
transid: task.id , state: setObj.state});
}
if (callback) {
callback(err, setObj);
}
});
}
function copyObj(obj) {
var copy = {};
for (var attr in obj) {
copy[attr] = obj[attr];
}
return copy;
}
exports.init = init;
//require('./hookLogger.js').init(exports, logger);