navi-hey 1.0.0 → 1.2.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 +38 -4
- package/bin/navi.js +3 -3
- package/lib/{factories → background}/JobFactory.js +2 -2
- package/lib/{registry → background}/JobRegistry.js +17 -0
- package/lib/{registry → background}/JobRegistryInstance.js +42 -1
- 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/enqueuers/ActionEnqueuer.js +53 -0
- package/lib/{models → enqueuers}/ActionsEnqueuer.js +6 -3
- package/lib/{models → enqueuers}/AssetRequestEnqueuer.js +6 -2
- package/lib/enqueuers/PaginatedActionEnqueuer.js +53 -0
- package/lib/enqueuers/PaginatedActionsEnqueuer.js +47 -0
- package/lib/exceptions/{ConfigurationFileNotFound.js → config/ConfigurationFileNotFound.js} +1 -1
- package/lib/exceptions/{ConfigurationFileNotProvided.js → config/ConfigurationFileNotProvided.js} +1 -1
- package/lib/exceptions/{MissingTopLevelConfgKey.js → config/MissingTopLevelConfgKey.js} +1 -1
- package/lib/exceptions/http/ConflictError.js +14 -0
- package/lib/exceptions/{ForbiddenError.js → http/ForbiddenError.js} +1 -1
- package/lib/exceptions/{NotFoundError.js → http/NotFoundError.js} +1 -1
- package/lib/exceptions/{ItemNotFound.js → registry/ItemNotFound.js} +1 -1
- package/lib/exceptions/{MissingActionResource.js → registry/MissingActionResource.js} +1 -1
- package/lib/exceptions/{MissingMappingVariable.js → registry/MissingMappingVariable.js} +1 -1
- package/lib/exceptions/{InvalidHtmlResponseBody.js → request/InvalidHtmlResponseBody.js} +1 -1
- package/lib/exceptions/{InvalidResponseBody.js → request/InvalidResponseBody.js} +1 -1
- package/lib/exceptions/{NullResponse.js → request/NullResponse.js} +1 -1
- package/lib/exceptions/{RequestFailed.js → request/RequestFailed.js} +1 -1
- package/lib/{models → jobs}/ActionProcessingJob.js +21 -7
- package/lib/{models → jobs}/AssetDownloadJob.js +6 -6
- package/lib/{models → jobs}/HtmlParseJob.js +23 -10
- package/lib/jobs/PaginatedActionProcessingJob.js +77 -0
- package/lib/{models → jobs}/ResourceRequestJob.js +18 -15
- package/lib/models/{Config.js → configs/Config.js} +3 -3
- package/lib/models/configs/PaginationConfig.js +63 -0
- package/lib/models/configs/WebConfig.js +19 -0
- package/lib/models/{ResourceRequest.js → request/ResourceRequest.js} +32 -7
- package/lib/models/{ResourceRequestAction.js → request/ResourceRequestAction.js} +9 -6
- package/lib/models/request/ResourceRequestPaginatedAction.js +81 -0
- package/lib/models/{PathSegmentTraverser.js → response/PathSegmentTraverser.js} +1 -1
- package/lib/models/{ResponseParser.js → response/ResponseParser.js} +1 -1
- package/lib/registry/ClientRegistry.js +1 -1
- package/lib/registry/LogRegistry.js +146 -0
- package/lib/registry/LogRegistryInstance.js +156 -0
- package/lib/registry/NamedRegistry.js +1 -1
- package/lib/registry/ResourceRegistry.js +1 -1
- package/lib/serializers/JobIndexSerializer.js +13 -3
- package/lib/serializers/LogSerializer.js +26 -0
- package/lib/server/PathValidator.js +1 -1
- package/lib/server/RouteRegister.js +42 -9
- package/lib/server/Router.js +49 -31
- package/lib/server/WebServer.js +28 -8
- package/lib/server/{AssetsRequestHandler.js → handlers/AssetsRequestHandler.js} +3 -3
- package/lib/server/{BaseUrlsRequestHandler.js → handlers/BaseUrlsRequestHandler.js} +3 -3
- package/lib/server/{IndexRequestHandler.js → handlers/IndexRequestHandler.js} +2 -2
- package/lib/server/handlers/JobsFilter.js +55 -0
- package/lib/server/handlers/LogsRequestHandler.js +55 -0
- package/lib/server/handlers/SettingsRequestHandler.js +35 -0
- package/lib/server/{StatsRequestHandler.js → handlers/StatsRequestHandler.js} +3 -3
- package/lib/server/handlers/engine/EngineContinueRequestHandler.js +34 -0
- package/lib/server/handlers/engine/EnginePauseRequestHandler.js +34 -0
- package/lib/server/handlers/engine/EngineRestartRequestHandler.js +34 -0
- package/lib/server/handlers/engine/EngineShutdownRequestHandler.js +29 -0
- package/lib/server/handlers/engine/EngineStartRequestHandler.js +34 -0
- package/lib/server/handlers/engine/EngineStatusRequestHandler.js +27 -0
- package/lib/server/handlers/engine/EngineStopRequestHandler.js +34 -0
- package/lib/server/handlers/jobs/JobLogsRequestHandler.js +39 -0
- package/lib/server/{JobRequestHandler.js → handlers/jobs/JobRequestHandler.js} +4 -4
- package/lib/server/handlers/jobs/JobRetryRequestHandler.js +40 -0
- package/lib/server/{JobsRequestHandler.js → handlers/jobs/JobsRequestHandler.js} +7 -4
- package/lib/services/Application.js +138 -84
- package/lib/services/ApplicationInstance.js +274 -0
- package/lib/services/Client.js +23 -19
- package/lib/services/ConfigLoader.js +1 -1
- package/lib/services/ConfigParser.js +7 -7
- package/lib/services/Engine.js +51 -6
- package/lib/services/EngineEvents.js +42 -0
- package/lib/services/FailureChecker.js +4 -4
- package/lib/services/WorkersAllocator.js +2 -2
- package/lib/utils/HtmlElementParser.js +5 -4
- package/lib/utils/HtmlParser.js +5 -5
- 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-DBkz-poX.css → index-CuWjW0sT.css} +1 -1
- package/static/assets/index-D5tOOLpl.js +11 -0
- package/static/index.html +2 -2
- package/lib/exceptions/LockedByOtherWorker.js +0 -14
- package/lib/models/ActionEnqueuer.js +0 -34
- package/lib/models/WebConfig.js +0 -15
- /package/lib/{models → background}/Job.js +0 -0
- /package/lib/{registry → background}/WorkersRegistry.js +0 -0
- /package/lib/exceptions/{MissingClientsConfig.js → config/MissingClientsConfig.js} +0 -0
- /package/lib/exceptions/{MissingResourceConfig.js → config/MissingResourceConfig.js} +0 -0
- /package/lib/exceptions/{ClientNotFound.js → registry/ClientNotFound.js} +0 -0
- /package/lib/exceptions/{ResourceNotFound.js → registry/ResourceNotFound.js} +0 -0
- /package/lib/{factories → factory}/Factory.js +0 -0
- /package/lib/models/{FailureConfig.js → configs/FailureConfig.js} +0 -0
- /package/lib/models/{LogConfig.js → configs/LogConfig.js} +0 -0
- /package/lib/models/{WorkersConfig.js → configs/WorkersConfig.js} +0 -0
- /package/lib/models/{AssetRequest.js → request/AssetRequest.js} +0 -0
- /package/lib/models/{Resource.js → request/Resource.js} +0 -0
- /package/lib/models/{ParametersMapper.js → response/ParametersMapper.js} +0 -0
- /package/lib/models/{PathResolver.js → response/PathResolver.js} +0 -0
- /package/lib/models/{ResponseWrapper.js → response/ResponseWrapper.js} +0 -0
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { LogRegistryInstance } from './LogRegistryInstance.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* LogRegistry is a static singleton facade for the application's buffered log publisher.
|
|
5
|
+
*
|
|
6
|
+
* Call `LogRegistry.build(options)` once during application bootstrap.
|
|
7
|
+
* Use `LogRegistry.reset()` in tests to restore a clean state between examples.
|
|
8
|
+
* @author darthjee
|
|
9
|
+
*/
|
|
10
|
+
class LogRegistry {
|
|
11
|
+
static #instance = null;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Creates and stores the singleton instance.
|
|
15
|
+
* @param {object} [options={}] - Forwarded to LogRegistryInstance constructor.
|
|
16
|
+
* @returns {LogRegistryInstance} The created instance.
|
|
17
|
+
* @throws {Error} If build() has already been called without a preceding reset().
|
|
18
|
+
*/
|
|
19
|
+
static build(options = {}) {
|
|
20
|
+
if (LogRegistry.#instance) {
|
|
21
|
+
throw new Error('LogRegistry.build() has already been called. Call reset() first.');
|
|
22
|
+
}
|
|
23
|
+
LogRegistry.#instance = new LogRegistryInstance(options);
|
|
24
|
+
return LogRegistry.#instance;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Destroys the singleton instance. Intended for test teardown.
|
|
29
|
+
* @returns {void}
|
|
30
|
+
*/
|
|
31
|
+
static reset() {
|
|
32
|
+
LogRegistry.#instance = null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Logs a debug message to both the console and the API buffer.
|
|
37
|
+
* @param {string} message - The message to log.
|
|
38
|
+
* @param {object} [attributes={}] - Optional structured metadata.
|
|
39
|
+
* @returns {void}
|
|
40
|
+
*/
|
|
41
|
+
static debug(message, attributes = {}) {
|
|
42
|
+
LogRegistry.#getInstance().debug(message, attributes);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Logs an error message to both the console and the API buffer.
|
|
47
|
+
* @param {string} message - The message to log.
|
|
48
|
+
* @param {object} [attributes={}] - Optional structured metadata.
|
|
49
|
+
* @returns {void}
|
|
50
|
+
*/
|
|
51
|
+
static error(message, attributes = {}) {
|
|
52
|
+
LogRegistry.#getInstance().error(message, attributes);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Gets a specific log by ID.
|
|
57
|
+
* @param {number} id - The log entry ID to look up.
|
|
58
|
+
* @returns {import('../utils/logging/Log.js').Log|undefined} The matching log entry, or undefined if not found.
|
|
59
|
+
*/
|
|
60
|
+
static getLogById(id) {
|
|
61
|
+
return LogRegistry.#getInstance().getLogById(id);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Gets logs in chronological order (oldest first), optionally filtered to entries newer than lastId.
|
|
66
|
+
* @param {object} [options={}] - Query options.
|
|
67
|
+
* @param {number|string} [options.lastId] - When provided, returns only logs newer than this ID.
|
|
68
|
+
* Returns an empty array if the ID is not found.
|
|
69
|
+
* @returns {Array<import('../utils/logging/Log.js').Log>} Array of log entries.
|
|
70
|
+
*/
|
|
71
|
+
static getLogs({ lastId } = {}) {
|
|
72
|
+
return LogRegistry.#getInstance().getLogs({ lastId });
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Gets logs stored in the per-job buffer for the given job ID,
|
|
77
|
+
* optionally filtered to entries newer than lastId.
|
|
78
|
+
* @param {string|number} jobId - The job ID to look up logs for.
|
|
79
|
+
* @param {object} [options={}] - Query options.
|
|
80
|
+
* @param {number|string} [options.lastId] - When provided, returns only logs newer than this ID.
|
|
81
|
+
* @returns {Array<import('../utils/logging/Log.js').Log>} Array of log entries.
|
|
82
|
+
*/
|
|
83
|
+
static getLogsByJobId(jobId, { lastId } = {}) {
|
|
84
|
+
return LogRegistry.#getInstance().getLogsByJobId(jobId, { lastId });
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Gets logs stored in the per-worker buffer for the given worker ID.
|
|
89
|
+
* @param {string|number} workerId - The worker ID to look up logs for.
|
|
90
|
+
* @returns {Array<import('../utils/logging/Log.js').Log>} Array of log entries.
|
|
91
|
+
*/
|
|
92
|
+
static getLogsByWorkerId(workerId) {
|
|
93
|
+
return LogRegistry.#getInstance().getLogsByWorkerId(workerId);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Gets logs filtered by level.
|
|
98
|
+
* @param {string} level - The log level to filter by.
|
|
99
|
+
* @returns {Array<import('../utils/logging/Log.js').Log>} Array of log entries matching the level.
|
|
100
|
+
*/
|
|
101
|
+
static getLogsByLevel(level) {
|
|
102
|
+
return LogRegistry.#getInstance().getLogsByLevel(level);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Returns all logs as plain JSON objects.
|
|
107
|
+
* @returns {Array<object>} Array of plain log objects.
|
|
108
|
+
*/
|
|
109
|
+
static getLogsJSON() {
|
|
110
|
+
return LogRegistry.#getInstance().getLogsJSON();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Logs an info message to both the console and the API buffer.
|
|
115
|
+
* @param {string} message - The message to log.
|
|
116
|
+
* @param {object} [attributes={}] - Optional structured metadata.
|
|
117
|
+
* @returns {void}
|
|
118
|
+
*/
|
|
119
|
+
static info(message, attributes = {}) {
|
|
120
|
+
LogRegistry.#getInstance().info(message, attributes);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Logs a warn message to both the console and the API buffer.
|
|
125
|
+
* @param {string} message - The message to log.
|
|
126
|
+
* @param {object} [attributes={}] - Optional structured metadata.
|
|
127
|
+
* @returns {void}
|
|
128
|
+
*/
|
|
129
|
+
static warn(message, attributes = {}) {
|
|
130
|
+
LogRegistry.#getInstance().warn(message, attributes);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Returns the singleton instance, throwing if not yet built.
|
|
135
|
+
* @returns {LogRegistryInstance} The singleton instance.
|
|
136
|
+
* @throws {Error} If build() has not been called.
|
|
137
|
+
*/
|
|
138
|
+
static #getInstance() {
|
|
139
|
+
if (!LogRegistry.#instance) {
|
|
140
|
+
throw new Error('LogRegistry has not been built. Call LogRegistry.build() first.');
|
|
141
|
+
}
|
|
142
|
+
return LogRegistry.#instance;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export { LogRegistry };
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { BufferedLogger } from '../utils/logging/BufferedLogger.js';
|
|
2
|
+
import { LogBufferCollection } from '../utils/logging/LogBufferCollection.js';
|
|
3
|
+
import { LogFilter } from '../utils/logging/LogFilter.js';
|
|
4
|
+
import { Logger } from '../utils/logging/Logger.js';
|
|
5
|
+
import { LoggerGroup } from '../utils/logging/LoggerGroup.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Holds a LoggerGroup (Logger + BufferedLogger) for the LogRegistry singleton.
|
|
9
|
+
* Exposes debug/info/warn/error methods that fan out to both outputs, and filtered
|
|
10
|
+
* log queries via LogFilter. Not exported directly; accessed only via LogRegistry.
|
|
11
|
+
* @author darthjee
|
|
12
|
+
*/
|
|
13
|
+
class LogRegistryInstance {
|
|
14
|
+
#bufferedLogger;
|
|
15
|
+
#loggerGroup;
|
|
16
|
+
#jobLogs;
|
|
17
|
+
#workerLogs;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Creates a new LogRegistryInstance.
|
|
21
|
+
* @param {object} [options={}] - Options for the loggers.
|
|
22
|
+
* @param {string} [options.level] - Log level threshold.
|
|
23
|
+
* @param {number} [options.retention=100] - Maximum number of logs to retain.
|
|
24
|
+
*/
|
|
25
|
+
constructor({ level, retention } = {}) {
|
|
26
|
+
this.#bufferedLogger = new BufferedLogger(level, retention);
|
|
27
|
+
this.#loggerGroup = new LoggerGroup([Logger.default(), this.#bufferedLogger]);
|
|
28
|
+
this.#jobLogs = new LogBufferCollection(retention);
|
|
29
|
+
this.#workerLogs = new LogBufferCollection(retention);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Returns the underlying BufferedLogger instance.
|
|
34
|
+
* @returns {BufferedLogger} The underlying BufferedLogger instance.
|
|
35
|
+
*/
|
|
36
|
+
get bufferedLogger() {
|
|
37
|
+
return this.#bufferedLogger;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Logs a debug message to both the console and the buffer.
|
|
42
|
+
* @param {string} message - The message to log.
|
|
43
|
+
* @param {object} [attributes={}] - Optional structured metadata.
|
|
44
|
+
* @returns {void}
|
|
45
|
+
*/
|
|
46
|
+
debug(message, attributes = {}) {
|
|
47
|
+
this.#dispatch('debug', message, attributes);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Logs an error message to both the console and the buffer.
|
|
52
|
+
* @param {string} message - The message to log.
|
|
53
|
+
* @param {object} [attributes={}] - Optional structured metadata.
|
|
54
|
+
* @returns {void}
|
|
55
|
+
*/
|
|
56
|
+
error(message, attributes = {}) {
|
|
57
|
+
this.#dispatch('error', message, attributes);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Gets a specific log by ID.
|
|
62
|
+
* @param {number} id - The log entry ID to look up.
|
|
63
|
+
* @returns {import('../utils/logging/Log.js').Log|undefined} The matching log entry, or undefined if not found.
|
|
64
|
+
*/
|
|
65
|
+
getLogById(id) {
|
|
66
|
+
return this.#bufferedLogger.getLogById(id);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Gets logs in chronological order (oldest first), optionally filtered to entries newer than lastId.
|
|
71
|
+
* @param {object} [options={}] - Query options.
|
|
72
|
+
* @param {number|string} [options.lastId] - When provided, returns only logs newer than this ID.
|
|
73
|
+
* Returns an empty array if the ID is not found.
|
|
74
|
+
* @returns {Array<import('../utils/logging/Log.js').Log>} Array of log entries.
|
|
75
|
+
*/
|
|
76
|
+
getLogs({ lastId } = {}) {
|
|
77
|
+
return new LogFilter(this.bufferedLogger.getLogs()).filter({ lastId });
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Gets logs stored in the per-job buffer for the given job ID,
|
|
82
|
+
* optionally filtered to entries newer than lastId.
|
|
83
|
+
* @param {string|number} jobId - The job ID to look up logs for.
|
|
84
|
+
* @param {object} [options={}] - Query options.
|
|
85
|
+
* @param {number|string} [options.lastId] - When provided, returns only logs newer than this ID.
|
|
86
|
+
* @returns {Array<import('../utils/logging/Log.js').Log>} Array of log entries.
|
|
87
|
+
*/
|
|
88
|
+
getLogsByJobId(jobId, { lastId } = {}) {
|
|
89
|
+
return new LogFilter(this.#jobLogs.getLogs(jobId)).filter({ lastId });
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Gets logs stored in the per-worker buffer for the given worker ID.
|
|
94
|
+
* @param {string|number} workerId - The worker ID to look up logs for.
|
|
95
|
+
* @returns {Array<import('../utils/logging/Log.js').Log>} Array of log entries.
|
|
96
|
+
*/
|
|
97
|
+
getLogsByWorkerId(workerId) {
|
|
98
|
+
return this.#workerLogs.getLogs(workerId);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Gets logs filtered by level.
|
|
103
|
+
* @param {string} level - The log level to filter by.
|
|
104
|
+
* @returns {Array<import('../utils/logging/Log.js').Log>} Array of log entries matching the level.
|
|
105
|
+
*/
|
|
106
|
+
getLogsByLevel(level) {
|
|
107
|
+
return this.#bufferedLogger.getLogsByLevel(level);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Returns all logs as plain JSON objects.
|
|
112
|
+
* @returns {Array<object>} Array of plain log objects.
|
|
113
|
+
*/
|
|
114
|
+
getLogsJSON() {
|
|
115
|
+
return this.#bufferedLogger.getLogsJSON();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Logs an info message to both the console and the buffer.
|
|
120
|
+
* @param {string} message - The message to log.
|
|
121
|
+
* @param {object} [attributes={}] - Optional structured metadata.
|
|
122
|
+
* @returns {void}
|
|
123
|
+
*/
|
|
124
|
+
info(message, attributes = {}) {
|
|
125
|
+
this.#dispatch('info', message, attributes);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Logs a warn message to both the console and the buffer.
|
|
130
|
+
* @param {string} message - The message to log.
|
|
131
|
+
* @param {object} [attributes={}] - Optional structured metadata.
|
|
132
|
+
* @returns {void}
|
|
133
|
+
*/
|
|
134
|
+
warn(message, attributes = {}) {
|
|
135
|
+
this.#dispatch('warn', message, attributes);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Fans out the log call to the logger group and, if a new log was buffered,
|
|
140
|
+
* routes it to the per-job and per-worker collections based on attributes.
|
|
141
|
+
* @param {string} level - The log level.
|
|
142
|
+
* @param {string} message - The log message.
|
|
143
|
+
* @param {object} attributes - Optional structured metadata.
|
|
144
|
+
* @returns {void}
|
|
145
|
+
*/
|
|
146
|
+
#dispatch(level, message, attributes) {
|
|
147
|
+
const before = this.#bufferedLogger.latestLog;
|
|
148
|
+
this.#loggerGroup[level](message, attributes);
|
|
149
|
+
const log = this.#bufferedLogger.latestLog;
|
|
150
|
+
if (log === before) return;
|
|
151
|
+
if (attributes.jobId !== undefined) this.#jobLogs.push(attributes.jobId, log);
|
|
152
|
+
if (attributes.workerId !== undefined) this.#workerLogs.push(attributes.workerId, log);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export { LogRegistryInstance };
|
|
@@ -6,20 +6,30 @@ import { Serializer } from './Serializer.js';
|
|
|
6
6
|
*/
|
|
7
7
|
class JobIndexSerializer extends Serializer {
|
|
8
8
|
/**
|
|
9
|
-
* Serializes a single job instance into a `{ id, status, attempts, jobClass }` object.
|
|
9
|
+
* Serializes a single job instance into a `{ id, status, attempts, jobClass[, url] }` object.
|
|
10
|
+
* For `ResourceRequestJob` instances, the resolved URL is included as `url`.
|
|
11
|
+
* For other job instances that carry an `originUrl` argument, that value is included as `url`.
|
|
10
12
|
*
|
|
11
13
|
* @param {object} job - A job instance.
|
|
12
14
|
* @param {object} options - Serialization options.
|
|
13
15
|
* @param {string} options.status - The status label to embed in the serialized object.
|
|
14
|
-
* @returns {{ id: string, status: string, attempts: number, jobClass: string }} The serialized job data.
|
|
16
|
+
* @returns {{ id: string, status: string, attempts: number, jobClass: string, url?: string }} The serialized job data.
|
|
15
17
|
*/
|
|
16
18
|
static _serializeObject(job, { status }) {
|
|
17
|
-
|
|
19
|
+
const result = {
|
|
18
20
|
id: job.id,
|
|
19
21
|
status,
|
|
20
22
|
attempts: job._attempts,
|
|
21
23
|
jobClass: job.constructor.name,
|
|
22
24
|
};
|
|
25
|
+
|
|
26
|
+
if (job.constructor.name === 'ResourceRequestJob') {
|
|
27
|
+
result.url = job.arguments?.url;
|
|
28
|
+
} else if (job.arguments?.originUrl) {
|
|
29
|
+
result.url = job.arguments.originUrl;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return result;
|
|
23
33
|
}
|
|
24
34
|
}
|
|
25
35
|
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Serializer } from './Serializer.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Serializes one or more Log instances into plain data objects.
|
|
5
|
+
* @augments Serializer
|
|
6
|
+
* @author darthjee
|
|
7
|
+
*/
|
|
8
|
+
class LogSerializer extends Serializer {
|
|
9
|
+
/**
|
|
10
|
+
* Serializes a single Log instance.
|
|
11
|
+
*
|
|
12
|
+
* @param {import('../utils/logging/Log.js').Log} log - The log entry to serialize.
|
|
13
|
+
* @returns {{ id: number, level: string, message: string, attributes: object, timestamp: string }} Plain object representation of the log entry.
|
|
14
|
+
*/
|
|
15
|
+
static _serializeObject(log) {
|
|
16
|
+
return {
|
|
17
|
+
id: log.id,
|
|
18
|
+
level: log.level,
|
|
19
|
+
message: log.message,
|
|
20
|
+
attributes: log.attributes,
|
|
21
|
+
timestamp: log.timestamp.toISOString(),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export { LogSerializer };
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { ConflictError } from '../exceptions/http/ConflictError.js';
|
|
2
|
+
import { ForbiddenError } from '../exceptions/http/ForbiddenError.js';
|
|
3
|
+
import { NotFoundError } from '../exceptions/http/NotFoundError.js';
|
|
4
|
+
import { Logger } from '../utils/logging/Logger.js';
|
|
3
5
|
|
|
4
6
|
/**
|
|
5
7
|
* Registers a route on an Express router by binding the handler's handle method.
|
|
@@ -27,26 +29,57 @@ class RouteRegister {
|
|
|
27
29
|
this.#router.get(route, (req, res) => {
|
|
28
30
|
try {
|
|
29
31
|
handler.handle(req, res);
|
|
32
|
+
Logger.debug(`${req.method} ${req.path} ${res.statusCode}`);
|
|
30
33
|
} catch (e) {
|
|
31
|
-
this.#handleError(e, res);
|
|
34
|
+
this.#handleError(e, req, res);
|
|
32
35
|
}
|
|
33
36
|
});
|
|
34
37
|
}
|
|
35
38
|
|
|
36
39
|
/**
|
|
37
|
-
*
|
|
40
|
+
* Registers a PATCH route on the router.
|
|
41
|
+
* Catches ForbiddenError → 403, NotFoundError → 404, and any other error → 500.
|
|
42
|
+
* @param {object} params - Options for registering a route.
|
|
43
|
+
* @param {string} params.route - The route path (e.g. '/engine/pause').
|
|
44
|
+
* @param {object} params.handler - The handler whose handle method is called.
|
|
45
|
+
* @returns {void}
|
|
46
|
+
*/
|
|
47
|
+
registerPatch({ route, handler }) {
|
|
48
|
+
this.#router.patch(route, async (req, res) => {
|
|
49
|
+
try {
|
|
50
|
+
await handler.handle(req, res);
|
|
51
|
+
Logger.debug(`${req.method} ${req.path} ${res.statusCode}`);
|
|
52
|
+
} catch (e) {
|
|
53
|
+
this.#handleError(e, req, res);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Maps a caught exception to an HTTP error response and logs the access entry.
|
|
38
60
|
* @param {Error} e - The caught exception.
|
|
61
|
+
* @param {object} req - The Express request object.
|
|
39
62
|
* @param {object} res - The Express response object.
|
|
40
63
|
* @returns {void}
|
|
41
64
|
*/
|
|
42
|
-
#handleError(e, res) {
|
|
43
|
-
|
|
44
|
-
|
|
65
|
+
#handleError(e, req, res) {
|
|
66
|
+
let statusCode;
|
|
67
|
+
let message;
|
|
68
|
+
if (e instanceof ConflictError) {
|
|
69
|
+
statusCode = 409;
|
|
70
|
+
message = 'Conflict';
|
|
71
|
+
} else if (e instanceof ForbiddenError) {
|
|
72
|
+
statusCode = 403;
|
|
73
|
+
message = 'Forbidden';
|
|
45
74
|
} else if (e instanceof NotFoundError) {
|
|
46
|
-
|
|
75
|
+
statusCode = 404;
|
|
76
|
+
message = e.message;
|
|
47
77
|
} else {
|
|
48
|
-
|
|
78
|
+
statusCode = 500;
|
|
79
|
+
message = 'Internal Server Error';
|
|
49
80
|
}
|
|
81
|
+
res.status(statusCode).json({ error: message });
|
|
82
|
+
Logger.debug(`${req.method} ${req.path} ${statusCode}`);
|
|
50
83
|
}
|
|
51
84
|
}
|
|
52
85
|
|
package/lib/server/Router.js
CHANGED
|
@@ -1,13 +1,24 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
2
|
import { fileURLToPath } from 'url';
|
|
3
3
|
import express from 'express';
|
|
4
|
-
import { AssetsRequestHandler } from './AssetsRequestHandler.js';
|
|
5
|
-
import { BaseUrlsRequestHandler } from './BaseUrlsRequestHandler.js';
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
4
|
+
import { AssetsRequestHandler } from './handlers/AssetsRequestHandler.js';
|
|
5
|
+
import { BaseUrlsRequestHandler } from './handlers/BaseUrlsRequestHandler.js';
|
|
6
|
+
import { EngineContinueRequestHandler } from './handlers/engine/EngineContinueRequestHandler.js';
|
|
7
|
+
import { EnginePauseRequestHandler } from './handlers/engine/EnginePauseRequestHandler.js';
|
|
8
|
+
import { EngineRestartRequestHandler } from './handlers/engine/EngineRestartRequestHandler.js';
|
|
9
|
+
import { EngineShutdownRequestHandler } from './handlers/engine/EngineShutdownRequestHandler.js';
|
|
10
|
+
import { EngineStartRequestHandler } from './handlers/engine/EngineStartRequestHandler.js';
|
|
11
|
+
import { EngineStatusRequestHandler } from './handlers/engine/EngineStatusRequestHandler.js';
|
|
12
|
+
import { EngineStopRequestHandler } from './handlers/engine/EngineStopRequestHandler.js';
|
|
13
|
+
import { IndexRequestHandler } from './handlers/IndexRequestHandler.js';
|
|
14
|
+
import { JobLogsRequestHandler } from './handlers/jobs/JobLogsRequestHandler.js';
|
|
15
|
+
import { JobRequestHandler } from './handlers/jobs/JobRequestHandler.js';
|
|
16
|
+
import { JobRetryRequestHandler } from './handlers/jobs/JobRetryRequestHandler.js';
|
|
17
|
+
import { JobsRequestHandler } from './handlers/jobs/JobsRequestHandler.js';
|
|
18
|
+
import { LogsRequestHandler } from './handlers/LogsRequestHandler.js';
|
|
19
|
+
import { SettingsRequestHandler } from './handlers/SettingsRequestHandler.js';
|
|
20
|
+
import { StatsRequestHandler } from './handlers/StatsRequestHandler.js';
|
|
9
21
|
import { RouteRegister } from './RouteRegister.js';
|
|
10
|
-
import { StatsRequestHandler } from './StatsRequestHandler.js';
|
|
11
22
|
|
|
12
23
|
const { Router: ExpressRouter } = express;
|
|
13
24
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
@@ -18,10 +29,16 @@ const staticDir = path.join(__dirname, '../../static');
|
|
|
18
29
|
* @author darthjee
|
|
19
30
|
*/
|
|
20
31
|
class Router {
|
|
32
|
+
#webConfig;
|
|
33
|
+
|
|
21
34
|
/**
|
|
22
35
|
* Creates a new Router instance.
|
|
36
|
+
* @param {object} [options={}] - Constructor options.
|
|
37
|
+
* @param {object} [options.webConfig={}] - Web configuration, used by handlers that need it.
|
|
23
38
|
*/
|
|
24
|
-
constructor() {
|
|
39
|
+
constructor({ webConfig = {} } = {}) {
|
|
40
|
+
this.#webConfig = webConfig;
|
|
41
|
+
}
|
|
25
42
|
|
|
26
43
|
/**
|
|
27
44
|
* Creates and returns an Express Router with all routes registered.
|
|
@@ -31,34 +48,35 @@ class Router {
|
|
|
31
48
|
const router = ExpressRouter();
|
|
32
49
|
const register = new RouteRegister(router);
|
|
33
50
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
handler: new JobsRequestHandler(),
|
|
47
|
-
});
|
|
51
|
+
const GET_ROUTES = {
|
|
52
|
+
'/settings.json': new SettingsRequestHandler({ enableShutdown: this.#webConfig.enableShutdown }),
|
|
53
|
+
'/stats.json': new StatsRequestHandler(),
|
|
54
|
+
'/clients/base_urls.json': new BaseUrlsRequestHandler(),
|
|
55
|
+
'/jobs/:status.json': new JobsRequestHandler(),
|
|
56
|
+
'/jobs/:job_id/logs.json': new JobLogsRequestHandler({ pageSize: this.#webConfig.logsPageSize }),
|
|
57
|
+
'/job/:id.json': new JobRequestHandler(),
|
|
58
|
+
'/engine/status': new EngineStatusRequestHandler(),
|
|
59
|
+
'/logs.json': new LogsRequestHandler({ pageSize: this.#webConfig.logsPageSize }),
|
|
60
|
+
'/': new IndexRequestHandler(),
|
|
61
|
+
'/assets/*path': new AssetsRequestHandler(),
|
|
62
|
+
};
|
|
48
63
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
64
|
+
const PATCH_ROUTES = {
|
|
65
|
+
'/jobs/:id/retry': new JobRetryRequestHandler(),
|
|
66
|
+
'/engine/pause': new EnginePauseRequestHandler(),
|
|
67
|
+
'/engine/stop': new EngineStopRequestHandler(),
|
|
68
|
+
'/engine/continue': new EngineContinueRequestHandler(),
|
|
69
|
+
'/engine/start': new EngineStartRequestHandler(),
|
|
70
|
+
'/engine/restart': new EngineRestartRequestHandler(),
|
|
71
|
+
'/engine/shutdown': new EngineShutdownRequestHandler(),
|
|
72
|
+
};
|
|
53
73
|
|
|
54
|
-
|
|
55
|
-
route
|
|
56
|
-
handler: new IndexRequestHandler(),
|
|
74
|
+
Object.entries(GET_ROUTES).forEach(([route, handler]) => {
|
|
75
|
+
register.register({ route, handler });
|
|
57
76
|
});
|
|
58
77
|
|
|
59
|
-
|
|
60
|
-
route
|
|
61
|
-
handler: new AssetsRequestHandler(),
|
|
78
|
+
Object.entries(PATCH_ROUTES).forEach(([route, handler]) => {
|
|
79
|
+
register.registerPatch({ route, handler });
|
|
62
80
|
});
|
|
63
81
|
|
|
64
82
|
router.use(express.static(staticDir));
|
package/lib/server/WebServer.js
CHANGED
|
@@ -9,25 +9,45 @@ import { Logger } from '../utils/logging/Logger.js';
|
|
|
9
9
|
class WebServer {
|
|
10
10
|
#port;
|
|
11
11
|
#app;
|
|
12
|
+
#httpServer;
|
|
13
|
+
#startPromise;
|
|
12
14
|
|
|
13
15
|
/**
|
|
14
16
|
* @param {object} params - Options for initializing the WebServer.
|
|
15
|
-
* @param {
|
|
17
|
+
* @param {object} params.webConfig - The web configuration object.
|
|
16
18
|
*/
|
|
17
|
-
constructor({
|
|
18
|
-
this.#port = port;
|
|
19
|
+
constructor({ webConfig }) {
|
|
20
|
+
this.#port = webConfig.port;
|
|
19
21
|
this.#app = express();
|
|
20
|
-
this.#app.use(new Router().build());
|
|
22
|
+
this.#app.use(new Router({ webConfig }).build());
|
|
21
23
|
}
|
|
22
24
|
|
|
23
25
|
/**
|
|
24
26
|
* Starts the Express server on the configured port.
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
+
* Returns a Promise that resolves when the HTTP server fires its 'close' event,
|
|
28
|
+
* or rejects if the server fails to start.
|
|
29
|
+
* @returns {Promise<void>} A Promise that resolves when the server closes.
|
|
27
30
|
*/
|
|
28
31
|
start() {
|
|
29
32
|
Logger.info(`Listening to port ${this.#port}`);
|
|
30
|
-
|
|
33
|
+
this.#startPromise = new Promise((resolve, reject) => {
|
|
34
|
+
this.#httpServer = this.#app.listen(this.#port);
|
|
35
|
+
this.#httpServer.on('close', resolve);
|
|
36
|
+
this.#httpServer.on('error', reject);
|
|
37
|
+
});
|
|
38
|
+
return this.#startPromise;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Closes the HTTP server. Stops accepting new connections and forcibly closes
|
|
43
|
+
* any remaining keep-alive connections on the next event-loop tick so that
|
|
44
|
+
* the start() promise can resolve.
|
|
45
|
+
* @returns {Promise<void>|undefined} The promise from start(), or undefined if not started.
|
|
46
|
+
*/
|
|
47
|
+
shutdown() {
|
|
48
|
+
this.#httpServer?.close();
|
|
49
|
+
setImmediate(() => this.#httpServer?.closeAllConnections());
|
|
50
|
+
return this.#startPromise;
|
|
31
51
|
}
|
|
32
52
|
|
|
33
53
|
/**
|
|
@@ -38,7 +58,7 @@ class WebServer {
|
|
|
38
58
|
*/
|
|
39
59
|
static build({ webConfig }) {
|
|
40
60
|
if (!webConfig) return null;
|
|
41
|
-
return new WebServer({
|
|
61
|
+
return new WebServer({ webConfig });
|
|
42
62
|
}
|
|
43
63
|
}
|
|
44
64
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
2
|
import { fileURLToPath } from 'url';
|
|
3
|
-
import { PathValidator } from '
|
|
4
|
-
import { RequestHandler } from '
|
|
3
|
+
import { PathValidator } from '../PathValidator.js';
|
|
4
|
+
import { RequestHandler } from '../RequestHandler.js';
|
|
5
5
|
|
|
6
6
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
7
7
|
|
|
@@ -16,7 +16,7 @@ class AssetsRequestHandler extends RequestHandler {
|
|
|
16
16
|
*/
|
|
17
17
|
constructor() {
|
|
18
18
|
super();
|
|
19
|
-
this.assetsDir = path.resolve(__dirname, '
|
|
19
|
+
this.assetsDir = path.resolve(__dirname, '../../../static/assets');
|
|
20
20
|
this.validator = new PathValidator(this.assetsDir);
|
|
21
21
|
}
|
|
22
22
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { ClientRegistry } from '../../registry/ClientRegistry.js';
|
|
2
|
+
import { BaseUrlsSerializer } from '../../serializers/BaseUrlsSerializer.js';
|
|
3
|
+
import { RequestHandler } from '../RequestHandler.js';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Handles GET /clients/base_urls.json requests.
|