From 4673b4c012b2a13b2ac91e4bb6cfea37fa283358 Mon Sep 17 00:00:00 2001 From: Kevin Aleman Date: Mon, 28 Feb 2022 11:56:30 -0600 Subject: [PATCH] Refresh server connection when MI server settings change --- app/lib/server/startup/settings.ts | 4 ++-- app/voip/server/startup.ts | 13 +++++++++++++ server/sdk/types/IVoipService.ts | 1 + .../voip/connector/asterisk/CommandHandler.ts | 5 +++++ server/services/voip/service.ts | 10 ++++++++-- 5 files changed, 29 insertions(+), 4 deletions(-) diff --git a/app/lib/server/startup/settings.ts b/app/lib/server/startup/settings.ts index 51cd43e91a10..7c1c2f670d8a 100644 --- a/app/lib/server/startup/settings.ts +++ b/app/lib/server/startup/settings.ts @@ -3160,7 +3160,7 @@ settingsRegistry.addGroup('Call_Center', function () { }); this.section('Server_Configuration', function () { this.add('VoIP_Server_Host', '', { - type: 'password', + type: 'string', public: true, enableQuery: { _id: 'VoIP_Enabled', @@ -3176,7 +3176,7 @@ settingsRegistry.addGroup('Call_Center', function () { }, }); this.add('VoIP_Server_Name', '', { - type: 'int', + type: 'string', public: true, enableQuery: { _id: 'VoIP_Enabled', diff --git a/app/voip/server/startup.ts b/app/voip/server/startup.ts index 1220b5bb2ee2..06703d944d77 100644 --- a/app/voip/server/startup.ts +++ b/app/voip/server/startup.ts @@ -1,6 +1,19 @@ +import debounce from 'lodash.debounce'; + import { settings } from '../../settings/server'; import { Voip } from '../../../server/sdk'; +const debouncedRefresh = debounce(Voip.refresh, 1000); + settings.watch('VoIP_Enabled', (value: boolean) => { return value ? Voip.init() : Voip.stop(); }); + +settings.changeMultiple( + ['VoIP_Management_Server_Host', 'VoIP_Management_Server_Port', 'VoIP_Management_Server_Username', 'VoIP_Management_Server_Password'], + (_values) => { + // Here, if 4 settings are changed at once, we're getting 4 diff callbacks. The good part is that all callbacks are fired almost instantly + // So to avoid stopping/starting voip too often, we debounce the call and restart 1 second after the last setting has reached us. + return debouncedRefresh(); + }, +); diff --git a/server/sdk/types/IVoipService.ts b/server/sdk/types/IVoipService.ts index 60997559f8cf..78f42792bb01 100644 --- a/server/sdk/types/IVoipService.ts +++ b/server/sdk/types/IVoipService.ts @@ -19,4 +19,5 @@ export interface IVoipService { cachedQueueDetails(): () => Promise<{ name: string; members: string[] }[]>; init(): Promise; stop(): Promise; + refresh(): Promise; } diff --git a/server/services/voip/connector/asterisk/CommandHandler.ts b/server/services/voip/connector/asterisk/CommandHandler.ts index 0dc3175f3cf5..b74e7ceee2fa 100644 --- a/server/services/voip/connector/asterisk/CommandHandler.ts +++ b/server/services/voip/connector/asterisk/CommandHandler.ts @@ -151,6 +151,11 @@ export class CommandHandler { } stop(): void { + if (!this.continuousMonitor) { + // service is already stopped or was never initialized + return; + } + this.continuousMonitor.cleanMonitor(); for (const connection of this.connections.values()) { connection.closeConnection(); diff --git a/server/services/voip/service.ts b/server/services/voip/service.ts index 3e780d61df30..127f3d66d734 100644 --- a/server/services/voip/service.ts +++ b/server/services/voip/service.ts @@ -51,7 +51,7 @@ export class VoipService extends ServiceClassInternal implements IVoipService { await this.commandHandler.initConnection(CommandType.AMI); this.logger.info('VoIP service started'); } catch (err) { - this.logger.error('Error initializing VOIP service', err); + this.logger.error({ msg: 'Error initializing VOIP service', err }); } } @@ -61,10 +61,16 @@ export class VoipService extends ServiceClassInternal implements IVoipService { this.commandHandler.stop(); this.logger.info('VoIP service stopped'); } catch (err) { - this.logger.error('Error stopping VoIP service', err); + this.logger.error({ msg: 'Error stopping VoIP service', err }); } } + async refresh(): Promise { + this.logger.info('Restarting VoIP service due to settings changes'); + await this.stop(); + await this.init(); + } + getServerConfigData(type: ServerType): IVoipCallServerConfig | IVoipManagementServerConfig { return getServerConfigDataFromSettings(type); }