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.
Files changed (72) hide show
  1. package/README.md +4 -0
  2. package/bin/navi.js +3 -3
  3. package/lib/{factories → background}/JobFactory.js +2 -2
  4. package/lib/{registry → background}/JobRegistry.js +18 -1
  5. package/lib/{registry → background}/JobRegistryInstance.js +51 -6
  6. package/lib/{models → background}/Worker.js +5 -3
  7. package/lib/{factories → background}/WorkerFactory.js +2 -2
  8. package/lib/{registry → background}/WorkersRegistryInstance.js +1 -1
  9. package/lib/{models → enqueuers}/ActionEnqueuer.js +4 -1
  10. package/lib/{models → enqueuers}/AssetRequestEnqueuer.js +6 -2
  11. package/lib/exceptions/ConflictError.js +14 -0
  12. package/lib/{models → jobs}/ActionProcessingJob.js +4 -4
  13. package/lib/{models → jobs}/AssetDownloadJob.js +6 -6
  14. package/lib/{models → jobs}/HtmlParseJob.js +6 -6
  15. package/lib/{models → jobs}/ResourceRequestJob.js +11 -11
  16. package/lib/models/Config.js +4 -2
  17. package/lib/models/FailureConfig.js +36 -0
  18. package/lib/models/ResourceRequest.js +5 -2
  19. package/lib/models/ResourceRequestAction.js +6 -3
  20. package/lib/models/WebConfig.js +5 -1
  21. package/lib/registry/ClientRegistry.js +60 -2
  22. package/lib/registry/LogRegistry.js +146 -0
  23. package/lib/registry/LogRegistryInstance.js +156 -0
  24. package/lib/serializers/BaseUrlsSerializer.js +18 -0
  25. package/lib/serializers/JobIndexSerializer.js +10 -3
  26. package/lib/serializers/LogSerializer.js +26 -0
  27. package/lib/server/BaseUrlsRequestHandler.js +33 -0
  28. package/lib/server/EngineContinueRequestHandler.js +34 -0
  29. package/lib/server/EnginePauseRequestHandler.js +34 -0
  30. package/lib/server/EngineRestartRequestHandler.js +34 -0
  31. package/lib/server/EngineShutdownRequestHandler.js +29 -0
  32. package/lib/server/EngineStartRequestHandler.js +34 -0
  33. package/lib/server/EngineStatusRequestHandler.js +27 -0
  34. package/lib/server/EngineStopRequestHandler.js +34 -0
  35. package/lib/server/JobLogsRequestHandler.js +39 -0
  36. package/lib/server/JobRequestHandler.js +1 -1
  37. package/lib/server/JobRetryRequestHandler.js +40 -0
  38. package/lib/server/JobsFilter.js +55 -0
  39. package/lib/server/JobsRequestHandler.js +5 -2
  40. package/lib/server/LogsRequestHandler.js +55 -0
  41. package/lib/server/RouteRegister.js +40 -7
  42. package/lib/server/Router.js +44 -20
  43. package/lib/server/SettingsRequestHandler.js +34 -0
  44. package/lib/server/StatsRequestHandler.js +2 -2
  45. package/lib/server/WebServer.js +28 -8
  46. package/lib/services/Application.js +135 -81
  47. package/lib/services/ApplicationInstance.js +272 -0
  48. package/lib/services/Client.js +27 -22
  49. package/lib/services/ConfigLoader.js +2 -1
  50. package/lib/services/ConfigParser.js +13 -2
  51. package/lib/services/Engine.js +51 -6
  52. package/lib/services/EngineEvents.js +42 -0
  53. package/lib/services/FailureChecker.js +78 -0
  54. package/lib/services/WorkersAllocator.js +2 -2
  55. package/lib/utils/EnvResolver.js +22 -0
  56. package/lib/utils/HtmlElementParser.js +5 -4
  57. package/lib/utils/HtmlParser.js +4 -4
  58. package/lib/utils/PromiseAggregator.js +11 -5
  59. package/lib/utils/logging/BufferedLogger.js +8 -0
  60. package/lib/utils/logging/LogBuffer.js +25 -5
  61. package/lib/utils/logging/LogBufferCollection.js +56 -0
  62. package/lib/utils/logging/LogContext.js +51 -0
  63. package/lib/utils/logging/LogFilter.js +34 -0
  64. package/package.json +1 -1
  65. package/static/assets/index-BfggoMvd.js +11 -0
  66. package/static/assets/{index-DBkz-poX.css → index-CuWjW0sT.css} +1 -1
  67. package/static/index.html +2 -2
  68. package/static/assets/index-BF8_ow13.js +0 -11
  69. /package/lib/{models → background}/Job.js +0 -0
  70. /package/lib/{registry → background}/WorkersRegistry.js +0 -0
  71. /package/lib/{models → enqueuers}/ActionsEnqueuer.js +0 -0
  72. /package/lib/{factories → factory}/Factory.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
