navi-hey 0.5.2 → 1.1.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 +4 -0
- package/bin/navi.js +3 -3
- package/lib/{factories → background}/JobFactory.js +2 -2
- package/lib/{registry → background}/JobRegistry.js +18 -1
- package/lib/{registry → background}/JobRegistryInstance.js +51 -6
- package/lib/{models → background}/Worker.js +5 -3
- package/lib/{factories → background}/WorkerFactory.js +2 -2
- package/lib/{registry → background}/WorkersRegistryInstance.js +1 -1
- package/lib/{models → enqueuers}/ActionEnqueuer.js +4 -1
- package/lib/{models → enqueuers}/AssetRequestEnqueuer.js +6 -2
- package/lib/exceptions/ConflictError.js +14 -0
- package/lib/{models → jobs}/ActionProcessingJob.js +4 -4
- package/lib/{models → jobs}/AssetDownloadJob.js +6 -6
- package/lib/{models → jobs}/HtmlParseJob.js +6 -6
- package/lib/{models → jobs}/ResourceRequestJob.js +11 -11
- package/lib/models/Config.js +4 -2
- package/lib/models/FailureConfig.js +36 -0
- package/lib/models/ResourceRequest.js +5 -2
- package/lib/models/ResourceRequestAction.js +6 -3
- package/lib/models/WebConfig.js +5 -1
- package/lib/registry/ClientRegistry.js +60 -2
- package/lib/registry/LogRegistry.js +146 -0
- package/lib/registry/LogRegistryInstance.js +156 -0
- package/lib/serializers/BaseUrlsSerializer.js +18 -0
- package/lib/serializers/JobIndexSerializer.js +10 -3
- package/lib/serializers/LogSerializer.js +26 -0
- package/lib/server/BaseUrlsRequestHandler.js +33 -0
- package/lib/server/EngineContinueRequestHandler.js +34 -0
- package/lib/server/EnginePauseRequestHandler.js +34 -0
- package/lib/server/EngineRestartRequestHandler.js +34 -0
- package/lib/server/EngineShutdownRequestHandler.js +29 -0
- package/lib/server/EngineStartRequestHandler.js +34 -0
- package/lib/server/EngineStatusRequestHandler.js +27 -0
- package/lib/server/EngineStopRequestHandler.js +34 -0
- package/lib/server/JobLogsRequestHandler.js +39 -0
- package/lib/server/JobRequestHandler.js +1 -1
- package/lib/server/JobRetryRequestHandler.js +40 -0
- package/lib/server/JobsFilter.js +55 -0
- package/lib/server/JobsRequestHandler.js +5 -2
- package/lib/server/LogsRequestHandler.js +55 -0
- package/lib/server/RouteRegister.js +40 -7
- package/lib/server/Router.js +44 -20
- package/lib/server/SettingsRequestHandler.js +34 -0
- package/lib/server/StatsRequestHandler.js +2 -2
- package/lib/server/WebServer.js +28 -8
- package/lib/services/Application.js +135 -81
- package/lib/services/ApplicationInstance.js +272 -0
- package/lib/services/Client.js +27 -22
- package/lib/services/ConfigLoader.js +2 -1
- package/lib/services/ConfigParser.js +13 -2
- package/lib/services/Engine.js +51 -6
- package/lib/services/EngineEvents.js +42 -0
- package/lib/services/FailureChecker.js +78 -0
- package/lib/services/WorkersAllocator.js +2 -2
- package/lib/utils/EnvResolver.js +22 -0
- package/lib/utils/HtmlElementParser.js +5 -4
- package/lib/utils/HtmlParser.js +4 -4
- package/lib/utils/PromiseAggregator.js +11 -5
- package/lib/utils/logging/BufferedLogger.js +8 -0
- package/lib/utils/logging/LogBuffer.js +25 -5
- package/lib/utils/logging/LogBufferCollection.js +56 -0
- package/lib/utils/logging/LogContext.js +51 -0
- package/lib/utils/logging/LogFilter.js +34 -0
- package/package.json +1 -1
- package/static/assets/index-BfggoMvd.js +11 -0
- package/static/assets/{index-DBkz-poX.css → index-CuWjW0sT.css} +1 -1
- package/static/index.html +2 -2
- package/static/assets/index-BF8_ow13.js +0 -11
- /package/lib/{models → background}/Job.js +0 -0
- /package/lib/{registry → background}/WorkersRegistry.js +0 -0
- /package/lib/{models → enqueuers}/ActionsEnqueuer.js +0 -0
- /package/lib/{factories → factory}/Factory.js +0 -0
|
@@ -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.
|
package/lib/services/Engine.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { WorkersAllocator } from './WorkersAllocator.js';
|
|
2
|
-
import { JobRegistry } from '../
|
|
3
|
-
import { WorkersRegistry } from '../
|
|
2
|
+
import { JobRegistry } from '../background/JobRegistry.js';
|
|
3
|
+
import { WorkersRegistry } from '../background/WorkersRegistry.js';
|
|
4
4
|
import { Logger } from '../utils/logging/Logger.js';
|
|
5
5
|
|
|
6
6
|
/**
|
|
@@ -8,35 +8,70 @@ import { Logger } from '../utils/logging/Logger.js';
|
|
|
8
8
|
*
|
|
9
9
|
* It continuously checks for available jobs and idle workers, and assigns
|
|
10
10
|
* jobs to workers until there are no more jobs and no more busy workers.
|
|
11
|
+
* In keepAlive mode (web UI present), the loop continues indefinitely until
|
|
12
|
+
* `stop()` is explicitly called.
|
|
11
13
|
*/
|
|
12
14
|
class Engine {
|
|
13
15
|
#sleepMs;
|
|
16
|
+
#stopped = false;
|
|
17
|
+
#paused = false;
|
|
18
|
+
#keepAlive;
|
|
14
19
|
|
|
15
20
|
/**
|
|
16
21
|
* Creates an instance of Engine.
|
|
17
22
|
* @param {object} param0 - The parameters for creating an Engine instance.
|
|
18
23
|
* @param {WorkersAllocator} param0.allocator - The workers allocator to manage job allocation.
|
|
19
24
|
* @param {number} [param0.sleepMs=500] - Milliseconds to wait when all jobs are in cooldown. Use a negative value to disable sleeping (e.g. in tests).
|
|
25
|
+
* @param {boolean} [param0.keepAlive=false] - When true, the loop runs indefinitely until `stop()` is called (web UI mode).
|
|
20
26
|
*/
|
|
21
|
-
constructor({ allocator, sleepMs = 500 } = {}) {
|
|
27
|
+
constructor({ allocator, sleepMs = 500, keepAlive = false } = {}) {
|
|
22
28
|
this.#sleepMs = sleepMs;
|
|
29
|
+
this.#keepAlive = keepAlive;
|
|
23
30
|
|
|
24
31
|
this.allocator = allocator || new WorkersAllocator();
|
|
25
32
|
}
|
|
26
33
|
|
|
34
|
+
/**
|
|
35
|
+
* Stops the engine by setting the stop flag.
|
|
36
|
+
* The current iteration will complete before the loop exits.
|
|
37
|
+
* @returns {void}
|
|
38
|
+
*/
|
|
39
|
+
stop() {
|
|
40
|
+
this.#stopped = true;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Pauses allocation without exiting the loop.
|
|
45
|
+
* While paused, the engine keeps iterating but skips job allocation.
|
|
46
|
+
* @returns {void}
|
|
47
|
+
*/
|
|
48
|
+
pause() {
|
|
49
|
+
this.#paused = true;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Resumes allocation after a pause.
|
|
54
|
+
* @returns {void}
|
|
55
|
+
*/
|
|
56
|
+
resume() {
|
|
57
|
+
this.#paused = false;
|
|
58
|
+
}
|
|
59
|
+
|
|
27
60
|
/**
|
|
28
61
|
* Starts the engine by processing jobs.
|
|
29
62
|
*
|
|
30
63
|
* This method continuously checks for available jobs and idle workers, and assigns
|
|
31
|
-
* jobs to workers until there are no more jobs and no more busy workers
|
|
64
|
+
* jobs to workers until there are no more jobs and no more busy workers,
|
|
65
|
+
* or until `stop()` has been called. In keepAlive mode, the loop continues even
|
|
66
|
+
* when the queue is empty, waiting for new work.
|
|
32
67
|
* @returns {Promise<void>}
|
|
33
68
|
*/
|
|
34
69
|
async start() {
|
|
35
|
-
while (this.#
|
|
70
|
+
while (!this.#stopped && this.#shouldContinue()) {
|
|
36
71
|
Logger.debug('Promoting ready jobs and allocating to idle workers if available...');
|
|
37
72
|
JobRegistry.promoteReadyJobs();
|
|
38
73
|
|
|
39
|
-
if (JobRegistry.hasReadyJob()) {
|
|
74
|
+
if (!this.#paused && JobRegistry.hasReadyJob()) {
|
|
40
75
|
this.allocator.allocate();
|
|
41
76
|
}
|
|
42
77
|
|
|
@@ -45,6 +80,16 @@ class Engine {
|
|
|
45
80
|
}
|
|
46
81
|
}
|
|
47
82
|
|
|
83
|
+
/**
|
|
84
|
+
* Determines whether the engine loop should continue.
|
|
85
|
+
* In keepAlive mode, always returns true. Otherwise, returns true only
|
|
86
|
+
* while there are jobs or busy workers.
|
|
87
|
+
* @returns {boolean}
|
|
88
|
+
*/
|
|
89
|
+
#shouldContinue() {
|
|
90
|
+
return this.#keepAlive || this.#continueAllocating();
|
|
91
|
+
}
|
|
92
|
+
|
|
48
93
|
/**
|
|
49
94
|
* Checks if the engine should continue allocating jobs to workers.
|
|
50
95
|
* @returns {boolean} True if there are jobs to process or busy workers, false otherwise.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* EngineEvents is a singleton event bus for engine lifecycle transitions.
|
|
5
|
+
* Components subscribe via `EngineEvents.on(name, handler)` and the engine
|
|
6
|
+
* fires events via `EngineEvents.emit(name)`.
|
|
7
|
+
*
|
|
8
|
+
* Supported event names: 'stop', 'start', 'restart', 'reset'.
|
|
9
|
+
* @author darthjee
|
|
10
|
+
*/
|
|
11
|
+
class EngineEvents {
|
|
12
|
+
static #emitter = new EventEmitter();
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Fires the named event, invoking all registered listeners synchronously.
|
|
16
|
+
* @param {string} eventName - The event to fire.
|
|
17
|
+
* @returns {void}
|
|
18
|
+
*/
|
|
19
|
+
static emit(eventName) {
|
|
20
|
+
EngineEvents.#emitter.emit(eventName);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Registers a listener for the named event.
|
|
25
|
+
* @param {string} eventName - The event to listen for.
|
|
26
|
+
* @param {Function} handler - The callback to invoke when the event fires.
|
|
27
|
+
* @returns {void}
|
|
28
|
+
*/
|
|
29
|
+
static on(eventName, handler) {
|
|
30
|
+
EngineEvents.#emitter.on(eventName, handler);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Removes all registered listeners. Intended for test teardown.
|
|
35
|
+
* @returns {void}
|
|
36
|
+
*/
|
|
37
|
+
static reset() {
|
|
38
|
+
EngineEvents.#emitter.removeAllListeners();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export { EngineEvents };
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { JobRegistry } from '../background/JobRegistry.js';
|
|
2
|
+
import { LogRegistry } from '../registry/LogRegistry.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} True if the ratio is above the configured threshold.
|
|
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
|
+
LogRegistry.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 };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { JobRegistry } from '../
|
|
2
|
-
import { WorkersRegistry } from '../
|
|
1
|
+
import { JobRegistry } from '../background/JobRegistry.js';
|
|
2
|
+
import { WorkersRegistry } from '../background/WorkersRegistry.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* WorkersAllocator is responsible for allocating jobs to idle workers.
|
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
|
*
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { Logger } from './logging/Logger.js';
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
2
|
* HtmlElementParser wraps a single parsed HTML element and extracts attribute values,
|
|
5
3
|
* logging a warning when the requested attribute is absent.
|
|
@@ -8,14 +6,17 @@ import { Logger } from './logging/Logger.js';
|
|
|
8
6
|
class HtmlElementParser {
|
|
9
7
|
#element;
|
|
10
8
|
#selector;
|
|
9
|
+
#logContext;
|
|
11
10
|
|
|
12
11
|
/**
|
|
13
12
|
* @param {object} element A parsed HTML element with a `getAttribute` method.
|
|
14
13
|
* @param {string} selector The CSS selector that matched this element (used in warning messages).
|
|
14
|
+
* @param {LogContext} logContext Context carrying workerId/jobId for log entries.
|
|
15
15
|
*/
|
|
16
|
-
constructor(element, selector) {
|
|
16
|
+
constructor(element, selector, logContext) {
|
|
17
17
|
this.#element = element;
|
|
18
18
|
this.#selector = selector;
|
|
19
|
+
this.#logContext = logContext;
|
|
19
20
|
}
|
|
20
21
|
|
|
21
22
|
/**
|
|
@@ -28,7 +29,7 @@ class HtmlElementParser {
|
|
|
28
29
|
const value = this.#element.getAttribute(attribute);
|
|
29
30
|
|
|
30
31
|
if (value === undefined || value === null) {
|
|
31
|
-
|
|
32
|
+
this.#logContext.warn(`HtmlParser: element matched by "${this.#selector}" is missing attribute "${attribute}"`);
|
|
32
33
|
return null;
|
|
33
34
|
}
|
|
34
35
|
|
package/lib/utils/HtmlParser.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { parse } from 'node-html-parser';
|
|
2
2
|
import { HtmlElementParser } from './HtmlElementParser.js';
|
|
3
3
|
import { InvalidHtmlResponseBody } from '../exceptions/InvalidHtmlResponseBody.js';
|
|
4
|
-
import { Logger } from './logging/Logger.js';
|
|
5
4
|
|
|
6
5
|
/**
|
|
7
6
|
* HtmlParser parses a raw HTML string and extracts attribute values using CSS selectors.
|
|
@@ -17,10 +16,11 @@ class HtmlParser {
|
|
|
17
16
|
* @param {string} rawHtml The raw HTML string to parse.
|
|
18
17
|
* @param {string} selector CSS selector to match elements.
|
|
19
18
|
* @param {string} attribute Attribute name whose value is extracted from each matched element.
|
|
19
|
+
* @param {LogContext} logContext Context carrying workerId/jobId for log entries.
|
|
20
20
|
* @returns {Array<string>} Array of attribute values from matched elements.
|
|
21
21
|
* @throws {InvalidHtmlResponseBody} If the HTML cannot be parsed.
|
|
22
22
|
*/
|
|
23
|
-
static parse(rawHtml, selector, attribute) {
|
|
23
|
+
static parse(rawHtml, selector, attribute, logContext) {
|
|
24
24
|
let root;
|
|
25
25
|
|
|
26
26
|
try {
|
|
@@ -32,13 +32,13 @@ class HtmlParser {
|
|
|
32
32
|
const elements = root.querySelectorAll(selector);
|
|
33
33
|
|
|
34
34
|
if (elements.length === 0) {
|
|
35
|
-
|
|
35
|
+
logContext.warn(`HtmlParser: selector "${selector}" matched zero elements`);
|
|
36
36
|
return [];
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
const values = [];
|
|
40
40
|
for (const element of elements) {
|
|
41
|
-
const value = new HtmlElementParser(element, selector).getAttribute(attribute);
|
|
41
|
+
const value = new HtmlElementParser(element, selector, logContext).getAttribute(attribute);
|
|
42
42
|
if (value !== null) values.push(value);
|
|
43
43
|
}
|
|
44
44
|
|
|
@@ -19,17 +19,23 @@ class PromiseAggregator {
|
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
|
-
* Waits for all registered promises to settle.
|
|
22
|
+
* Waits for all registered promises to settle, including any added during the wait.
|
|
23
|
+
* Loops until the internal list is empty, draining settled promises each iteration.
|
|
23
24
|
* If one or more promises reject, it still waits for the remaining
|
|
24
25
|
* ones and then re-throws the first rejection error.
|
|
25
26
|
* @returns {Promise<void>}
|
|
26
27
|
*/
|
|
27
28
|
async wait() {
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
while (this.#promises.length > 0) {
|
|
30
|
+
const current = [...this.#promises];
|
|
31
|
+
const results = await Promise.allSettled(current);
|
|
30
32
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
+
this.#promises = this.#promises.filter((p) => !current.includes(p));
|
|
34
|
+
|
|
35
|
+
const firstRejection = results.find((r) => r.status === 'rejected');
|
|
36
|
+
if (firstRejection) {
|
|
37
|
+
throw firstRejection.reason;
|
|
38
|
+
}
|
|
33
39
|
}
|
|
34
40
|
}
|
|
35
41
|
}
|
|
@@ -30,6 +30,14 @@ class BufferedLogger extends BaseLogger {
|
|
|
30
30
|
this.#buffer.add(level, message, attributes);
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
+
/**
|
|
34
|
+
* Returns the most recently added log, or undefined if the buffer is empty.
|
|
35
|
+
* @returns {import('./Log.js').Log|undefined}
|
|
36
|
+
*/
|
|
37
|
+
get latestLog() {
|
|
38
|
+
return this.#buffer.latestLog;
|
|
39
|
+
}
|
|
40
|
+
|
|
33
41
|
/**
|
|
34
42
|
* Gets all logs from the buffer.
|
|
35
43
|
* @returns {Array<import('./Log.js').Log>} Array of log entries.
|
|
@@ -31,10 +31,10 @@ class LogBuffer {
|
|
|
31
31
|
add(level, message, attributes = {}) {
|
|
32
32
|
const log = this.#factory.build(level, message, attributes);
|
|
33
33
|
|
|
34
|
-
this.#logs.
|
|
34
|
+
this.#logs.unshift(log);
|
|
35
35
|
|
|
36
36
|
if (this.#logs.length > this.#retention) {
|
|
37
|
-
this.#logs.
|
|
37
|
+
this.#logs.pop();
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
return log;
|
|
@@ -45,7 +45,7 @@ class LogBuffer {
|
|
|
45
45
|
* @returns {Array<Log>} Array of log entries.
|
|
46
46
|
*/
|
|
47
47
|
getLogs() {
|
|
48
|
-
return [...this.#logs];
|
|
48
|
+
return [...this.#logs].reverse();
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
/**
|
|
@@ -63,7 +63,27 @@ class LogBuffer {
|
|
|
63
63
|
* @returns {Array<Log>} Array of log entries matching the level.
|
|
64
64
|
*/
|
|
65
65
|
getLogsByLevel(level) {
|
|
66
|
-
return this.#logs.filter(log => log.level === level);
|
|
66
|
+
return this.#logs.filter(log => log.level === level).reverse();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Appends an existing Log instance to the buffer, respecting the retention limit.
|
|
71
|
+
* @param {import('./Log.js').Log} log - The Log instance to append.
|
|
72
|
+
* @returns {void}
|
|
73
|
+
*/
|
|
74
|
+
push(log) {
|
|
75
|
+
this.#logs.unshift(log);
|
|
76
|
+
if (this.#logs.length > this.#retention) {
|
|
77
|
+
this.#logs.pop();
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Returns the most recently added log, or undefined if the buffer is empty.
|
|
83
|
+
* @returns {import('./Log.js').Log|undefined}
|
|
84
|
+
*/
|
|
85
|
+
get latestLog() {
|
|
86
|
+
return this.#logs[0];
|
|
67
87
|
}
|
|
68
88
|
|
|
69
89
|
/**
|
|
@@ -95,7 +115,7 @@ class LogBuffer {
|
|
|
95
115
|
* @returns {Array<object>} Array of log objects.
|
|
96
116
|
*/
|
|
97
117
|
toJSON() {
|
|
98
|
-
return this.#logs.map(log => log.toJSON());
|
|
118
|
+
return [...this.#logs].reverse().map(log => log.toJSON());
|
|
99
119
|
}
|
|
100
120
|
}
|
|
101
121
|
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { LogBuffer } from './LogBuffer.js';
|
|
2
|
+
import { EngineEvents } from '../../services/EngineEvents.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* LogBufferCollection manages a keyed map of LogBuffer instances.
|
|
6
|
+
* Buffers are created on first use and cleared automatically when the engine stops.
|
|
7
|
+
* @author darthjee
|
|
8
|
+
*/
|
|
9
|
+
class LogBufferCollection {
|
|
10
|
+
#buffers;
|
|
11
|
+
#retention;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param {number} [retention=100] - Maximum number of logs to retain per buffer.
|
|
15
|
+
*/
|
|
16
|
+
constructor(retention = 100) {
|
|
17
|
+
this.#buffers = new Map();
|
|
18
|
+
this.#retention = retention;
|
|
19
|
+
EngineEvents.on('stop', () => this.clear());
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Appends an existing Log instance to the buffer for the given key.
|
|
24
|
+
* Lazily creates the buffer on first use.
|
|
25
|
+
* @param {string|number} key - The key identifying the buffer (e.g. jobId or workerId).
|
|
26
|
+
* @param {import('./Log.js').Log} log - The Log instance to append.
|
|
27
|
+
* @returns {void}
|
|
28
|
+
*/
|
|
29
|
+
push(key, log) {
|
|
30
|
+
if (!this.#buffers.has(key)) {
|
|
31
|
+
this.#buffers.set(key, new LogBuffer(this.#retention));
|
|
32
|
+
}
|
|
33
|
+
this.#buffers.get(key).push(log);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Returns all logs for the given key in chronological order (oldest first).
|
|
38
|
+
* Returns an empty array if no buffer exists for that key.
|
|
39
|
+
* @param {string|number} key - The key to look up.
|
|
40
|
+
* @returns {Array<import('./Log.js').Log>}
|
|
41
|
+
*/
|
|
42
|
+
getLogs(key) {
|
|
43
|
+
if (!this.#buffers.has(key)) return [];
|
|
44
|
+
return this.#buffers.get(key).getLogs();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Removes all per-key buffers.
|
|
49
|
+
* @returns {void}
|
|
50
|
+
*/
|
|
51
|
+
clear() {
|
|
52
|
+
this.#buffers = new Map();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export { LogBufferCollection };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { LogRegistry } from '../../registry/LogRegistry.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* LogContext wraps a fixed set of attributes and forwards log calls to LogRegistry
|
|
5
|
+
* with those attributes merged into every entry.
|
|
6
|
+
* @author darthjee
|
|
7
|
+
*/
|
|
8
|
+
class LogContext {
|
|
9
|
+
#attributes;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @param {object} attributes Fixed attributes to include on every log entry (e.g. workerId, jobId).
|
|
13
|
+
*/
|
|
14
|
+
constructor(attributes) {
|
|
15
|
+
this.#attributes = attributes;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @param {string} message The log message.
|
|
20
|
+
* @param {object} [attrs={}] Additional per-call attributes merged with context attributes.
|
|
21
|
+
*/
|
|
22
|
+
debug(message, attrs = {}) {
|
|
23
|
+
LogRegistry.debug(message, { ...this.#attributes, ...attrs });
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @param {string} message The log message.
|
|
28
|
+
* @param {object} [attrs={}] Additional per-call attributes merged with context attributes.
|
|
29
|
+
*/
|
|
30
|
+
info(message, attrs = {}) {
|
|
31
|
+
LogRegistry.info(message, { ...this.#attributes, ...attrs });
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @param {string} message The log message.
|
|
36
|
+
* @param {object} [attrs={}] Additional per-call attributes merged with context attributes.
|
|
37
|
+
*/
|
|
38
|
+
warn(message, attrs = {}) {
|
|
39
|
+
LogRegistry.warn(message, { ...this.#attributes, ...attrs });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @param {string} message The log message.
|
|
44
|
+
* @param {object} [attrs={}] Additional per-call attributes merged with context attributes.
|
|
45
|
+
*/
|
|
46
|
+
error(message, attrs = {}) {
|
|
47
|
+
LogRegistry.error(message, { ...this.#attributes, ...attrs });
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export { LogContext };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Filters a list of log entries based on query options.
|
|
3
|
+
* @author darthjee
|
|
4
|
+
*/
|
|
5
|
+
class LogFilter {
|
|
6
|
+
#logs;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @param {Array<import('./Log.js').Log>} logs - The full list of logs to filter (oldest first).
|
|
10
|
+
*/
|
|
11
|
+
constructor(logs) {
|
|
12
|
+
this.#logs = logs;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Returns the filtered log list.
|
|
17
|
+
* @param {object} [options={}]
|
|
18
|
+
* @param {number|string} [options.lastId] - When provided, returns only logs newer than this ID.
|
|
19
|
+
* Returns an empty array if the ID is not found.
|
|
20
|
+
* @returns {Array<import('./Log.js').Log>}
|
|
21
|
+
*/
|
|
22
|
+
filter({ lastId } = {}) {
|
|
23
|
+
if (lastId === undefined) return this.#logs;
|
|
24
|
+
|
|
25
|
+
const id = parseInt(lastId, 10);
|
|
26
|
+
const index = this.#logs.findIndex(log => log.id === id);
|
|
27
|
+
|
|
28
|
+
if (index === -1) return [];
|
|
29
|
+
|
|
30
|
+
return this.#logs.slice(index + 1);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export { LogFilter };
|