Skip to content

Commit e32ec4e

Browse files
committed
תיקון פונקציונליות timeout
איפוס הטיימר בכל בקשה חדשה, ותיקונים נוספים
1 parent 047dc30 commit e32ec4e

File tree

3 files changed

+33
-43
lines changed

3 files changed

+33
-43
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ npm i yemot-router2
5454
**5.1.2**
5555
תוקנה התמיכה בבקשות POST (ההגדרה api_url_post=yes בשלוחה), שבהן הפרמטרים נשלחים בbody ולא בquery
5656
נוסף פרוקסי שמיירט נסיון גישה לreq.query בבקשות POST או לreq.body בבקשות GET, ומציג הסבר מפורט לתיקון.
57+
58+
**5.1.3**
59+
תוקנה האופציה timeout באתחול הראוטר.
5760
</details>
5861

5962
<details>

lib/call.js

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ class Call {
4646
this.globalResponseTxt = '';
4747
}
4848

49-
await this.blockRunningUntilNextRequest();
50-
5149
this.send(responseText);
5250

5351
await this.blockRunningUntilNextRequest();
@@ -155,34 +153,29 @@ class Call {
155153
}
156154

157155
async blockRunningUntilNextRequest () {
158-
if (!this.res._headerSent) return; // TODO: check if this is needed. legacy code
159-
if (this.ops.printLog) this.logger('🔒 fn running blocked - wait to next request from yemot');
156+
if (this.ops.printLog) this.logger('🔒 fn running blocked - waiting to next request from yemot');
160157
return new Promise((resolve, reject) => {
158+
if (this.ops.timeout) {
159+
clearTimeout(this._timeoutId);
160+
this._timeoutId = setTimeout(() => {
161+
if (!this.res._headerSent) this.res.json({ message: 'timeout' });
162+
reject(new TimeoutError(this));
163+
}, this.ops.timeout);
164+
}
161165
this.eventsEmitter.once(this.callId, (isHangup) => {
162166
this.logger('🔓 fn running unblocked');
163-
if (this.ops.timeout) {
164-
setTimeout(() => {
165-
this.onTimeout(reject);
166-
}, this.ops.timeout);
167-
}
167+
if (this._timeoutId) {
168+
clearTimeout(this._timeoutId);
169+
};
168170
if (isHangup) {
169-
this.onHangup(reject);
171+
if (!this.res._headerSent) this.res.json({ message: 'hangup' });
172+
reject(new HangupError(this));
170173
} else {
171174
resolve(isHangup);
172175
}
173176
});
174177
});
175178
}
176-
177-
onHangup (reject) {
178-
this.res.json({ message: 'hangup' });
179-
reject(new HangupError(this));
180-
}
181-
182-
onTimeout (reject) {
183-
this.res.json({ message: 'timeout' });
184-
reject(new TimeoutError(this));
185-
}
186179
}
187180

188181
module.exports = Call;

lib/yemot_router.js

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ function YemotRouter (options = {}) {
2121
timeout: options.timeout || 0,
2222
uncaughtErrorsHandler: options.uncaughtErrorsHandler || null
2323
};
24+
25+
if (ops.timeout && Number.isNaN(ops.timeout)) {
26+
throw new Error('YemotRouter: if you set timeout option, it must be a number');
27+
}
28+
2429
const activeCalls = {};
2530
const eventsEmitter = new EventEmitter();
2631

@@ -29,43 +34,32 @@ function YemotRouter (options = {}) {
2934
console.log(colors[color](`[${callId}]: ${msg}`));
3035
}
3136

32-
function onFnDone (callId) {
33-
delete activeCalls[callId];
34-
logger(callId, '🆗 the function is done');
35-
}
36-
37-
function onExit (callId) {
38-
delete activeCalls[callId];
39-
logger(callId, '👋 call is exit');
40-
}
41-
42-
function onHangup (callId) {
43-
delete activeCalls[callId];
44-
logger(callId, '👋 call is hangup');
45-
}
46-
47-
function onTimeout (callId) {
37+
function deleteCall (callId) {
38+
if (activeCalls[callId]?._timeoutId) {
39+
clearTimeout(activeCalls[callId]._timeoutId);
40+
}
4841
delete activeCalls[callId];
49-
logger(callId, 'timeout for receiving a response from the caller. the call has been deleted from active calls', 'red');
5042
}
5143

5244
async function makeNewCall (fn, callId, call) {
5345
try {
5446
await fn(call);
55-
onFnDone(callId);
47+
deleteCall(callId);
48+
logger(callId, '🆗 the function is done');
5649
} catch (error) {
50+
deleteCall(callId);
51+
5752
// טיפול בשגיאות מלאכותיות שנועדו לעצור את ריצת הפונקציה - בניתוק לדוגמה
5853
if (error instanceof HangupError) {
59-
onHangup(callId);
54+
logger(callId, '👋 call is hangup');
6055
} else if (error instanceof TimeoutError) {
61-
onTimeout(callId);
56+
logger(callId, 'the timeout for the caller\'s answer has expired. the call function has killed and been removed from the active calls');
6257
} else if (error instanceof ExitError) {
63-
onExit(callId);
58+
logger(callId, '👋 call is exit');
6459
} else {
6560
if (ops.uncaughtErrorsHandler) {
6661
logger(callId, '💥 Uncaught error. applying uncaughtErrorsHandler', 'red');
6762
ops.uncaughtErrorsHandler(error, call);
68-
delete activeCalls[callId];
6963
} else {
7064
logger(callId, '💥 Uncaught error. no uncaughtErrorsHandler, throwing error', 'red');
7165
throw error;
@@ -90,7 +84,7 @@ function YemotRouter (options = {}) {
9084
const values = req.method === 'POST' ? req.body : req.query;
9185
const requiredValues = ['ApiPhone', 'ApiDID', 'ApiExtension', 'ApiCallId'];
9286
if (requiredValues.some((key) => !Object.prototype.hasOwnProperty.call(values, key))) {
93-
return res.json({ message: 'request is not valid yemot request' });
87+
return res.json({ message: 'the request is not valid yemot request' });
9488
}
9589

9690
if (req.method === 'POST') {

0 commit comments

Comments
 (0)