58
+ * @returns {import('../utils/logging/Log.js').Log|undefined}
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={}]
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>}
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
79
+ * @param {object} [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>}
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
90
+ * @returns {Array<import('../utils/logging/Log.js').Log>}
91
+ */
92
+ static getLogsByWorkerId(workerId) {
93
+ return LogRegistry.#getInstance().getLogsByWorkerId(workerId);
94
+ }
95
+
96
+ /**
97
+ * Gets logs filtered by level.
98
+ * @param {string} level
99
+ * @returns {Array<import('../utils/logging/Log.js').Log>}
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>}
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}
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}
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
63
+ * @returns {import('../utils/logging/Log.js').Log|undefined}
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={}]
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>}
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
84
+ * @param {object} [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>}
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
95
+ * @returns {Array<import('../utils/logging/Log.js').Log>}
96
+ */
97
+ getLogsByWorkerId(workerId) {
98
+ return this.#workerLogs.getLogs(workerId);
99
+ }
100
+
101
+ /**
102
+ * Gets logs filtered by level.
103
+ * @param {string} level
104
+ * @returns {Array<import('../utils/logging/Log.js').Log>}
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>}
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
142
+ * @param {string} message
143
+ * @param {object} attributes
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 };
@@ -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 };
@@ -6,20 +6,27 @@ 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`.
10
11
  *
11
12
  * @param {object} job - A job instance.
12
13
  * @param {object} options - Serialization options.
13
14
  * @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.
15
+ * @returns {{ id: string, status: string, attempts: number, jobClass: string, url?: string }} The serialized job data.
15
16
  */
16
17
  static _serializeObject(job, { status }) {
17
- return {
18
+ const result = {
18
19
  id: job.id,
19
20
  status,
20
21
  attempts: job._attempts,
21
22
  jobClass: job.constructor.name,
22
23
  };
24
+
25
+ if (job.constructor.name === 'ResourceRequestJob') {
26
+ result.url = job.arguments?.url;
27
+ }
28
+
29
+ return result;
23
30
  }
24
31
  }
25
32
 
