-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from digicatapult/feature/in-154
Feature/in 154
- Loading branch information
Showing
18 changed files
with
634 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,36 @@ | ||
const { createNodeApi } = require('./api') | ||
const { setupKeyWatcher } = require('./keyWatcher') | ||
const { ConnectionError } = require('../utils/Errors') | ||
|
||
module.exports = { | ||
setupKeyWatcher: async ({ onUpdate }) => { | ||
const api = await createNodeApi() | ||
await setupKeyWatcher(api)({ onUpdate }) | ||
return api | ||
}, | ||
nodeHealthCheck: async (api, name = 'substrate') => { | ||
try { | ||
if (!(await api._isConnected)) throw new ConnectionError({ name }) | ||
const [chain, runtime] = await Promise.all([api._runtimeChain, api._runtimeVersion]) | ||
|
||
return { | ||
name, | ||
status: 'up', | ||
details: { | ||
chain, | ||
runtime: { | ||
name: runtime.specName, | ||
versions: { | ||
spec: runtime.specVersion.toNumber(), | ||
impl: runtime.implVersion.toNumber(), | ||
authoring: runtime.authoringVersion.toNumber(), | ||
transaction: runtime.transactionVersion.toNumber(), | ||
}, | ||
}, | ||
}, | ||
} | ||
} catch (error) { | ||
return { name, status: 'error', error } | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class TimeoutError extends Error { | ||
constructor(service) { | ||
super() | ||
this.type = this.constructor.name | ||
this.service = service.name | ||
this.message = 'Timeout error, no response from a service' | ||
} | ||
} | ||
|
||
class ConnectionError extends Error { | ||
constructor(service) { | ||
super() | ||
this.service = service.name | ||
this.message = 'Connection is not established, will retry during next polling cycle' | ||
} | ||
} | ||
|
||
module.exports = { | ||
TimeoutError, | ||
ConnectionError, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
const { TimeoutError } = require('./Errors') | ||
const { HEALTHCHECK_POLL_PERIOD_MS, HEALTHCHECK_TIMEOUT_MS } = require('../env') | ||
|
||
class ServiceWatcher { | ||
#pollPeriod | ||
#timeout | ||
|
||
// TODO add a method for updating this.services | ||
constructor(apis) { | ||
this.report = {} | ||
this.#pollPeriod = HEALTHCHECK_POLL_PERIOD_MS | ||
this.#timeout = HEALTHCHECK_TIMEOUT_MS | ||
this.services = this.#init(apis) | ||
} | ||
|
||
delay(ms, service = false) { | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => (service ? reject(new TimeoutError(service)) : resolve()), ms) | ||
}) | ||
} | ||
|
||
update(name, details = 'unknown') { | ||
if (!name || typeof name !== 'string') return null // some handling | ||
if (this.report[name] === details) return null // no need to update | ||
|
||
this.report = { | ||
...this.report, | ||
[name]: details, | ||
} | ||
} | ||
|
||
// organize services and store in this.services | ||
#init(services) { | ||
return Object.keys(services) | ||
.map((service) => { | ||
const { healthCheck, ...api } = services[service] | ||
return healthCheck | ||
? { | ||
name: service, | ||
poll: () => healthCheck(api, service), | ||
} | ||
: null | ||
}) | ||
.filter(Boolean) | ||
} | ||
|
||
// fire and forget, cancel using ServiceWatcher.gen.return() | ||
// or ServiceWatcher.gen.throw(<instance of error>) | ||
start() { | ||
if (this.services.length < 1) return null | ||
this.gen = this.#generator() | ||
|
||
const recursive = async (getAll = Promise.resolve([])) => { | ||
try { | ||
const services = await getAll | ||
services.forEach(({ name, ...rest }) => this.update(name, rest)) | ||
await this.delay(this.#pollPeriod) | ||
} catch (error) { | ||
// if no service assume that this is server error e.g. TypeError, Parse... | ||
const name = error.service || 'server' | ||
this.update(name, { error, status: 'error' }) | ||
} | ||
|
||
const { value } = this.gen.next() | ||
recursive(value) | ||
} | ||
|
||
const { value } = this.gen.next() | ||
recursive(value) | ||
} | ||
|
||
// a generator function that returns poll fn for each service | ||
*#generator() { | ||
while (true) | ||
yield Promise.all( | ||
this.services.map((service) => Promise.race([service.poll(), this.delay(this.#timeout, service)])) | ||
) | ||
} | ||
} | ||
|
||
module.exports = ServiceWatcher |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.