-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#296: split off the services, health and middleware
- Loading branch information
1 parent
e0b8813
commit 037ca36
Showing
48 changed files
with
1,677 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,4 @@ | ||
|
||
# Changelog | ||
|
||
This package doesn't keep a changelog. See the changelog in the [github repository](https://github.com/MaskingTechnology/jitar/blob/main/CHANGELOG.md) |
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,9 @@ | ||
|
||
# Jitar Health | ||
|
||
This package contains the components for health monitoring of the [Jitar](https://jitar.dev) runtime. | ||
|
||
For more information about Jitar: | ||
|
||
* [Visit our website](https://jitar.dev) | ||
* [Read the documentation](https://docs.jitar.dev). |
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,44 @@ | ||
{ | ||
"name": "@jitar/health", | ||
"version": "0.7.4", | ||
"description": "Health library for the Jitar runtime.", | ||
"author": "Masking Technology <[email protected]> (https://jitar.dev)", | ||
"license": "MIT", | ||
"type": "module", | ||
"types": "dist/index.d.ts", | ||
"exports": { | ||
".": "./dist/index.js" | ||
}, | ||
"files": [ | ||
"CHANGELOG.md", | ||
"README.md", | ||
"dist" | ||
], | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"scripts": { | ||
"test": "vitest run", | ||
"test-coverage": "vitest run --coverage", | ||
"lint": "eslint . --ext .ts", | ||
"build": "tsc -p tsconfig.json", | ||
"clean": "rm -rf dist", | ||
"prepublishOnly": "npm run clean && npm run build" | ||
}, | ||
"dependencies": { | ||
"@jitar/errors": "*", | ||
"@jitar/sourcing": "*" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/MaskingTechnology/jitar.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/MaskingTechnology/jitar/issues" | ||
}, | ||
"homepage": "https://jitar.dev", | ||
"keywords": [ | ||
"configuration", | ||
"jitar" | ||
] | ||
} |
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,98 @@ | ||
|
||
import { SourcingManager } from '@jitar/sourcing'; | ||
|
||
import InvalidHealthCheck from './errors/InvalidHealthCheck'; | ||
import HealthCheck from './interfaces/HealthCheck'; | ||
|
||
export default class HealthManager | ||
{ | ||
#sourcingManager: SourcingManager; | ||
#healthChecks: Map<string, HealthCheck> = new Map(); | ||
|
||
constructor(sourcingManager: SourcingManager) | ||
{ | ||
this.#sourcingManager = sourcingManager; | ||
} | ||
|
||
async importHealthCheck(filename: string): Promise<void> | ||
{ | ||
const module = await this.#sourcingManager.import(filename); | ||
const healthCheck = module.default as HealthCheck; | ||
|
||
if (healthCheck?.isHealthy === undefined) | ||
{ | ||
throw new InvalidHealthCheck(filename); | ||
} | ||
|
||
this.addHealthCheck(healthCheck as HealthCheck); | ||
} | ||
|
||
addHealthCheck(healthCheck: HealthCheck): void | ||
{ | ||
this.#healthChecks.set(healthCheck.name, healthCheck); | ||
} | ||
|
||
clearHealthChecks(): void | ||
{ | ||
this.#healthChecks.clear(); | ||
} | ||
|
||
async isHealthy(): Promise<boolean> | ||
{ | ||
const promises: Promise<boolean>[] = []; | ||
|
||
for (const healthCheck of this.#healthChecks.values()) | ||
{ | ||
const promise = this.#executeHealthCheck(healthCheck); | ||
|
||
promises.push(promise); | ||
} | ||
|
||
return Promise.all(promises) | ||
.then(results => results.every(result => result)) | ||
.catch(() => false); | ||
} | ||
|
||
async getHealth(): Promise<Map<string, boolean>> | ||
{ | ||
const promises: Promise<{ name: string, isHealthy: boolean }>[] = []; | ||
|
||
for (const [name, healthCheck] of this.#healthChecks) | ||
{ | ||
const promise = this.#executeHealthCheck(healthCheck) | ||
.then(result => ({ name, isHealthy: result })) | ||
.catch(() => ({ name, isHealthy: false })); | ||
|
||
promises.push(promise); | ||
} | ||
|
||
const healthChecks = new Map<string, boolean>(); | ||
|
||
return Promise.allSettled(promises) | ||
.then(results => results.forEach(result => | ||
{ | ||
result.status === 'fulfilled' | ||
? healthChecks.set(result.value.name, result.value.isHealthy) | ||
: healthChecks.set(result.reason.name, false); | ||
})) | ||
.then(() => healthChecks); | ||
} | ||
|
||
async #executeHealthCheck(healthCheck: HealthCheck): Promise<boolean> | ||
{ | ||
const health = healthCheck.isHealthy(); | ||
const milliseconds = healthCheck.timeout; | ||
|
||
if (milliseconds === undefined) | ||
{ | ||
return health; | ||
} | ||
|
||
const timeout = new Promise((resolve) => | ||
{ | ||
setTimeout(resolve, milliseconds); | ||
}).then(() => false); | ||
|
||
return Promise.race([timeout, health]); | ||
} | ||
} |
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,19 @@ | ||
|
||
import { ServerError } from '@jitar/errors'; | ||
import { Loadable } from '@jitar/serialization'; | ||
|
||
export default class InvalidHealthCheck extends ServerError | ||
{ | ||
#url: string; | ||
|
||
constructor(url: string) | ||
{ | ||
super(`Module '${url}' does not export a valid health check`); | ||
|
||
this.#url = url; | ||
} | ||
|
||
get url() { return this.#url; } | ||
} | ||
|
||
(InvalidHealthCheck as Loadable).source = 'RUNTIME_ERROR_LOCATION'; |
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,4 @@ | ||
|
||
export { default as HealthCheck } from './interfaces/HealthCheck'; | ||
|
||
export { default as HealthManager } from './HealthManager'; |
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,11 @@ | ||
|
||
interface HealthCheck | ||
{ | ||
get name(): string; | ||
|
||
get timeout(): number | undefined; | ||
|
||
isHealthy(): Promise<boolean>; | ||
} | ||
|
||
export default HealthCheck; |
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 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ESNext", | ||
"module": "ESNext", | ||
"rootDir": "./src/", | ||
"moduleResolution": "node", | ||
"declaration": true, | ||
"outDir": "./dist", | ||
"removeComments": true, | ||
"esModuleInterop": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"strict": true, | ||
"skipLibCheck": true | ||
}, | ||
"exclude": [ | ||
"vite.config.ts", | ||
"node_modules", | ||
"dist", | ||
"test" | ||
] | ||
} |
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,10 @@ | ||
// vite.config.ts | ||
import { defineConfig } from 'vitest/config'; | ||
|
||
export default defineConfig({ | ||
test: { | ||
coverage: { | ||
provider: 'v8' | ||
}, | ||
}, | ||
}); |
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,4 @@ | ||
|
||
# Changelog | ||
|
||
This package doesn't keep a changelog. See the changelog in the [github repository](https://github.com/MaskingTechnology/jitar/blob/main/CHANGELOG.md) |
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,9 @@ | ||
|
||
# Jitar Middleware | ||
|
||
This package contains the components for middleware support for the [Jitar](https://jitar.dev) runtime. | ||
|
||
For more information about Jitar: | ||
|
||
* [Visit our website](https://jitar.dev) | ||
* [Read the documentation](https://docs.jitar.dev). |
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,45 @@ | ||
{ | ||
"name": "@jitar/middleware", | ||
"version": "0.7.4", | ||
"description": "Middleware library for the Jitar runtime.", | ||
"author": "Masking Technology <[email protected]> (https://jitar.dev)", | ||
"license": "MIT", | ||
"type": "module", | ||
"types": "dist/index.d.ts", | ||
"exports": { | ||
".": "./dist/index.js" | ||
}, | ||
"files": [ | ||
"CHANGELOG.md", | ||
"README.md", | ||
"dist" | ||
], | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"scripts": { | ||
"test": "vitest run", | ||
"test-coverage": "vitest run --coverage", | ||
"lint": "eslint . --ext .ts", | ||
"build": "tsc -p tsconfig.json", | ||
"clean": "rm -rf dist", | ||
"prepublishOnly": "npm run clean && npm run build" | ||
}, | ||
"dependencies": { | ||
"@jitar/errors": "*", | ||
"@jitar/execution": "*", | ||
"@jitar/sourcing": "*" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/MaskingTechnology/jitar.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/MaskingTechnology/jitar/issues" | ||
}, | ||
"homepage": "https://jitar.dev", | ||
"keywords": [ | ||
"configuration", | ||
"jitar" | ||
] | ||
} |
Oops, something went wrong.