navi-hey 0.5.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -1
- package/lib/models/ActionProcessingJob.js +1 -1
- package/lib/models/AssetDownloadJob.js +1 -1
- package/lib/models/Config.js +4 -2
- package/lib/models/FailureConfig.js +36 -0
- package/lib/models/HtmlParseJob.js +1 -1
- package/lib/models/ResourceRequestJob.js +1 -1
- package/lib/registry/ClientRegistry.js +60 -2
- package/lib/registry/JobRegistry.js +1 -1
- package/lib/registry/JobRegistryInstance.js +9 -5
- package/lib/serializers/BaseUrlsSerializer.js +18 -0
- package/lib/server/BaseUrlsRequestHandler.js +33 -0
- package/lib/server/Router.js +6 -0
- package/lib/services/Application.js +4 -4
- package/lib/services/Client.js +8 -5
- package/lib/services/ConfigLoader.js +2 -1
- package/lib/services/ConfigParser.js +13 -2
- package/lib/services/FailureChecker.js +78 -0
- package/lib/utils/EnvResolver.js +22 -0
- package/package.json +1 -1
- package/static/index.html +1 -1
- package/static/assets/index-BF8_ow13.js +0 -11
package/README.md
CHANGED
|
@@ -62,6 +62,9 @@ workers:
|
|
|
62
62
|
log:
|
|
63
63
|
size: 100 # max number of log entries kept in memory (default: 100)
|
|
64
64
|
|
|
65
|
+
failure:
|
|
66
|
+
threshold: 10.0 # optional: exit with failure if > 10% of jobs are dead
|
|
67
|
+
|
|
65
68
|
web:
|
|
66
69
|
port: 3000 # port for the monitoring web UI (omit to disable)
|
|
67
70
|
|
|
@@ -113,6 +116,7 @@ resources:
|
|
|
113
116
|
| `workers.sleep` | Milliseconds the engine waits between allocation ticks. Defaults to `500`. |
|
|
114
117
|
| `workers.max-retries` | Maximum number of times a job is retried before being moved to the dead queue. Defaults to `3`. |
|
|
115
118
|
| `log.size` | Maximum number of log entries kept in the in-memory log buffer. Defaults to `100`. |
|
|
119
|
+
| `failure.threshold` | Optional. Percentage (0–100) of dead jobs that triggers a non-zero exit code. When absent, Navi always exits successfully. |
|
|
116
120
|
| `web.port` | Port for the local monitoring web UI. Omit the `web` key entirely to run Navi without the web server. The web UI frontend is bundled with the package and served from `source/static/`. After frontend code changes, run `yarn build` in the `navi_frontend` service to update the bundled assets. |
|
|
117
121
|
|
|
118
122
|
When the web server is enabled, the following screens are available:
|
|
@@ -122,7 +126,7 @@ When the web server is enabled, the following screens are available:
|
|
|
122
126
|
| Dashboard | `/#/` | Real-time job queue stats (counts per status). |
|
|
123
127
|
| Jobs list | `/#/jobs` | Table of all jobs across every status, with links to individual job pages. |
|
|
124
128
|
| Job detail | `/#/job/:id` | Full details for a specific job (ID, status, attempt count). |
|
|
125
|
-
| `clients.<name>.base_url` | Base URL for the named HTTP client. |
|
|
129
|
+
| `clients.<name>.base_url` | Base URL for the named HTTP client. Supports environment variable references (`$VAR` or `${VAR}`), resolved at configuration load time. |
|
|
126
130
|
| `clients.<name>.headers` | Optional HTTP headers sent with every request of this client. Header values support environment variable references (`$VAR` or `${VAR}`), resolved at configuration load time. |
|
|
127
131
|
| `resources.<name>` | A named group of URL requests to warm. |
|
|
128
132
|
| `url` | URL path (appended to the client's `base_url`). Supports `{:placeholder}` tokens. |
|
|
@@ -47,7 +47,7 @@ class ActionProcessingJob extends Job {
|
|
|
47
47
|
* @returns {Promise<void>}
|
|
48
48
|
*/
|
|
49
49
|
async perform() {
|
|
50
|
-
Logger.
|
|
50
|
+
Logger.debug(`ActionProcessingJob #${this.id} performing`);
|
|
51
51
|
try {
|
|
52
52
|
this.lastError = undefined;
|
|
53
53
|
await this.#action.execute(this.#item);
|
|
@@ -43,7 +43,7 @@ class AssetDownloadJob extends Job {
|
|
|
43
43
|
* @returns {Promise<object>} The HTTP response.
|
|
44
44
|
*/
|
|
45
45
|
async perform() {
|
|
46
|
-
Logger.
|
|
46
|
+
Logger.debug(`AssetDownloadJob #${this.id} performing`);
|
|
47
47
|
try {
|
|
48
48
|
this.lastError = undefined;
|
|
49
49
|
const client = this.#clientRegistry.getClient(this.#clientName);
|
package/lib/models/Config.js
CHANGED
|
@@ -16,13 +16,15 @@ class Config {
|
|
|
16
16
|
* @param {WorkersConfig} params.workersConfig - The configuration for worker instances.
|
|
17
17
|
* @param {WebConfig|null} [params.webConfig] - Optional web server configuration.
|
|
18
18
|
* @param {LogConfig} [params.logConfig] - Optional log configuration.
|
|
19
|
+
* @param {FailureConfig|null} [params.failureConfig] - Optional failure threshold configuration.
|
|
19
20
|
*/
|
|
20
|
-
constructor({ resources, clients, workersConfig, webConfig, logConfig }) {
|
|
21
|
+
constructor({ resources, clients, workersConfig, webConfig, logConfig, failureConfig }) {
|
|
21
22
|
this.resourceRegistry = ResourceRegistry.build(resources);
|
|
22
|
-
this.clientRegistry =
|
|
23
|
+
this.clientRegistry = ClientRegistry.build(clients);
|
|
23
24
|
this.workersConfig = workersConfig;
|
|
24
25
|
this.webConfig = webConfig ?? null;
|
|
25
26
|
this.logConfig = logConfig ?? new LogConfig();
|
|
27
|
+
this.failureConfig = failureConfig ?? null;
|
|
26
28
|
}
|
|
27
29
|
|
|
28
30
|
/**
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents the failure threshold configuration for the application.
|
|
3
|
+
* @author darthjee
|
|
4
|
+
*/
|
|
5
|
+
class FailureConfig {
|
|
6
|
+
#threshold;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Creates an instance of FailureConfig.
|
|
10
|
+
* @param {object} config - The configuration object.
|
|
11
|
+
* @param {number} config.threshold - The maximum acceptable percentage of dead jobs (0–100).
|
|
12
|
+
*/
|
|
13
|
+
constructor({ threshold }) {
|
|
14
|
+
this.#threshold = threshold;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Gets the failure threshold percentage.
|
|
19
|
+
* @returns {number} The threshold value.
|
|
20
|
+
*/
|
|
21
|
+
get threshold() {
|
|
22
|
+
return this.#threshold;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Creates a FailureConfig instance from a raw YAML object, or returns null if absent.
|
|
27
|
+
* @param {object|null|undefined} obj - The raw failure config object from YAML.
|
|
28
|
+
* @returns {FailureConfig|null} A new FailureConfig instance, or null if not configured.
|
|
29
|
+
*/
|
|
30
|
+
static fromObject(obj) {
|
|
31
|
+
if (!obj) return null;
|
|
32
|
+
return new FailureConfig(obj);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export { FailureConfig };
|
|
@@ -56,7 +56,7 @@ class HtmlParseJob extends Job {
|
|
|
56
56
|
* @returns {Promise<void>}
|
|
57
57
|
*/
|
|
58
58
|
async perform() {
|
|
59
|
-
Logger.
|
|
59
|
+
Logger.debug(`HtmlParseJob #${this.id} performing`);
|
|
60
60
|
try {
|
|
61
61
|
this.lastError = undefined;
|
|
62
62
|
for (const assetRequest of this.#assetRequests) {
|
|
@@ -41,7 +41,7 @@ class ResourceRequestJob extends Job {
|
|
|
41
41
|
* @returns {Promise} A promise that resolves with the HTTP response.
|
|
42
42
|
*/
|
|
43
43
|
async perform() {
|
|
44
|
-
Logger.
|
|
44
|
+
Logger.debug(`ResourceRequestJob #${this.id} performing`);
|
|
45
45
|
try {
|
|
46
46
|
this.lastError = undefined;
|
|
47
47
|
const response = await this.#getClient().perform(this.#resourceRequest, this.#parameters);
|
|
@@ -2,8 +2,10 @@ import { NamedRegistry } from './NamedRegistry.js';
|
|
|
2
2
|
import { ClientNotFound } from '../exceptions/ClientNotFound.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* ClientRegistry
|
|
6
|
-
*
|
|
5
|
+
* ClientRegistry manages client retrieval logic and exposes a static singleton facade.
|
|
6
|
+
*
|
|
7
|
+
* Call `ClientRegistry.build(clients)` once during application bootstrap.
|
|
8
|
+
* Use `ClientRegistry.reset()` in tests to restore a clean state between examples.
|
|
7
9
|
*
|
|
8
10
|
* @example
|
|
9
11
|
* const clientRegistry = new ClientRegistry({
|
|
@@ -38,6 +40,62 @@ class ClientRegistry extends NamedRegistry {
|
|
|
38
40
|
*/
|
|
39
41
|
static notFoundException = ClientNotFound;
|
|
40
42
|
|
|
43
|
+
static #instance = null;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Creates and stores the singleton instance.
|
|
47
|
+
* @param {object} clients - An object mapping client names to Client instances.
|
|
48
|
+
* @returns {ClientRegistry} The created instance.
|
|
49
|
+
* @throws {Error} If `build()` has already been called without a preceding `reset()`.
|
|
50
|
+
*/
|
|
51
|
+
static build(clients) {
|
|
52
|
+
if (ClientRegistry.#instance) {
|
|
53
|
+
throw new Error('ClientRegistry.build() has already been called. Call reset() first.');
|
|
54
|
+
}
|
|
55
|
+
ClientRegistry.#instance = new ClientRegistry(clients);
|
|
56
|
+
return ClientRegistry.#instance;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Destroys the singleton instance. Intended for test teardown.
|
|
61
|
+
* @returns {void}
|
|
62
|
+
*/
|
|
63
|
+
static reset() {
|
|
64
|
+
ClientRegistry.#instance = null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Retrieves a client by name from the singleton instance.
|
|
69
|
+
* @param {string} [name] - The name of the client to retrieve.
|
|
70
|
+
* @returns {Client} The requested client instance.
|
|
71
|
+
* @throws {ClientNotFound} If the client with the specified name does not exist.
|
|
72
|
+
* @throws {Error} If `build()` has not been called.
|
|
73
|
+
*/
|
|
74
|
+
static getClient(name) {
|
|
75
|
+
return ClientRegistry.#getInstance().getClient(name);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Returns all registered client instances from the singleton.
|
|
80
|
+
* @returns {Client[]} Array of all registered Client instances.
|
|
81
|
+
* @throws {Error} If `build()` has not been called.
|
|
82
|
+
*/
|
|
83
|
+
static all() {
|
|
84
|
+
return Object.values(ClientRegistry.#getInstance().items);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Returns the singleton instance, throwing if not yet built.
|
|
89
|
+
* @returns {ClientRegistry} The singleton instance.
|
|
90
|
+
* @throws {Error} If `build()` has not been called.
|
|
91
|
+
*/
|
|
92
|
+
static #getInstance() {
|
|
93
|
+
if (!ClientRegistry.#instance) {
|
|
94
|
+
throw new Error('ClientRegistry has not been built. Call ClientRegistry.build() first.');
|
|
95
|
+
}
|
|
96
|
+
return ClientRegistry.#instance;
|
|
97
|
+
}
|
|
98
|
+
|
|
41
99
|
/**
|
|
42
100
|
* Retrieves a client by name or the default client if no name is provided.
|
|
43
101
|
*
|
|
@@ -121,7 +121,7 @@ class JobRegistry {
|
|
|
121
121
|
|
|
122
122
|
/**
|
|
123
123
|
* Returns counts of jobs in each state.
|
|
124
|
-
* @returns {{ enqueued: number, processing: number, failed: number, retryQueue: number, finished: number, dead: number }} Job counts per state.
|
|
124
|
+
* @returns {{ enqueued: number, processing: number, failed: number, retryQueue: number, finished: number, dead: number, total: number }} Job counts per state, plus total (dead + finished).
|
|
125
125
|
*/
|
|
126
126
|
static stats() {
|
|
127
127
|
return JobRegistry.#getInstance().stats();
|
|
@@ -161,16 +161,20 @@ class JobRegistryInstance {
|
|
|
161
161
|
|
|
162
162
|
/**
|
|
163
163
|
* Returns counts of jobs in each state.
|
|
164
|
-
* @returns {{ enqueued: number, processing: number, failed: number, retryQueue: number, finished: number, dead: number }} Counts of jobs in each state.
|
|
164
|
+
* @returns {{ enqueued: number, processing: number, failed: number, retryQueue: number, finished: number, dead: number, total: number }} Counts of jobs in each state, plus total (dead + finished).
|
|
165
165
|
*/
|
|
166
166
|
stats() {
|
|
167
|
+
const dead = this.#dead.size();
|
|
168
|
+
const finished = this.#finished.size();
|
|
169
|
+
|
|
167
170
|
return {
|
|
168
|
-
enqueued:
|
|
171
|
+
enqueued: this.#enqueued.size(),
|
|
169
172
|
processing: this.#processing.size(),
|
|
170
|
-
failed:
|
|
173
|
+
failed: this.#failed.size(),
|
|
171
174
|
retryQueue: this.#retryQueue.size(),
|
|
172
|
-
finished
|
|
173
|
-
dead
|
|
175
|
+
finished,
|
|
176
|
+
dead,
|
|
177
|
+
total: dead + finished,
|
|
174
178
|
};
|
|
175
179
|
}
|
|
176
180
|
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Serializer } from './Serializer.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Serializes a Client instance to its base URL string.
|
|
5
|
+
* @author darthjee
|
|
6
|
+
*/
|
|
7
|
+
class BaseUrlsSerializer extends Serializer {
|
|
8
|
+
/**
|
|
9
|
+
* Extracts the baseUrl from a Client instance.
|
|
10
|
+
* @param {object} client - The Client instance to serialize.
|
|
11
|
+
* @returns {string} The client's base URL.
|
|
12
|
+
*/
|
|
13
|
+
static _serializeObject(client) {
|
|
14
|
+
return client.baseUrl;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { BaseUrlsSerializer };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { RequestHandler } from './RequestHandler.js';
|
|
2
|
+
import { ClientRegistry } from '../registry/ClientRegistry.js';
|
|
3
|
+
import { BaseUrlsSerializer } from '../serializers/BaseUrlsSerializer.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Handles GET /clients/base_urls.json requests.
|
|
7
|
+
* Responds with the list of all unique client base URLs.
|
|
8
|
+
* @author darthjee
|
|
9
|
+
*/
|
|
10
|
+
class BaseUrlsRequestHandler extends RequestHandler {
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new BaseUrlsRequestHandler instance.
|
|
13
|
+
*/
|
|
14
|
+
constructor() {
|
|
15
|
+
super();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Responds with all unique client base URLs.
|
|
20
|
+
* @param {object} _req - The Express request object.
|
|
21
|
+
* @param {object} res - The Express response object.
|
|
22
|
+
* @returns {void}
|
|
23
|
+
*/
|
|
24
|
+
handle(_req, res) {
|
|
25
|
+
const clients = ClientRegistry.all();
|
|
26
|
+
const allUrls = BaseUrlsSerializer.serialize(clients);
|
|
27
|
+
const base_urls = [...new Set(allUrls)];
|
|
28
|
+
|
|
29
|
+
res.json({ base_urls });
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export { BaseUrlsRequestHandler };
|
package/lib/server/Router.js
CHANGED
|
@@ -2,6 +2,7 @@ import path from 'path';
|
|
|
2
2
|
import { fileURLToPath } from 'url';
|
|
3
3
|
import express from 'express';
|
|
4
4
|
import { AssetsRequestHandler } from './AssetsRequestHandler.js';
|
|
5
|
+
import { BaseUrlsRequestHandler } from './BaseUrlsRequestHandler.js';
|
|
5
6
|
import { IndexRequestHandler } from './IndexRequestHandler.js';
|
|
6
7
|
import { JobRequestHandler } from './JobRequestHandler.js';
|
|
7
8
|
import { JobsRequestHandler } from './JobsRequestHandler.js';
|
|
@@ -35,6 +36,11 @@ class Router {
|
|
|
35
36
|
handler: new StatsRequestHandler(),
|
|
36
37
|
});
|
|
37
38
|
|
|
39
|
+
register.register({
|
|
40
|
+
route: '/clients/base_urls.json',
|
|
41
|
+
handler: new BaseUrlsRequestHandler(),
|
|
42
|
+
});
|
|
43
|
+
|
|
38
44
|
register.register({
|
|
39
45
|
route: '/jobs/:status.json',
|
|
40
46
|
handler: new JobsRequestHandler(),
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Engine } from './Engine.js';
|
|
2
|
+
import { FailureChecker } from './FailureChecker.js';
|
|
2
3
|
import { ConfigurationFileNotProvided } from '../exceptions/ConfigurationFileNotProvided.js';
|
|
3
4
|
import { JobFactory } from '../factories/JobFactory.js';
|
|
4
5
|
import { ActionProcessingJob } from '../models/ActionProcessingJob.js';
|
|
@@ -52,6 +53,7 @@ class Application {
|
|
|
52
53
|
|
|
53
54
|
/**
|
|
54
55
|
* Starts the application by building the engine, web server, enqueueing initial jobs, and starting both.
|
|
56
|
+
* After the engine finishes, checks the dead-job ratio against the configured failure threshold.
|
|
55
57
|
* @returns {Promise<void>}
|
|
56
58
|
*/
|
|
57
59
|
async run() {
|
|
@@ -65,6 +67,8 @@ class Application {
|
|
|
65
67
|
aggregator.add(this.engine.start());
|
|
66
68
|
|
|
67
69
|
await aggregator.wait();
|
|
70
|
+
|
|
71
|
+
new FailureChecker({ failureConfig: this.config.failureConfig }).check();
|
|
68
72
|
}
|
|
69
73
|
|
|
70
74
|
/**
|
|
@@ -74,7 +78,6 @@ class Application {
|
|
|
74
78
|
buildEngine() {
|
|
75
79
|
return new Engine({ sleepMs: this.config.workersConfig.sleep });
|
|
76
80
|
}
|
|
77
|
-
|
|
78
81
|
/**
|
|
79
82
|
* Builds and returns a WebServer if web configuration is present, otherwise null.
|
|
80
83
|
* @returns {WebServer|null} The created WebServer instance or null.
|
|
@@ -84,7 +87,6 @@ class Application {
|
|
|
84
87
|
webConfig: this.config.webConfig,
|
|
85
88
|
});
|
|
86
89
|
}
|
|
87
|
-
|
|
88
90
|
/**
|
|
89
91
|
* Gets the buffered logger instance created during config loading.
|
|
90
92
|
* @returns {BufferedLogger} The buffered logger instance.
|
|
@@ -92,7 +94,6 @@ class Application {
|
|
|
92
94
|
get bufferedLogger() {
|
|
93
95
|
return this.#bufferedLogger;
|
|
94
96
|
}
|
|
95
|
-
|
|
96
97
|
/**
|
|
97
98
|
* Enqueues all parameter-free ResourceRequests into the job registry.
|
|
98
99
|
* These are requests whose URLs contain no {:placeholder} tokens and can be
|
|
@@ -104,7 +105,6 @@ class Application {
|
|
|
104
105
|
JobRegistry.enqueue('ResourceRequestJob', { resourceRequest, parameters: {} });
|
|
105
106
|
});
|
|
106
107
|
}
|
|
107
|
-
|
|
108
108
|
/**
|
|
109
109
|
* Initializes the job factory, job registry, and workers registry from the loaded configuration.
|
|
110
110
|
* @returns {void}
|
package/lib/services/Client.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import axios from 'axios';
|
|
2
2
|
import { RequestFailed } from '../exceptions/RequestFailed.js';
|
|
3
|
-
import { EnvResolver } from '../utils/EnvResolver.js';
|
|
4
3
|
import { Logger } from '../utils/logging/Logger.js';
|
|
5
4
|
|
|
6
5
|
/**
|
|
@@ -32,13 +31,13 @@ class Client {
|
|
|
32
31
|
* @param {object} config Client configuration object.
|
|
33
32
|
* @param {string} config.base_url Base URL for the client.
|
|
34
33
|
* @param {number} [config.timeout] Optional request timeout in milliseconds.
|
|
35
|
-
* @param {object} [config.headers] Optional HTTP headers.
|
|
36
|
-
* `$VAR` or `${VAR}` are resolved from `process.env` at parse time.
|
|
34
|
+
* @param {object} [config.headers] Optional HTTP headers.
|
|
37
35
|
* @returns {Client} A new Client instance.
|
|
38
36
|
*/
|
|
39
37
|
static fromObject(name, config) {
|
|
40
|
-
const headers =
|
|
41
|
-
|
|
38
|
+
const headers = config.headers || {};
|
|
39
|
+
const baseUrl = config.base_url;
|
|
40
|
+
return new Client({ name, baseUrl, timeout: config.timeout, headers });
|
|
42
41
|
}
|
|
43
42
|
|
|
44
43
|
/**
|
|
@@ -114,10 +113,14 @@ class Client {
|
|
|
114
113
|
validateStatus: () => true,
|
|
115
114
|
});
|
|
116
115
|
|
|
116
|
+
Logger.info(`[Client:${this.name}] Response ${requestUrl} → ${response.status}`);
|
|
117
|
+
|
|
117
118
|
if (response.status !== expectedStatus) {
|
|
119
|
+
Logger.info(`[Client:${this.name}] ${requestUrl} did not match (got ${response.status}, expected ${expectedStatus})`);
|
|
118
120
|
throw new RequestFailed(response.status, requestUrl);
|
|
119
121
|
}
|
|
120
122
|
|
|
123
|
+
Logger.info(`[Client:${this.name}] ${requestUrl} matched (expected ${expectedStatus})`);
|
|
121
124
|
return response;
|
|
122
125
|
}
|
|
123
126
|
|
|
@@ -2,6 +2,7 @@ import { readFileSync } from 'node:fs';
|
|
|
2
2
|
import YAML from 'yaml';
|
|
3
3
|
import { ConfigParser } from './ConfigParser.js';
|
|
4
4
|
import { ConfigurationFileNotFound } from '../exceptions/ConfigurationFileNotFound.js';
|
|
5
|
+
import { EnvResolver } from '../utils/EnvResolver.js';
|
|
5
6
|
import { Logger } from '../utils/logging/Logger.js';
|
|
6
7
|
|
|
7
8
|
/**
|
|
@@ -54,7 +55,7 @@ class ConfigLoader {
|
|
|
54
55
|
* @throws {ConfigurationFileNotFound} If the configuration file is not found at the specified path.
|
|
55
56
|
*/
|
|
56
57
|
#parseYaml() {
|
|
57
|
-
return YAML.parse(this.#yamlContent());
|
|
58
|
+
return YAML.parse(EnvResolver.resolveString(this.#yamlContent()));
|
|
58
59
|
}
|
|
59
60
|
|
|
60
61
|
/**
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Client } from './Client.js';
|
|
2
2
|
import { MissingClientsConfig } from '../exceptions/MissingClientsConfig.js';
|
|
3
3
|
import { MissingResourceConfig } from '../exceptions/MissingResourceConfig.js';
|
|
4
|
+
import { FailureConfig } from '../models/FailureConfig.js';
|
|
4
5
|
import { LogConfig } from '../models/LogConfig.js';
|
|
5
6
|
import { Resource } from '../models/Resource.js';
|
|
6
7
|
import { WebConfig } from '../models/WebConfig.js';
|
|
@@ -41,8 +42,9 @@ class ConfigParser {
|
|
|
41
42
|
* clients: Record<string, Client>,
|
|
42
43
|
* workersConfig: WorkersConfig,
|
|
43
44
|
* webConfig: WebConfig|null,
|
|
44
|
-
* logConfig: LogConfig
|
|
45
|
-
*
|
|
45
|
+
* logConfig: LogConfig,
|
|
46
|
+
* failureConfig: FailureConfig|null
|
|
47
|
+
* }} Parsed configuration with resources, clients, workersConfig, webConfig, logConfig, and failureConfig.
|
|
46
48
|
*/
|
|
47
49
|
parse() {
|
|
48
50
|
const mappedResources = Object.fromEntries(
|
|
@@ -59,6 +61,7 @@ class ConfigParser {
|
|
|
59
61
|
workersConfig: this.#workersConfig(),
|
|
60
62
|
webConfig: this.#webConfig(),
|
|
61
63
|
logConfig: this.#logConfig(),
|
|
64
|
+
failureConfig: this.#failureConfig(),
|
|
62
65
|
};
|
|
63
66
|
}
|
|
64
67
|
|
|
@@ -87,6 +90,14 @@ class ConfigParser {
|
|
|
87
90
|
return LogConfig.fromObject(this.config.log);
|
|
88
91
|
}
|
|
89
92
|
|
|
93
|
+
/**
|
|
94
|
+
* Creates a FailureConfig from the parsed YAML failure section, or null if absent.
|
|
95
|
+
* @returns {FailureConfig|null} The failure configuration instance or null.
|
|
96
|
+
*/
|
|
97
|
+
#failureConfig() {
|
|
98
|
+
return FailureConfig.fromObject(this.config.failure);
|
|
99
|
+
}
|
|
100
|
+
|
|
90
101
|
/**
|
|
91
102
|
* Maps the resources to entries suitable for Object.fromEntries.
|
|
92
103
|
* @returns {Array<Array>} Entries of resource name and Resource instance.
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { JobRegistry } from '../registry/JobRegistry.js';
|
|
2
|
+
import { Logger } from '../utils/logging/Logger.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* FailureChecker evaluates the dead-job ratio after a run and exits with a non-zero
|
|
6
|
+
* code when the ratio exceeds the configured threshold.
|
|
7
|
+
* @author darthjee
|
|
8
|
+
*/
|
|
9
|
+
class FailureChecker {
|
|
10
|
+
#failureConfig;
|
|
11
|
+
#dead;
|
|
12
|
+
#total;
|
|
13
|
+
#cachedRatio;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Creates a new FailureChecker instance.
|
|
17
|
+
* @param {object} params - Construction parameters.
|
|
18
|
+
* @param {FailureConfig|null} params.failureConfig - The failure threshold configuration, or null to skip checking.
|
|
19
|
+
*/
|
|
20
|
+
constructor({ failureConfig }) {
|
|
21
|
+
this.#failureConfig = failureConfig;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Checks the dead-job ratio against the configured failure threshold and exits with
|
|
26
|
+
* a non-zero code if the threshold is exceeded.
|
|
27
|
+
* @returns {void}
|
|
28
|
+
*/
|
|
29
|
+
check() {
|
|
30
|
+
if (!this.#failureConfig) return;
|
|
31
|
+
|
|
32
|
+
this.#loadStats();
|
|
33
|
+
if (this.#total === 0) return;
|
|
34
|
+
|
|
35
|
+
if (this.#isOverThreshold()) {
|
|
36
|
+
this.#fail();
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Loads dead and total job counts from the registry into instance attributes.
|
|
42
|
+
* @returns {void}
|
|
43
|
+
*/
|
|
44
|
+
#loadStats() {
|
|
45
|
+
const { dead, total } = JobRegistry.stats();
|
|
46
|
+
this.#dead = dead;
|
|
47
|
+
this.#total = total;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Calculates the percentage of dead jobs relative to total jobs.
|
|
52
|
+
* The result is memoized after the first call.
|
|
53
|
+
* @returns {number} The dead-job ratio as a percentage (0–100).
|
|
54
|
+
*/
|
|
55
|
+
#ratio() {
|
|
56
|
+
this.#cachedRatio ??= (this.#dead / this.#total) * 100;
|
|
57
|
+
return this.#cachedRatio;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Returns true when the dead-job ratio exceeds the configured threshold.
|
|
62
|
+
* @returns {boolean}
|
|
63
|
+
*/
|
|
64
|
+
#isOverThreshold() {
|
|
65
|
+
return this.#ratio() > this.#failureConfig.threshold;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Logs a descriptive error message and exits the process with a non-zero code.
|
|
70
|
+
* @returns {void}
|
|
71
|
+
*/
|
|
72
|
+
#fail() {
|
|
73
|
+
Logger.error(`Failure threshold exceeded: ${this.#ratio().toFixed(2)}% of jobs are dead (threshold: ${this.#failureConfig.threshold}%)`);
|
|
74
|
+
process.exit(1);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export { FailureChecker };
|
package/lib/utils/EnvResolver.js
CHANGED
|
@@ -29,6 +29,28 @@ class EnvResolver {
|
|
|
29
29
|
);
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
/**
|
|
33
|
+
* Resolves environment variable references in a raw string (e.g. YAML file content).
|
|
34
|
+
*
|
|
35
|
+
* Replaces all `$VAR` and `${VAR}` occurrences with their environment values.
|
|
36
|
+
*
|
|
37
|
+
* @param {string} raw Raw string content.
|
|
38
|
+
* @returns {string} Resolved string with env var references replaced.
|
|
39
|
+
*/
|
|
40
|
+
static resolveString(raw) {
|
|
41
|
+
return String(raw).replace(ENV_VAR_PATTERN, (_match, braced, bare) => {
|
|
42
|
+
const varName = braced || bare;
|
|
43
|
+
const resolved = process.env[varName];
|
|
44
|
+
|
|
45
|
+
if (resolved === undefined) {
|
|
46
|
+
Logger.warn(`Environment variable not defined: ${varName}`);
|
|
47
|
+
return '';
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return resolved;
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
32
54
|
/**
|
|
33
55
|
* Resolves environment variable references in a single string value.
|
|
34
56
|
*
|
package/package.json
CHANGED
package/static/index.html
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
<meta charset="UTF-8" />
|
|
5
5
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
6
|
<title>Navi</title>
|
|
7
|
-
<script type="module" crossorigin src="/assets/index-
|
|
7
|
+
<script type="module" crossorigin src="/assets/index-B-s_bIBy.js"></script>
|
|
8
8
|
<link rel="stylesheet" crossorigin href="/assets/index-DBkz-poX.css">
|
|
9
9
|
</head>
|
|
10
10
|
<body>
|