Skip to content

Commit 3350f02

Browse files
cancitGitHub Enterprise
authored andcommitted
FM-852: add setWorkerOfflineIfDisconnected param (#412)
1 parent bfa3ee7 commit 3350f02

File tree

5 files changed

+30
-3
lines changed

5 files changed

+30
-3
lines changed

lib/Worker.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ class Worker extends EventEmitter {
145145
* @private
146146
* @type {EventBridgeSignaling}
147147
*/
148-
this._signaling = new deps.EventBridgeSignaling(this, { closeExistingSessions: options.closeExistingSessions });
148+
this._signaling = new deps.EventBridgeSignaling(this, {
149+
closeExistingSessions: options.closeExistingSessions,
150+
setWorkerOfflineIfDisconnected: options.setWorkerOfflineIfDisconnected,
151+
});
149152
/**
150153
* @private
151154
* @type {RetryUtil}
@@ -715,6 +718,7 @@ export default Worker;
715718
* @property {string} [connectActivitySid=''] - The {@link Activity} state of the Worker upon connect
716719
* @property {boolean} [closeExistingSessions=false] - - Whether other open sessions of this {@link Worker}
717720
* should be terminated
721+
* @property {boolean} [setWorkerOfflineIfDisconnected=true] - A boolean defining whether if {@link Worker} availability set as Offline when the connection is terminated
718722
* @property {string} [logLevel='error'] - The level of logging to enable
719723
* ['error', 'warn', 'info', 'debug', 'trace', 'silent']
720724
* @property {string} [region] - the realm for connections (ex. "stage-us1")

lib/signaling/EventBridgeSignaling.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const CLIENT_VERSION = packageJson.version;
1616
/**
1717
* @typedef {Object} EventBridgeSignal.Options
1818
* @property {boolean} [closeExistingSessions=false] - A boolean defining whether other open sessions of the {@link Worker} should be terminated
19+
* @property {boolean} [setWorkerOfflineIfDisconnected=true] - A boolean defining whether if {@link Worker} availability set as Offline when the connection is terminated
1920
*/
2021

2122
/**
@@ -88,6 +89,10 @@ export default class EventBridgeSignaling extends EventEmitter {
8889
* @type {boolean}
8990
*/
9091
this.closeExistingSessions = options.closeExistingSessions || false;
92+
/**
93+
* @type {boolean}
94+
*/
95+
this.setWorkerOfflineIfDisconnected = options.setWorkerOfflineIfDisconnected;
9196
/**
9297
* @private
9398
* @type {import('../util/Configuration')}
@@ -181,7 +186,11 @@ export default class EventBridgeSignaling extends EventEmitter {
181186

182187
this._log.debug('createWebSocket called');
183188

184-
const queryParam = `?${EB_URL_PARAMS.TOKEN}=${this._config.token}&${EB_URL_PARAMS.CLOSE_EXISTING_SESSIONS}=${this.closeExistingSessions}&${EB_URL_PARAMS.CLIENT_VERSION}=${CLIENT_VERSION}`;
189+
let queryParam = `?${EB_URL_PARAMS.TOKEN}=${this._config.token}&${EB_URL_PARAMS.CLOSE_EXISTING_SESSIONS}=${this.closeExistingSessions}&${EB_URL_PARAMS.CLIENT_VERSION}=${CLIENT_VERSION}`;
190+
if (typeof this.setWorkerOfflineIfDisconnected !== 'undefined') {
191+
queryParam = queryParam.concat(`&${EB_URL_PARAMS.SET_WORKER_OFFLINE_IF_DISCONNECTED}=${this.setWorkerOfflineIfDisconnected}`);
192+
}
193+
this._log.debug('Websocket Connection URL', this._config.WS_SERVER + queryParam);
185194
this.webSocket = new WS(this._config.WS_SERVER + queryParam);
186195

187196
this._log.debug('New websocket created');

lib/util/Constants.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ export const DEFAULT_HTTP_TIMEOUT = 15000;
122122
export const EB_URL_PARAMS = {
123123
TOKEN: 'token',
124124
CLOSE_EXISTING_SESSIONS: 'closeExistingSessions',
125-
CLIENT_VERSION: 'clientVersion'
125+
CLIENT_VERSION: 'clientVersion',
126+
SET_WORKER_OFFLINE_IF_DISCONNECTED: 'setWorkerOfflineIfDisconnected'
126127
};
127128

128129
// Define common set of Twilio errors

test/unit/spec/signaling/EventBridgeSignaling.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ describe('EventBridgeSignaling', () => {
4949
const signaling = new EventBridgeSignaling(worker);
5050
assert.isFalse(signaling.closeExistingSessions);
5151
});
52+
53+
it('should set setWorkerOfflineIfDisconnected option', () => {
54+
const worker = new Worker(initialToken, WorkerConfig);
55+
const options = {
56+
setWorkerOfflineIfDisconnected: false
57+
};
58+
59+
const signaling = new EventBridgeSignaling(worker, options);
60+
61+
assert.isFalse(signaling.setWorkerOfflineIfDisconnected);
62+
});
63+
5264
});
5365

5466
describe('#updateToken(newToken)', () => {

types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ export interface ReservationParticipantOptions {
326326
export interface WorkerOptions {
327327
connectActivitySid?: string;
328328
closeExistingSessions?: boolean;
329+
setWorkerOfflineIfDisconnected?: boolean
329330
logLevel?: "error" | "warn" | "info" | "debug" | "trace" | "silent";
330331
ebServer?: string;
331332
wsServer?: string;

0 commit comments

Comments
 (0)