Skip to content

Commit

Permalink
fix(health-signal): log signal when forwarding with disabled http
Browse files Browse the repository at this point in the history
  • Loading branch information
burgerni10 authored and Nicolas Burger committed Sep 26, 2022
1 parent f9732f4 commit c92bdfd
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 122 deletions.
4 changes: 2 additions & 2 deletions src/client/Engine/HealthSignal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ const HealthSignal = ({ onChange, healthSignal }) => (
label="Endpoint"
name="engine.healthSignal.http.endpoint"
value={healthSignal.http.endpoint}
defaultValue=""
defaultValue="/engine/aliveSignal"
valid={validation.engine.healthSignal.http.endpoint}
help={<div>The endpoint send the health signal</div>}
help={<div>The endpoint send the health signal.</div>}
onChange={onChange}
/>
</Col>
Expand Down
12 changes: 6 additions & 6 deletions src/client/Engine/__snapshots__/Engine.spec.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3054,7 +3054,7 @@ exports[`Engine check Engine 1`] = `
class="form-text text-muted"
>
<div>
The endpoint send the health signal
The endpoint send the health signal.
</div>
</small>
</div>
Expand Down Expand Up @@ -6181,7 +6181,7 @@ exports[`Engine check Engine when config has no proxies 1`] = `
class="form-text text-muted"
>
<div>
The endpoint send the health signal
The endpoint send the health signal.
</div>
</small>
</div>
Expand Down Expand Up @@ -9732,7 +9732,7 @@ exports[`Engine check change engineName 1`] = `
class="form-text text-muted"
>
<div>
The endpoint send the health signal
The endpoint send the health signal.
</div>
</small>
</div>
Expand Down Expand Up @@ -13278,7 +13278,7 @@ exports[`Engine check change password 1`] = `
class="form-text text-muted"
>
<div>
The endpoint send the health signal
The endpoint send the health signal.
</div>
</small>
</div>
Expand Down Expand Up @@ -16824,7 +16824,7 @@ exports[`Engine check change port 1`] = `
class="form-text text-muted"
>
<div>
The endpoint send the health signal
The endpoint send the health signal.
</div>
</small>
</div>
Expand Down Expand Up @@ -20370,7 +20370,7 @@ exports[`Engine check change user 1`] = `
class="form-text text-muted"
>
<div>
The endpoint send the health signal
The endpoint send the health signal.
</div>
</small>
</div>
Expand Down
6 changes: 3 additions & 3 deletions src/client/Engine/__snapshots__/HealthSignal.spec.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ exports[`HealthSignal check HealthSignal 1`] = `
class="form-text text-muted"
>
<div>
The endpoint send the health signal
The endpoint send the health signal.
</div>
</small>
</div>
Expand Down Expand Up @@ -617,7 +617,7 @@ exports[`HealthSignal check change healthSignal http to false 1`] = `
class="form-text text-muted"
>
<div>
The endpoint send the health signal
The endpoint send the health signal.
</div>
</small>
</div>
Expand Down Expand Up @@ -1019,7 +1019,7 @@ exports[`HealthSignal check change healthSignal log to false 1`] = `
class="form-text text-muted"
>
<div>
The endpoint send the health signal
The endpoint send the health signal.
</div>
</small>
</div>
Expand Down
53 changes: 30 additions & 23 deletions src/engine/HealthSignal.class.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Class HealthSignal - sends health signal to a remote host
* Class HealthSignal - sends health signal to a remote host or into the logs
*/
class HealthSignal {
/**
Expand All @@ -18,7 +18,7 @@ class HealthSignal {
this.logging = logging

this.http = http
this.http.proxy = Array.isArray(engineConfig.proxies) && engineConfig.proxies.find(({ name }) => name === this.http.proxy)
this.http.proxy = Array.isArray(engineConfig.proxies) ? engineConfig.proxies.find(({ name }) => name === this.http.proxy) : null
this.httpTimer = null
this.loggingTimer = null
this.engineName = engineConfig.engineName
Expand All @@ -30,11 +30,11 @@ class HealthSignal {
*/
start() {
if (this.http.enabled) {
this.logger.info('Initializing http health signal')
this.logger.debug('Initializing HTTP health signal.')
this.httpTimer = setInterval(this.sendHttpSignal.bind(this), this.http.frequency * 1000)
}
if (this.logging.enabled) {
this.logger.info('Initializing logging health signal')
this.logger.debug('Initializing logging health signal.')
this.loggingTimer = setInterval(this.sendLoggingSignal.bind(this), this.logging.frequency * 1000)
}
}
Expand All @@ -57,9 +57,7 @@ class HealthSignal {
* @return {Promise<void>} - The response
*/
async sendHttpSignal() {
this.logger.trace('sendHttpSignal')

const healthStatus = await this.prepareStatus(this.http.verbose)
const healthStatus = this.prepareStatus(this.http.verbose)
healthStatus.id = this.engineName
try {
const data = JSON.stringify(healthStatus)
Expand All @@ -72,48 +70,57 @@ class HealthSignal {
data,
headers,
)
this.logger.debug('Health signal successful')
this.logger.debug('HTTP health signal sent successfully.')
} catch (error) {
this.logger.error(`sendRequest error status: ${error}`)
this.logger.error(error)
}
}

/**
* Callback to send the health signal with logger.
* @returns {void}
*/
async sendLoggingSignal() {
this.logger.trace('sendHttpSignal')

const healthStatus = await this.prepareStatus(true)
sendLoggingSignal() {
const healthStatus = this.prepareStatus(true)
this.logger.info(JSON.stringify(healthStatus))
}

/**
* Retrieve status information from the engine
* @param {boolean} verbose - return only the id when false, return full status when true
* @returns {object} - the status of OIBus
* @param {Boolean} verbose - Return only the static OIBus info when false, return full status when true
* @returns {Object} - The status of OIBus
*/
async prepareStatus(verbose) {
let status = await this.engine.getOIBusInfo()
prepareStatus(verbose) {
let status = this.engine.getOIBusInfo()
if (verbose) {
status = { ...status, ...this.engine.statusData }
}
return status
}

/**
* Forward an healthSignal request.
* @param {object} data - The content to forward
* Log and forward an healthSignal request.
* @param {Object} data - The content to forward
* @return {Promise<void>} - The response
*/
async forwardRequest(data) {
const stringData = JSON.stringify(data)
if (this.http.enabled) {
this.logger.debug('Forwarding healthSignal request')
const stringData = JSON.stringify(data)
this.logger.trace(`Forwarding health signal to "${this.http.host}".`)
const headers = { 'Content-Type': 'application/json' }
await this.engine.requestService.httpSend(this.http.host, 'POST', this.http.authentication, this.http.proxy, stringData, headers)
this.logger.debug('Forwarding healthSignal was successful')
this.logger.info(stringData)
await this.engine.requestService.httpSend(
`${this.http.host}${this.http.endpoint}`,
'POST',
this.http.authentication,
this.http.proxy,
stringData,
headers,
)
this.logger.trace(`Health signal successfully forwarded to "${this.http.host}".`)
} else {
this.logger.warn('HTTP health signal is disabled. Cannot forward payload.')
this.logger.info(stringData)
}
}
}
Expand Down
Loading

0 comments on commit c92bdfd

Please sign in to comment.