@@ -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 }}
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 };
@@ -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 };
@@ -0,0 +1,34 @@
1
+ import { RequestHandler } from './RequestHandler.js';
2
+ import { ConflictError } from '../exceptions/ConflictError.js';
3
+ import { Application } from '../services/Application.js';
4
+
5
+ /**
6
+ * Handles PATCH /engine/continue requests.
7
+ * Returns 409 Conflict if the engine is not in 'paused' status.
8
+ * @author darthjee
9
+ */
10
+ class EngineContinueRequestHandler extends RequestHandler {
11
+ /**
12
+ * Creates a new EngineContinueRequestHandler instance.
13
+ */
14
+ constructor() {
15
+ super();
16
+ }
17
+
18
+ /**
19
+ * Resumes engine processing after a pause.
20
+ * @param {object} _req - The Express request object.
21
+ * @param {object} res - The Express response object.
22
+ * @returns {Promise<void>}
23
+ */
24
+ async handle(_req, res) {
25
+ if (!Application.isPaused()) {
26
+ throw new ConflictError();
27
+ }
28
+
29
+ await Application.continue();
30
+ res.json({ status: 'running' });
31
+ }
32
+ }
33
+
34
+ export { EngineContinueRequestHandler };
@@ -0,0 +1,34 @@
1
+ import { RequestHandler } from './RequestHandler.js';
2
+ import { ConflictError } from '../exceptions/ConflictError.js';
3
+ import { Application } from '../services/Application.js';
4
+
5
+ /**
6
+ * Handles PATCH /engine/pause requests.
7
+ * Returns 409 Conflict if the engine is not in 'running' status.
8
+ * @author darthjee
9
+ */
10
+ class EnginePauseRequestHandler extends RequestHandler {
11
+ /**
12
+ * Creates a new EnginePauseRequestHandler instance.
13
+ */
14
+ constructor() {
15
+ super();
16
+ }
17
+
18
+ /**
19
+ * Initiates engine pause. Responds immediately with the transitional status.
20
+ * @param {object} _req - The Express request object.
21
+ * @param {object} res - The Express response object.
22
+ * @returns {Promise<void>}
23
+ */
24
+ async handle(_req, res) {
25
+ if (!Application.isRunning()) {
26
+ throw new ConflictError();
27
+ }
28
+
29
+ Application.pause();
30
+ res.json({ status: 'pausing' });
31
+ }
32
+ }
33
+
34
+ export { EnginePauseRequestHandler };
@@ -0,0 +1,34 @@
1
+ import { RequestHandler } from './RequestHandler.js';
2
+ import { ConflictError } from '../exceptions/ConflictError.js';
3
+ import { Application } from '../services/Application.js';
4
+
5
+ /**
6
+ * Handles PATCH /engine/restart requests.
7
+ * Returns 409 Conflict if the engine is not in 'running' status.
8
+ * @author darthjee
9
+ */
10
+ class EngineRestartRequestHandler extends RequestHandler {
11
+ /**
12
+ * Creates a new EngineRestartRequestHandler instance.
13
+ */
14
+ constructor() {
15
+ super();
16
+ }
17
+
18
+ /**
19
+ * Initiates engine restart. Responds immediately with the transitional status.
20
+ * @param {object} _req - The Express request object.
21
+ * @param {object} res - The Express response object.
22
+ * @returns {Promise<void>}
23
+ */
24
+ async handle(_req, res) {
25
+ if (!Application.isRunning()) {
26
+ throw new ConflictError();
27
+ }
28
+
29
+ Application.restart();
30
+ res.json({ status: 'stopping' });
31
+ }
32
+ }
33
+
34
+ export { EngineRestartRequestHandler };
@@ -0,0 +1,29 @@
1
+ import { RequestHandler } from './RequestHandler.js';
2
+ import { Application } from '../services/Application.js';
3
+
4
+ /**
5
+ * Handles PATCH /engine/shutdown requests.
6
+ * Shuts down the web server and stops the engine.
7
+ * @author darthjee
8
+ */
9
+ class EngineShutdownRequestHandler extends RequestHandler {
10
+ /**
11
+ * Creates a new EngineShutdownRequestHandler instance.
12
+ */
13
+ constructor() {
14
+ super();
15
+ }
16
+
17
+ /**
18
+ * Initiates server shutdown. Responds immediately with the transitional status.
19
+ * @param {object} _req - The Express request object.
20
+ * @param {object} res - The Express response object.
21
+ * @returns {void}
22
+ */
23
+ handle(_req, res) {
24
+ Application.shutdown();
25
+ res.json({ status: 'stopping' });
26
+ }
27
+ }
28
+
29
+ export { EngineShutdownRequestHandler };
@@ -0,0 +1,34 @@
1
+ import { RequestHandler } from './RequestHandler.js';
2
+ import { ConflictError } from '../exceptions/ConflictError.js';
3
+ import { Application } from '../services/Application.js';
4
+
5
+ /**
6
+ * Handles PATCH /engine/start requests.
7
+ * Returns 409 Conflict if the engine is not in 'stopped' status.
8
+ * @author darthjee
9
+ */
10
+ class EngineStartRequestHandler extends RequestHandler {
11
+ /**
12
+ * Creates a new EngineStartRequestHandler instance.
13
+ */
14
+ constructor() {
15
+ super();
16
+ }
17
+
18
+ /**
19
+ * Starts engine processing from a stopped state, re-enqueueing initial jobs.
20
+ * @param {object} _req - The Express request object.
21
+ * @param {object} res - The Express response object.
22
+ * @returns {Promise<void>}
23
+ */
24
+ async handle(_req, res) {
25
+ if (!Application.isStopped()) {
26
+ throw new ConflictError();
27
+ }
28
+
29
+ await Application.start();
30
+ res.json({ status: 'running' });
31
+ }
32
+ }
33
+
34
+ export { EngineStartRequestHandler };
@@ -0,0 +1,27 @@
1
+ import { RequestHandler } from './RequestHandler.js';
2
+ import { Application } from '../services/Application.js';
3
+
4
+ /**
5
+ * Handles GET /engine/status requests.
6
+ * @author darthjee
7
+ */
8
+ class EngineStatusRequestHandler extends RequestHandler {
9
+ /**
10
+ * Creates a new EngineStatusRequestHandler instance.
11
+ */
12
+ constructor() {
13
+ super();
14
+ }
15
+
16
+ /**
17
+ * Responds with the current engine status.
18
+ * @param {object} _req - The Express request object.
19
+ * @param {object} res - The Express response object.
20
+ * @returns {void}
21
+ */
22
+ handle(_req, res) {
23
+ res.json({ status: Application.status() });
24
+ }
25
+ }
26
+
27
+ export { EngineStatusRequestHandler };
@@ -0,0 +1,34 @@
1
+ import { RequestHandler } from './RequestHandler.js';
2
+ import { ConflictError } from '../exceptions/ConflictError.js';
3
+ import { Application } from '../services/Application.js';
4
+
5
+ /**
6
+ * Handles PATCH /engine/stop requests.
7
+ * Returns 409 Conflict if the engine is not in 'running' status.
8
+ * @author darthjee
9
+ */
10
+ class EngineStopRequestHandler extends RequestHandler {
11
+ /**
12
+ * Creates a new EngineStopRequestHandler instance.
13
+ */
14
+ constructor() {
15
+ super();
16
+ }
17
+
18
+ /**
19
+ * Initiates engine stop. Responds immediately with the transitional status.
20
+ * @param {object} _req - The Express request object.
21
+ * @param {object} res - The Express response object.
22
+ * @returns {Promise<void>}
23
+ */
24
+ async handle(_req, res) {
25
+ if (!Application.isRunning()) {
26
+ throw new ConflictError();
27
+ }
28
+
29
+ Application.stop();
30
+ res.json({ status: 'stopping' });
31
+ }
32
+ }
33
+
34
+ export { EngineStopRequestHandler };
@@ -0,0 +1,39 @@
1
+ import { RequestHandler } from './RequestHandler.js';
2
+ import { LogRegistry } from '../registry/LogRegistry.js';
3
+ import { LogSerializer } from '../serializers/LogSerializer.js';
4
+
5
+ /**
6
+ * Handles GET /jobs/:job_id/logs.json requests.
7
+ * Returns a paginated list of log entries for the given job, optionally
8
+ * filtered to entries newer than last_id.
9
+ * An unknown job_id returns an empty array.
10
+ * @author darthjee
11
+ */
12
+ class JobLogsRequestHandler extends RequestHandler {
13
+ #pageSize;
14
+
15
+ /**
16
+ * Creates a new JobLogsRequestHandler.
17
+ * @param {object} [options={}]
18
+ * @param {number} [options.pageSize=20] - Maximum number of log entries to return.
19
+ */
20
+ constructor({ pageSize = 20 } = {}) {
21
+ super();
22
+ this.#pageSize = pageSize;
23
+ }
24
+
25
+ /**
26
+ * Responds with a paginated JSON array of log entries for the given job.
27
+ * @param {object} req - The Express request object.
28
+ * @param {object} res - The Express response object.
29
+ * @returns {void}
30
+ */
31
+ handle(req, res) {
32
+ const { job_id: jobId } = req.params;
33
+ const { last_id: lastId } = req.query;
34
+ const logs = LogRegistry.getLogsByJobId(jobId, { lastId });
35
+ res.json(LogSerializer.serialize(logs.slice(0, this.#pageSize)));
36
+ }
37
+ }
38
+
39
+ export { JobLogsRequestHandler };