Commit 3fd857e00bec1d18b5c11cac91c13a3bc3ff3190

Authored by Adhidarma Hadiwinoto
1 parent dd043a5783
Exists in master

hapus promise tak disengaja

Showing 1 changed file with 2 additions and 2 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 || !config.globals.max_healthy_wait_ms) { return defaultMaxHealthyWaitMs; } 26 if (!config || !config.globals || !config.globals.max_healthy_wait_ms) { return defaultMaxHealthyWaitMs; }
27 return config.globals.max_healthy_wait_ms; 27 return 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 (!this.config) new Promise(function(resolve, reject) { 49 if (!this.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' && matrix.buddies[jid].resources['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[partner]) { return true; } 111 if (!matrix.buddies[partner]) { return true; }
112 if (!matrix.buddies[partner].resources) { return true; }; 112 if (!matrix.buddies[partner].resources) { return true; };
113 113
114 let resources = matrix.buddies[partner].resources; 114 let resources = matrix.buddies[partner].resources;
115 for (let key in resources) { 115 for (let key in resources) {
116 if (resources.hasOwnProperty(key)) { 116 if (resources.hasOwnProperty(key)) {
117 let resource = resources[key]; 117 let resource = resources[key];
118 if (resources[key].state == 'online') { 118 if (resources[key].state == 'online') {
119 return false; 119 return false;
120 } 120 }
121 } 121 }
122 } 122 }
123 logger.verbose('Offline partner detected: ' + partner); 123 logger.verbose('Offline partner detected: ' + partner);
124 return true; 124 return true;
125 } 125 }
126 126
127 MatrixUtil.prototype._isPartnerHealthy = function(partner) { 127 MatrixUtil.prototype._isPartnerHealthy = function(partner) {
128 if (!partner) { return; } 128 if (!partner) { return; }
129 partner = _cleanPartnerId(partner); 129 partner = _cleanPartnerId(partner);
130 130
131 if (this.isPartnerOffline(partner)) { 131 if (this.isPartnerOffline(partner)) {
132 return; 132 return;
133 } 133 }
134 134
135 let matrix = this.matrix; 135 let matrix = this.matrix;
136 let logger = this.logger; 136 let logger = this.logger;
137 137
138 if (!matrix) { return false; } 138 if (!matrix) { return false; }
139 139
140 if (!matrix.buddies[partner]) { return false; } 140 if (!matrix.buddies[partner]) { return false; }
141 if (!matrix.buddies[partner]['waiting_for_response']) { return true; } 141 if (!matrix.buddies[partner]['waiting_for_response']) { return true; }
142 if (!matrix.buddies[partner]['last_incoming']) { return false; } 142 if (!matrix.buddies[partner]['last_incoming']) { return false; }
143 if (!matrix.buddies[partner]['last_incoming']['last_update_ts']) { return false; } 143 if (!matrix.buddies[partner]['last_incoming']['last_update_ts']) { return false; }
144 144
145 let delta = Date.now() - Number(matrix.buddies[partner]['last_incoming']['last_update_ts']); 145 let delta = Date.now() - Number(matrix.buddies[partner]['last_incoming']['last_update_ts']);
146 let isHealthy = delta <= getMaxHealthyWaitMs(); 146 let isHealthy = delta <= getMaxHealthyWaitMs();
147 147
148 logger.verbose('Partner healthy analized', {partner: partner, isHealthy: isHealthy, delta: delta, maxHealthyWaitMs: getMaxHealthyWaitMs()}); 148 logger.verbose('Partner healthy analized', {partner: partner, isHealthy: isHealthy, delta: delta, maxHealthyWaitMs: getMaxHealthyWaitMs()});
149 return isHealthy; 149 return isHealthy;
150 } 150 }
151 151
152 MatrixUtil.prototype.isPartnerHealthy = function(partner) { 152 MatrixUtil.prototype.isPartnerHealthy = function(partner) {
153 let matrix = this.matrix; 153 let matrix = this.matrix;
154 154
155 let isHealthy = this._isPartnerHealthy(partner); 155 let isHealthy = this._isPartnerHealthy(partner);
156 156
157 if (!matrix.healthy_partners) { 157 if (!matrix.healthy_partners) {
158 matrix.healthy_partners = []; 158 matrix.healthy_partners = [];
159 } 159 }
160 160
161 // update matrix 161 // update matrix
162 let idx = matrix.healthy_partners.indexOf(partner); 162 let idx = matrix.healthy_partners.indexOf(partner);
163 163
164 if (isHealthy) { 164 if (isHealthy) {
165 if (idx < 0) { 165 if (idx < 0) {
166 matrix.healthy_partners.push(partner); 166 matrix.healthy_partners.push(partner);
167 } 167 }
168 } 168 }
169 else { 169 else {
170 if (idx > -1) { 170 if (idx > -1) {
171 matrix.healthy_partners.splice(idx, 1); 171 matrix.healthy_partners.splice(idx, 1);
172 } 172 }
173 } 173 }
174 174
175 return isHealthy; 175 return isHealthy;
176 } 176 }
177 177
178 MatrixUtil.prototype._updateLastResponseTime = function(partner) { 178 MatrixUtil.prototype._updateLastResponseTime = function(partner) {
179 let matrix = this.matrix; 179 let matrix = this.matrix;
180 let logger = this.logger; 180 let logger = this.logger;
181 181
182 if (!matrix.buddies[partner]['last_outgoing']) { 182 if (!matrix.buddies[partner]['last_outgoing']) {
183 logger.verbose('No outgoing yet, skip updateLastResponseTime'); 183 logger.verbose('No outgoing yet, skip updateLastResponseTime');
184 return; 184 return;
185 } 185 }
186 186
187 if (!matrix.buddies[partner]['last_outgoing']['last_update_ts']) { 187 if (!matrix.buddies[partner]['last_outgoing']['last_update_ts']) {
188 logger.verbose('No outgoing timestamp yet, skip updateLastResponseTime'); 188 logger.verbose('No outgoing timestamp yet, skip updateLastResponseTime');
189 return; 189 return;
190 } 190 }
191 191
192 if ( 192 if (
193 matrix.buddies[partner]['last_incoming'] 193 matrix.buddies[partner]['last_incoming']
194 && (Number(matrix.buddies[partner]['last_incoming']['last_update_ts']) > Number(matrix.buddies[partner]['last_outgoing']['last_update_ts'])) 194 && (Number(matrix.buddies[partner]['last_incoming']['last_update_ts']) > Number(matrix.buddies[partner]['last_outgoing']['last_update_ts']))
195 ) { 195 ) {
196 return; 196 return;
197 } 197 }
198 198
199 let delta = Date.now() - Number(matrix.buddies[partner]['last_outgoing']['last_update_ts']); 199 let delta = Date.now() - Number(matrix.buddies[partner]['last_outgoing']['last_update_ts']);
200 delta = (delta / 1000).toFixed(2); 200 delta = (delta / 1000).toFixed(2);
201 logger.verbose('MatrixUtil: Response time in ' + delta + ' seconds', {partner: partner}); 201 logger.verbose('MatrixUtil: Response time in ' + delta + ' seconds', {partner: partner});
202 matrix.buddies[partner]['last_response_time_in_secs'] = delta; 202 matrix.buddies[partner]['last_response_time_in_secs'] = delta;
203 } 203 }
204 204
205 MatrixUtil.prototype._updateLastMessage = function(partner, msg, direction) { 205 MatrixUtil.prototype._updateLastMessage = function(partner, msg, direction) {
206 if (!partner) { return; } 206 if (!partner) { return; }
207 partner = _cleanPartnerId(partner); 207 partner = _cleanPartnerId(partner);
208 208
209 let matrix = this.matrix; 209 let matrix = this.matrix;
210 let logger = this.logger; 210 let logger = this.logger;
211 211
212 if (!matrix) { 212 if (!matrix) {
213 return; 213 return;
214 } 214 }
215 215
216 if (!matrix.buddies) { 216 if (!matrix.buddies) {
217 matrix.buddies = {}; 217 matrix.buddies = {};
218 } 218 }
219 219
220 if (!matrix.buddies[partner]) { 220 if (!matrix.buddies[partner]) {
221 matrix.buddies[partner] = {}; 221 matrix.buddies[partner] = {};
222 } 222 }
223 223
224 if (direction == 'incoming') { 224 if (direction == 'incoming') {
225 try { 225 try {
226 this._updateLastResponseTime(partner); 226 this._updateLastResponseTime(partner);
227 } 227 }
228 catch(e) { 228 catch(e) {
229 logger.warn('Exception when updateLastResponseTime', {err: e}); 229 logger.warn('Exception when updateLastResponseTime', {err: e});
230 } 230 }
231 } 231 }
232 232
233 matrix.buddies[partner]['waiting_for_response'] = (direction == 'outgoing'); 233 matrix.buddies[partner]['waiting_for_response'] = (direction == 'outgoing');
234 234
235 matrix.buddies[partner]['last_' + direction] = { 235 matrix.buddies[partner]['last_' + direction] = {
236 msg: msg, 236 msg: msg,
237 last_update: moment().format(momentFormat), 237 last_update: moment().format(momentFormat),
238 last_update_ts: Date.now() 238 last_update_ts: Date.now()
239 } 239 }
240 240
241 if (direction == 'outgoing') { 241 if (direction == 'outgoing') {
242 return; 242 return;
243 } 243 }
244 } 244 }
245 245
246 MatrixUtil.prototype.updateLastIncoming = function(partner, msg) { 246 MatrixUtil.prototype.updateLastIncoming = function(partner, msg) {
247 this._updateLastMessage(partner, msg, 'incoming'); 247 this._updateLastMessage(partner, msg, 'incoming');
248 } 248 }
249 249
250 MatrixUtil.prototype.updateLastOutgoing = function(partner, msg) { 250 MatrixUtil.prototype.updateLastOutgoing = function(partner, msg) {
251 this._updateLastMessage(partner, msg, 'outgoing'); 251 this._updateLastMessage(partner, msg, 'outgoing');
252 } 252 }
253 253