Skip to content

Commit

Permalink
Refresh server connection when MI server settings change (#24649)
Browse files Browse the repository at this point in the history
  • Loading branch information
KevLehman authored Feb 28, 2022
1 parent a523503 commit d50e3ec
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
4 changes: 2 additions & 2 deletions app/lib/server/startup/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -3176,7 +3176,7 @@ settingsRegistry.addGroup('Call_Center', function () {
},
});
this.add('VoIP_Server_Name', '', {
type: 'int',
type: 'string',
public: true,
enableQuery: {
_id: 'VoIP_Enabled',
Expand Down
13 changes: 13 additions & 0 deletions app/voip/server/startup.ts
Original file line number Diff line number Diff line change
@@ -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();
},
);
1 change: 1 addition & 0 deletions server/sdk/types/IVoipService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ export interface IVoipService {
cachedQueueDetails(): () => Promise<{ name: string; members: string[] }[]>;
init(): Promise<void>;
stop(): Promise<void>;
refresh(): Promise<void>;
}
5 changes: 5 additions & 0 deletions server/services/voip/connector/asterisk/CommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 8 additions & 2 deletions server/services/voip/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
}

Expand All @@ -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<void> {
this.logger.info('Restarting VoIP service due to settings changes');
await this.stop();
await this.init();
}

getServerConfigData(type: ServerType): IVoipCallServerConfig | IVoipManagementServerConfig {
return getServerConfigDataFromSettings(type);
}
Expand Down

0 comments on commit d50e3ec

Please sign in to comment.