Commit 9c03fb34aa082a5cb7e65f6c782d88c366fbb896

Authored by Adhidarma Hadiwinoto
1 parent 0a06547ec5
Exists in master

simplifikasi penghapusan undefined resources

Showing 1 changed file with 1 additions and 1 deletions Inline Diff

1 'use strict'; 1 'use strict';
2 2
3 var moment = require('moment'); 3 var moment = require('moment');
4 4
5 var config; 5 var config;
6 6
7 var momentFormat = 'YYYY-MM-DD HH:mm:ss'; 7 var momentFormat = 'YYYY-MM-DD HH:mm:ss';
8 var defaultMaxHealthyWaitMs = 2 * 60 * 1000; 8 var defaultMaxHealthyWaitMs = 2 * 60 * 1000;
9 9
10 module.exports = MatrixUtil; 10 module.exports = MatrixUtil;
11 11
12 function _cleanPartnerId(partnerId) { 12 function _cleanPartnerId(partnerId) {
13 let cleaned = partnerId; 13 let cleaned = partnerId;
14 14
15 try { 15 try {
16 cleaned = cleaned.toLocaleString(); 16 cleaned = cleaned.toLocaleString();
17 cleaned = cleaned.trim().toLowerCase(); 17 cleaned = cleaned.trim().toLowerCase();
18 } catch(e) { 18 } catch(e) {
19 return partnerId; 19 return partnerId;
20 } 20 }
21 21
22 return cleaned; 22 return cleaned;
23 } 23 }
24 24
25 function getMaxHealthyWaitMs() { 25 function getMaxHealthyWaitMs() {
26 if (!config || !config.globals || !Number(config.globals.max_healthy_wait_ms)) { return defaultMaxHealthyWaitMs; } 26 if (!config || !config.globals || !Number(config.globals.max_healthy_wait_ms)) { return defaultMaxHealthyWaitMs; }
27 return Number(config.globals.max_healthy_wait_ms); 27 return Number(config.globals.max_healthy_wait_ms);
28 } 28 }
29 29
30 function MatrixUtil(options) { 30 function MatrixUtil(options) {
31 if (!options) { 31 if (!options) {
32 console.trace('Undefined options'); 32 console.trace('Undefined options');
33 process.exit(1); 33 process.exit(1);
34 } 34 }
35 35
36 this.matrix = options.matrix; 36 this.matrix = options.matrix;
37 if (!this.matrix) { 37 if (!this.matrix) {
38 console.trace("Matrix not set"); 38 console.trace("Matrix not set");
39 process.exit(1); 39 process.exit(1);
40 } 40 }
41 41
42 this.logger = options.logger; 42 this.logger = options.logger;
43 if (!this.logger) { 43 if (!this.logger) {
44 console.trace("Logger not set"); 44 console.trace("Logger not set");
45 process.exit(1); 45 process.exit(1);
46 } 46 }
47 47
48 config = options.config; 48 config = options.config;
49 if (!config) { 49 if (!config) {
50 console.trace("Config not set"); 50 console.trace("Config not set");
51 process.exit(1); 51 process.exit(1);
52 }; 52 };
53 } 53 }
54 54
55 MatrixUtil.prototype.updateBuddyState = function(jid, state, statusText, resource) { 55 MatrixUtil.prototype.updateBuddyState = function(jid, state, statusText, resource) {
56 if (!jid) {return; } 56 if (!jid) {return; }
57 if (jid == 'undefined') {return; } 57 if (jid == 'undefined') {return; }
58 58
59 jid = _cleanPartnerId(jid); 59 jid = _cleanPartnerId(jid);
60 60
61 if (!resource) { 61 if (!resource) {
62 resource = 'undefined'; 62 resource = 'undefined';
63 } 63 }
64 64
65 let logger = this.logger; 65 let logger = this.logger;
66 let matrix = this.matrix; 66 let matrix = this.matrix;
67 67
68 logger.verbose('Buddy state change', {jid: jid, state: state, statusText: statusText, resource: resource}); 68 logger.verbose('Buddy state change', {jid: jid, state: state, statusText: statusText, resource: resource});
69 69
70 if (!matrix) { 70 if (!matrix) {
71 return; 71 return;
72 } 72 }
73 73
74 if (!matrix.buddies) { 74 if (!matrix.buddies) {
75 matrix.buddies = {}; 75 matrix.buddies = {};
76 } 76 }
77 77
78 if (!matrix.buddies[jid]) { 78 if (!matrix.buddies[jid]) {
79 matrix.buddies[jid] = {resources: {}}; 79 matrix.buddies[jid] = {resources: {}};
80 } 80 }
81 81
82 try { 82 try {
83 matrix.buddies[jid]['resources'][resource] = { 83 matrix.buddies[jid]['resources'][resource] = {
84 state: state, 84 state: state,
85 statusText: statusText, 85 statusText: statusText,
86 last_update: moment().format(momentFormat) 86 last_update: moment().format(momentFormat)
87 } 87 }
88 } 88 }
89 catch(e) { 89 catch(e) {
90 logger.warn('MatrixUtil: Exception on update resources on matrix', {jid: jid, state: state, statusText: statusText, resource: resource}); 90 logger.warn('MatrixUtil: Exception on update resources on matrix', {jid: jid, state: state, statusText: statusText, resource: resource});
91 } 91 }
92 92
93 if (resource != 'undefined' && matrix.buddies[jid].resources['undefined']) { 93 if (resource != 'undefined') {
94 try { 94 try {
95 delete matrix.buddies[jid].resources['undefined']; 95 delete matrix.buddies[jid].resources['undefined'];
96 } 96 }
97 catch(e) {}; 97 catch(e) {};
98 } 98 }
99 } 99 }
100 100
101 MatrixUtil.prototype.isPartnerOffline = function(partner) { 101 MatrixUtil.prototype.isPartnerOffline = function(partner) {
102 if (!partner) { return; } 102 if (!partner) { return; }
103 103
104 partner = _cleanPartnerId(partner); 104 partner = _cleanPartnerId(partner);
105 105
106 let matrix = this.matrix; 106 let matrix = this.matrix;
107 let logger = this.logger; 107 let logger = this.logger;
108 108
109 if (!matrix) { return false; } 109 if (!matrix) { return false; }
110 110
111 if (!matrix.buddies) { matrix.buddies = {}; } 111 if (!matrix.buddies) { matrix.buddies = {}; }
112 if (!matrix.buddies[partner]) { return true; } 112 if (!matrix.buddies[partner]) { return true; }
113 if (!matrix.buddies[partner].resources) { return true; }; 113 if (!matrix.buddies[partner].resources) { return true; };
114 114
115 let resources = matrix.buddies[partner].resources; 115 let resources = matrix.buddies[partner].resources;
116 for (let key in resources) { 116 for (let key in resources) {
117 if (resources.hasOwnProperty(key)) { 117 if (resources.hasOwnProperty(key)) {
118 let resource = resources[key]; 118 let resource = resources[key];
119 if (resources[key].state == 'online') { 119 if (resources[key].state == 'online') {
120 return false; 120 return false;
121 } 121 }
122 } 122 }
123 } 123 }
124 logger.verbose('Offline partner detected: ' + partner); 124 logger.verbose('Offline partner detected: ' + partner);
125 return true; 125 return true;
126 } 126 }
127 127
128 MatrixUtil.prototype._isPartnerHealthy = function(partner) { 128 MatrixUtil.prototype._isPartnerHealthy = function(partner) {
129 if (!partner) { return; } 129 if (!partner) { return; }
130 partner = _cleanPartnerId(partner); 130 partner = _cleanPartnerId(partner);
131 131
132 if (this.isPartnerOffline(partner)) { 132 if (this.isPartnerOffline(partner)) {
133 return; 133 return;
134 } 134 }
135 135
136 let matrix = this.matrix; 136 let matrix = this.matrix;
137 let logger = this.logger; 137 let logger = this.logger;
138 138
139 if (!matrix) { return false; } 139 if (!matrix) { return false; }
140 140
141 if (!matrix.buddies[partner]) { return false; } 141 if (!matrix.buddies[partner]) { return false; }
142 if (!matrix.buddies[partner]['waiting_for_response']) { return true; } 142 if (!matrix.buddies[partner]['waiting_for_response']) { return true; }
143 if (!matrix.buddies[partner]['last_incoming']) { return false; } 143 if (!matrix.buddies[partner]['last_incoming']) { return false; }
144 if (!matrix.buddies[partner]['last_incoming']['last_update_ts']) { return false; } 144 if (!matrix.buddies[partner]['last_incoming']['last_update_ts']) { return false; }
145 145
146 let delta = Date.now() - Number(matrix.buddies[partner]['last_incoming']['last_update_ts']); 146 let delta = Date.now() - Number(matrix.buddies[partner]['last_incoming']['last_update_ts']);
147 let isHealthy = delta <= getMaxHealthyWaitMs(); 147 let isHealthy = delta <= getMaxHealthyWaitMs();
148 148
149 logger.verbose('Partner healthy analized', {partner: partner, isHealthy: isHealthy, delta: delta, maxHealthyWaitMs: getMaxHealthyWaitMs()}); 149 logger.verbose('Partner healthy analized', {partner: partner, isHealthy: isHealthy, delta: delta, maxHealthyWaitMs: getMaxHealthyWaitMs()});
150 return isHealthy; 150 return isHealthy;
151 } 151 }
152 152
153 MatrixUtil.prototype.isPartnerHealthy = function(partner) { 153 MatrixUtil.prototype.isPartnerHealthy = function(partner) {
154 let matrix = this.matrix; 154 let matrix = this.matrix;
155 155
156 let isHealthy = this._isPartnerHealthy(partner); 156 let isHealthy = this._isPartnerHealthy(partner);
157 157
158 if (!matrix.healthy_partners) { 158 if (!matrix.healthy_partners) {
159 matrix.healthy_partners = []; 159 matrix.healthy_partners = [];
160 } 160 }
161 161
162 // update matrix 162 // update matrix
163 let idx = matrix.healthy_partners.indexOf(partner); 163 let idx = matrix.healthy_partners.indexOf(partner);
164 164
165 if (isHealthy) { 165 if (isHealthy) {
166 if (idx < 0) { 166 if (idx < 0) {
167 matrix.healthy_partners.push(partner); 167 matrix.healthy_partners.push(partner);
168 } 168 }
169 } 169 }
170 else { 170 else {
171 if (idx > -1) { 171 if (idx > -1) {
172 matrix.healthy_partners.splice(idx, 1); 172 matrix.healthy_partners.splice(idx, 1);
173 } 173 }
174 } 174 }
175 175
176 return isHealthy; 176 return isHealthy;
177 } 177 }
178 178
179 MatrixUtil.prototype._updateLastResponseTime = function(partner) { 179 MatrixUtil.prototype._updateLastResponseTime = function(partner) {
180 let matrix = this.matrix; 180 let matrix = this.matrix;
181 let logger = this.logger; 181 let logger = this.logger;
182 182
183 if (!matrix.buddies[partner]['last_outgoing']) { 183 if (!matrix.buddies[partner]['last_outgoing']) {
184 logger.verbose('No outgoing yet, skip updateLastResponseTime'); 184 logger.verbose('No outgoing yet, skip updateLastResponseTime');
185 return; 185 return;
186 } 186 }
187 187
188 if (!matrix.buddies[partner]['last_outgoing']['last_update_ts']) { 188 if (!matrix.buddies[partner]['last_outgoing']['last_update_ts']) {
189 logger.verbose('No outgoing timestamp yet, skip updateLastResponseTime'); 189 logger.verbose('No outgoing timestamp yet, skip updateLastResponseTime');
190 return; 190 return;
191 } 191 }
192 192
193 if ( 193 if (
194 matrix.buddies[partner]['last_incoming'] 194 matrix.buddies[partner]['last_incoming']
195 && (Number(matrix.buddies[partner]['last_incoming']['last_update_ts']) > Number(matrix.buddies[partner]['last_outgoing']['last_update_ts'])) 195 && (Number(matrix.buddies[partner]['last_incoming']['last_update_ts']) > Number(matrix.buddies[partner]['last_outgoing']['last_update_ts']))
196 ) { 196 ) {
197 return; 197 return;
198 } 198 }
199 199
200 let delta = Date.now() - Number(matrix.buddies[partner]['last_outgoing']['last_update_ts']); 200 let delta = Date.now() - Number(matrix.buddies[partner]['last_outgoing']['last_update_ts']);
201 delta = (delta / 1000).toFixed(2); 201 delta = (delta / 1000).toFixed(2);
202 logger.verbose('MatrixUtil: Response time in ' + delta + ' seconds', {partner: partner}); 202 logger.verbose('MatrixUtil: Response time in ' + delta + ' seconds', {partner: partner});
203 matrix.buddies[partner]['last_response_time_in_secs'] = delta; 203 matrix.buddies[partner]['last_response_time_in_secs'] = delta;
204 } 204 }
205 205
206 MatrixUtil.prototype._updateLastMessage = function(partner, msg, direction) { 206 MatrixUtil.prototype._updateLastMessage = function(partner, msg, direction) {
207 if (!partner) { return; } 207 if (!partner) { return; }
208 partner = _cleanPartnerId(partner); 208 partner = _cleanPartnerId(partner);
209 209
210 let matrix = this.matrix; 210 let matrix = this.matrix;
211 let logger = this.logger; 211 let logger = this.logger;
212 212
213 if (!matrix) { 213 if (!matrix) {
214 return; 214 return;
215 } 215 }
216 216
217 if (!matrix.buddies) { 217 if (!matrix.buddies) {
218 matrix.buddies = {}; 218 matrix.buddies = {};
219 } 219 }
220 220
221 if (!matrix.buddies[partner]) { 221 if (!matrix.buddies[partner]) {
222 matrix.buddies[partner] = {}; 222 matrix.buddies[partner] = {};
223 } 223 }
224 224
225 if (direction == 'incoming') { 225 if (direction == 'incoming') {
226 try { 226 try {
227 this._updateLastResponseTime(partner); 227 this._updateLastResponseTime(partner);
228 } 228 }
229 catch(e) { 229 catch(e) {
230 logger.warn('Exception when updateLastResponseTime', {err: e}); 230 logger.warn('Exception when updateLastResponseTime', {err: e});
231 } 231 }
232 } 232 }
233 233
234 matrix.buddies[partner]['waiting_for_response'] = (direction == 'outgoing'); 234 matrix.buddies[partner]['waiting_for_response'] = (direction == 'outgoing');
235 235
236 matrix.buddies[partner]['last_' + direction] = { 236 matrix.buddies[partner]['last_' + direction] = {
237 msg: msg, 237 msg: msg,
238 last_update: moment().format(momentFormat), 238 last_update: moment().format(momentFormat),
239 last_update_ts: Date.now() 239 last_update_ts: Date.now()
240 } 240 }
241 241
242 if (direction == 'outgoing') { 242 if (direction == 'outgoing') {
243 return; 243 return;
244 } 244 }
245 } 245 }
246 246
247 MatrixUtil.prototype.updateLastIncoming = function(partner, msg) { 247 MatrixUtil.prototype.updateLastIncoming = function(partner, msg) {
248 this._updateLastMessage(partner, msg, 'incoming'); 248 this._updateLastMessage(partner, msg, 'incoming');
249 } 249 }
250 250
251 MatrixUtil.prototype.updateLastOutgoing = function(partner, msg) { 251 MatrixUtil.prototype.updateLastOutgoing = function(partner, msg) {
252 this._updateLastMessage(partner, msg, 'outgoing'); 252 this._updateLastMessage(partner, msg, 'outgoing');
253 } 253 }
254 254