()
| 16 | |
| 17 | |
| 18 | function initManagementClient() { |
| 19 | const { host, port } = _config.managementAgent; |
| 20 | |
| 21 | const ws = new WebSocket(`ws://${host}:${port}/watch`); |
| 22 | |
| 23 | ws.on('open', () => { |
| 24 | logger.info('connected with management agent'); |
| 25 | }); |
| 26 | |
| 27 | ws.on('close', (code, reason) => { |
| 28 | logger.info('disconnected from management agent', { reason }); |
| 29 | setTimeout(initManagementClient, CONNECTION_RETRY_TIMEOUT_MS); |
| 30 | }); |
| 31 | |
| 32 | ws.on('error', error => { |
| 33 | logger.error('error on connection with management agent', { error }); |
| 34 | }); |
| 35 | |
| 36 | ws.on('message', data => { |
| 37 | const method = 'initManagementclient::onMessage'; |
| 38 | const log = logger.newRequestLogger(); |
| 39 | let msg; |
| 40 | |
| 41 | if (!data) { |
| 42 | log.error('message without data', { method }); |
| 43 | return; |
| 44 | } |
| 45 | try { |
| 46 | msg = JSON.parse(data); |
| 47 | } catch (err) { |
| 48 | log.error('data is an invalid json', { method, err, data }); |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | if (msg.payload === undefined) { |
| 53 | log.error('message without payload', { method }); |
| 54 | return; |
| 55 | } |
| 56 | if (typeof msg.messageType !== 'number') { |
| 57 | log.error('messageType is not an integer', { |
| 58 | type: typeof msg.messageType, |
| 59 | method, |
| 60 | }); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | switch (msg.messageType) { |
| 65 | case managementAgentMessageType.NEW_OVERLAY: |
| 66 | patchConfiguration(msg.payload, log, err => { |
| 67 | if (err) { |
| 68 | log.error('failed to patch overlay', { |
| 69 | error: reshapeExceptionError(err), |
| 70 | method, |
| 71 | }); |
| 72 | } |
| 73 | }); |
| 74 | return; |
| 75 | default: |
no test coverage detected