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.
Files changed (107) hide show
  1. package/README.md +38 -4
  2. package/bin/navi.js +3 -3
  3. package/lib/{factories → background}/JobFactory.js +2 -2
  4. package/lib/{registry → background}/JobRegistry.js +17 -0
  5. package/lib/{registry → background}/JobRegistryInstance.js +42 -1
  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/enqueuers/ActionEnqueuer.js +53 -0
  10. package/lib/{models → enqueuers}/ActionsEnqueuer.js +6 -3
  11. package/lib/{models → enqueuers}/AssetRequestEnqueuer.js +6 -2
  12. package/lib/enqueuers/PaginatedActionEnqueuer.js +53 -0
  13. package/lib/enqueuers/PaginatedActionsEnqueuer.js +47 -0
  14. package/lib/exceptions/{ConfigurationFileNotFound.js → config/ConfigurationFileNotFound.js} +1 -1
  15. package/lib/exceptions/{ConfigurationFileNotProvided.js → config/ConfigurationFileNotProvided.js} +1 -1
  16. package/lib/exceptions/{MissingTopLevelConfgKey.js → config/MissingTopLevelConfgKey.js} +1 -1
  17. package/lib/exceptions/http/ConflictError.js +14 -0
  18. package/lib/exceptions/{ForbiddenError.js → http/ForbiddenError.js} +1 -1
  19. package/lib/exceptions/{NotFoundError.js → http/NotFoundError.js} +1 -1
  20. package/lib/exceptions/{ItemNotFound.js → registry/ItemNotFound.js} +1 -1
  21. package/lib/exceptions/{MissingActionResource.js → registry/MissingActionResource.js} +1 -1
  22. package/lib/exceptions/{MissingMappingVariable.js → registry/MissingMappingVariable.js} +1 -1
  23. package/lib/exceptions/{InvalidHtmlResponseBody.js → request/InvalidHtmlResponseBody.js} +1 -1
  24. package/lib/exceptions/{InvalidResponseBody.js → request/InvalidResponseBody.js} +1 -1
  25. package/lib/exceptions/{NullResponse.js → request/NullResponse.js} +1 -1
  26. package/lib/exceptions/{RequestFailed.js → request/RequestFailed.js} +1 -1
  27. package/lib/{models → jobs}/ActionProcessingJob.js +21 -7
  28. package/lib/{models → jobs}/AssetDownloadJob.js +6 -6
  29. package/lib/{models → jobs}/HtmlParseJob.js +23 -10
  30. package/lib/jobs/PaginatedActionProcessingJob.js +77 -0
  31. package/lib/{models → jobs}/ResourceRequestJob.js +18 -15
  32. package/lib/models/{Config.js → configs/Config.js} +3 -3
  33. package/lib/models/configs/PaginationConfig.js +63 -0
  34. package/lib/models/configs/WebConfig.js +19 -0
  35. package/lib/models/{ResourceRequest.js → request/ResourceRequest.js} +32 -7
  36. package/lib/models/{ResourceRequestAction.js → request/ResourceRequestAction.js} +9 -6
  37. package/lib/models/request/ResourceRequestPaginatedAction.js +81 -0
  38. package/lib/models/{PathSegmentTraverser.js → response/PathSegmentTraverser.js} +1 -1
  39. package/lib/models/{ResponseParser.js → response/ResponseParser.js} +1 -1
  40. package/lib/registry/ClientRegistry.js +1 -1
  41. package/lib/registry/LogRegistry.js +146 -0
  42. package/lib/registry/LogRegistryInstance.js +156 -0
  43. package/lib/registry/NamedRegistry.js +1 -1
  44. package/lib/registry/ResourceRegistry.js +1 -1
  45. package/lib/serializers/JobIndexSerializer.js +13 -3
  46. package/lib/serializers/LogSerializer.js +26 -0
  47. package/lib/server/PathValidator.js +1 -1
  48. package/lib/server/RouteRegister.js +42 -9
  49. package/lib/server/Router.js +49 -31
  50. package/lib/server/WebServer.js +28 -8
  51. package/lib/server/{AssetsRequestHandler.js → handlers/AssetsRequestHandler.js} +3 -3
  52. package/lib/server/{BaseUrlsRequestHandler.js → handlers/BaseUrlsRequestHandler.js} +3 -3
  53. package/lib/server/{IndexRequestHandler.js → handlers/IndexRequestHandler.js} +2 -2
  54. package/lib/server/handlers/JobsFilter.js +55 -0
  55. package/lib/server/handlers/LogsRequestHandler.js +55 -0
  56. package/lib/server/handlers/SettingsRequestHandler.js +35 -0
  57. package/lib/server/{StatsRequestHandler.js → handlers/StatsRequestHandler.js} +3 -3
  58. package/lib/server/handlers/engine/EngineContinueRequestHandler.js +34 -0
  59. package/lib/server/handlers/engine/EnginePauseRequestHandler.js +34 -0
  60. package/lib/server/handlers/engine/EngineRestartRequestHandler.js +34 -0
  61. package/lib/server/handlers/engine/EngineShutdownRequestHandler.js +29 -0
  62. package/lib/server/handlers/engine/EngineStartRequestHandler.js +34 -0
  63. package/lib/server/handlers/engine/EngineStatusRequestHandler.js +27 -0
  64. package/lib/server/handlers/engine/EngineStopRequestHandler.js +34 -0
  65. package/lib/server/handlers/jobs/JobLogsRequestHandler.js +39 -0
  66. package/lib/server/{JobRequestHandler.js → handlers/jobs/JobRequestHandler.js} +4 -4
  67. package/lib/server/handlers/jobs/JobRetryRequestHandler.js +40 -0
  68. package/lib/server/{JobsRequestHandler.js → handlers/jobs/JobsRequestHandler.js} +7 -4
  69. package/lib/services/Application.js +138 -84
  70. package/lib/services/ApplicationInstance.js +274 -0
  71. package/lib/services/Client.js +23 -19
  72. package/lib/services/ConfigLoader.js +1 -1
  73. package/lib/services/ConfigParser.js +7 -7
  74. package/lib/services/Engine.js +51 -6
  75. package/lib/services/EngineEvents.js +42 -0
  76. package/lib/services/FailureChecker.js +4 -4
  77. package/lib/services/WorkersAllocator.js +2 -2
  78. package/lib/utils/HtmlElementParser.js +5 -4
  79. package/lib/utils/HtmlParser.js +5 -5
  80. package/lib/utils/PromiseAggregator.js +11 -5
  81. package/lib/utils/logging/BufferedLogger.js +8 -0
  82. package/lib/utils/logging/LogBuffer.js +25 -5
  83. package/lib/utils/logging/LogBufferCollection.js +56 -0
  84. package/lib/utils/logging/LogContext.js +51 -0
  85. package/lib/utils/logging/LogFilter.js +34 -0
  86. package/package.json +1 -1
  87. package/static/assets/{index-DBkz-poX.css → index-CuWjW0sT.css} +1 -1
  88. package/static/assets/index-D5tOOLpl.js +11 -0
  89. package/static/index.html +2 -2
  90. package/lib/exceptions/LockedByOtherWorker.js +0 -14
  91. package/lib/models/ActionEnqueuer.js +0 -34
  92. package/lib/models/WebConfig.js +0 -15
  93. /package/lib/{models → background}/Job.js +0 -0
  94. /package/lib/{registry → background}/WorkersRegistry.js +0 -0
  95. /package/lib/exceptions/{MissingClientsConfig.js → config/MissingClientsConfig.js} +0 -0
  96. /package/lib/exceptions/{MissingResourceConfig.js → config/MissingResourceConfig.js} +0 -0
  97. /package/lib/exceptions/{ClientNotFound.js → registry/ClientNotFound.js} +0 -0
  98. /package/lib/exceptions/{ResourceNotFound.js → registry/ResourceNotFound.js} +0 -0
  99. /package/lib/{factories → factory}/Factory.js +0 -0
  100. /package/lib/models/{FailureConfig.js → configs/FailureConfig.js} +0 -0
  101. /package/lib/models/{LogConfig.js → configs/LogConfig.js} +0 -0
  102. /package/lib/models/{WorkersConfig.js → configs/WorkersConfig.js} +0 -0
  103. /package/lib/models/{AssetRequest.js → request/AssetRequest.js} +0 -0
  104. /package/lib/models/{Resource.js → request/Resource.js} +0 -0
  105. /package/lib/models/{ParametersMapper.js → response/ParametersMapper.js} +0 -0
  106. /package/lib/models/{PathResolver.js → response/PathResolver.js} +0 -0
  107. /package/lib/models/{ResponseWrapper.js → response/ResponseWrapper.js} +0 -0
@@ -1,9 +1,9 @@
1
1
  import path from 'path';
2
2
  import { fileURLToPath } from 'url';
3
- import { RequestHandler } from './RequestHandler.js';
3
+ import { RequestHandler } from '../RequestHandler.js';
4
4
 
5
5
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
6
- const staticDir = path.resolve(__dirname, '../../static');
6
+ const staticDir = path.resolve(__dirname, '../../../static');
7
7
 
8
8
  /**
9
9
  * Handles GET / and SPA catch-all requests by serving index.html.
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Filters a job collection by one or more job classes.
3
+ * @author darthjee
4
+ */
5
+ class JobsFilter {
6
+ /**
7
+ * Creates a new JobsFilter instance.
8
+ * @param {object[]} jobs - The collection of jobs to filter.
9
+ * @param {object} [filters={}] - The filters object (e.g. `{ class: ['ResourceRequestJob'] }`).
10
+ */
11
+ constructor(jobs, filters = {}) {
12
+ this._jobs = jobs;
13
+ this._filters = filters;
14
+ }
15
+
16
+ /**
17
+ * Returns the filtered job list.
18
+ * When no class filter is provided, all jobs are returned unchanged.
19
+ * @returns {object[]} The filtered jobs.
20
+ */
21
+ filter() {
22
+ const classes = this.#normalizeClasses();
23
+
24
+ if (classes.length === 0) {
25
+ return this._jobs;
26
+ }
27
+
28
+ return this._jobs.filter((job) => this.#matchesClass(job, classes));
29
+ }
30
+
31
+ /**
32
+ * Returns true when the job's class is listed in the requested classes.
33
+ * @param {object} job - A job instance.
34
+ * @param {string[]} classes - The list of accepted class names.
35
+ * @returns {boolean} True if the job's class is in the accepted list.
36
+ */
37
+ #matchesClass(job, classes) {
38
+ return classes.includes(job.constructor.name);
39
+ }
40
+
41
+ /**
42
+ * Normalises the class filter value to an array.
43
+ * Express's qs parser collapses a single `filters[class][]=Foo` to a plain string.
44
+ * @returns {string[]} The normalised array of class names.
45
+ */
46
+ #normalizeClasses() {
47
+ const value = this._filters.class;
48
+
49
+ if (!value) return [];
50
+ if (Array.isArray(value)) return value;
51
+ return [value];
52
+ }
53
+ }
54
+
55
+ export { JobsFilter };
@@ -0,0 +1,55 @@
1
+ import { LogRegistry } from '../../registry/LogRegistry.js';
2
+ import { LogSerializer } from '../../serializers/LogSerializer.js';
3
+ import { RequestHandler } from '../RequestHandler.js';
4
+
5
+ /**
6
+ * Handles GET /logs.json requests.
7
+ * Routes to per-job or per-worker buffers via optional `jobId` / `workerId` query params.
8
+ * Returns 400 when both params are present simultaneously.
9
+ * @author darthjee
10
+ */
11
+ class LogsRequestHandler extends RequestHandler {
12
+ #pageSize;
13
+
14
+ /**
15
+ * Creates a new LogsRequestHandler.
16
+ * @param {object} [options={}] - Constructor options.
17
+ * @param {number} [options.pageSize=20] - Maximum number of log entries to return.
18
+ */
19
+ constructor({ pageSize = 20 } = {}) {
20
+ super();
21
+ this.#pageSize = pageSize;
22
+ }
23
+
24
+ /**
25
+ * Responds with a paginated JSON array of log entries.
26
+ * @param {object} req - The Express request object.
27
+ * @param {object} res - The Express response object.
28
+ * @returns {void}
29
+ */
30
+ handle(req, res) {
31
+ const { last_id: lastId, jobId, workerId } = req.query;
32
+
33
+ if (jobId && workerId) {
34
+ res.status(400).json({ error: 'Cannot specify both jobId and workerId' });
35
+ return;
36
+ }
37
+
38
+ if (jobId) {
39
+ const logs = LogRegistry.getLogsByJobId(jobId);
40
+ res.json(LogSerializer.serialize(logs.slice(0, this.#pageSize)));
41
+ return;
42
+ }
43
+
44
+ if (workerId) {
45
+ const logs = LogRegistry.getLogsByWorkerId(workerId);
46
+ res.json(LogSerializer.serialize(logs.slice(0, this.#pageSize)));
47
+ return;
48
+ }
49
+
50
+ const logs = LogRegistry.getLogs({ lastId });
51
+ res.json(LogSerializer.serialize(logs.slice(0, this.#pageSize)));
52
+ }
53
+ }
54
+
55
+ export { LogsRequestHandler };
@@ -0,0 +1,35 @@
1
+ import { ForbiddenError } from '../../exceptions/http/ForbiddenError.js';
2
+ import { RequestHandler } from '../RequestHandler.js';
3
+
4
+ /**
5
+ * Handles GET /settings.json requests.
6
+ * Returns application settings visible to the frontend.
7
+ * Throws ForbiddenError when shutdown is disabled.
8
+ * @author darthjee
9
+ */
10
+ class SettingsRequestHandler extends RequestHandler {
11
+ #enableShutdown;
12
+
13
+ /**
14
+ * Creates a new SettingsRequestHandler.
15
+ * @param {object} [options={}] - Constructor options.
16
+ * @param {boolean} [options.enableShutdown=true] - Whether shutdown is permitted.
17
+ */
18
+ constructor({ enableShutdown = true } = {}) {
19
+ super();
20
+ this.#enableShutdown = enableShutdown;
21
+ }
22
+
23
+ /**
24
+ * Responds with the application settings JSON.
25
+ * @param {object} _req - The Express request object.
26
+ * @param {object} res - The Express response object.
27
+ * @returns {void}
28
+ */
29
+ handle(_req, res) {
30
+ if (!this.#enableShutdown) throw new ForbiddenError();
31
+ res.json({ enable_shutdown: true });
32
+ }
33
+ }
34
+
35
+ export { SettingsRequestHandler };
@@ -1,6 +1,6 @@
1
- import { RequestHandler } from './RequestHandler.js';
2
- import { JobRegistry } from '../registry/JobRegistry.js';
3
- import { WorkersRegistry } from '../registry/WorkersRegistry.js';
1
+ import { JobRegistry } from '../../background/JobRegistry.js';
2
+ import { WorkersRegistry } from '../../background/WorkersRegistry.js';
3
+ import { RequestHandler } from '../RequestHandler.js';
4
4
 
5
5
  /**
6
6
  * Handles GET /stats.json requests.
@@ -0,0 +1,34 @@
1
+ import { ConflictError } from '../../../exceptions/http/ConflictError.js';
2
+ import { Application } from '../../../services/Application.js';
3
+ import { RequestHandler } from '../../RequestHandler.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 { ConflictError } from '../../../exceptions/http/ConflictError.js';
2
+ import { Application } from '../../../services/Application.js';
3
+ import { RequestHandler } from '../../RequestHandler.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 { ConflictError } from '../../../exceptions/http/ConflictError.js';
2
+ import { Application } from '../../../services/Application.js';
3
+ import { RequestHandler } from '../../RequestHandler.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 { Application } from '../../../services/Application.js';
2
+ import { RequestHandler } from '../../RequestHandler.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 { ConflictError } from '../../../exceptions/http/ConflictError.js';
2
+ import { Application } from '../../../services/Application.js';
3
+ import { RequestHandler } from '../../RequestHandler.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 { Application } from '../../../services/Application.js';
2
+ import { RequestHandler } from '../../RequestHandler.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 { ConflictError } from '../../../exceptions/http/ConflictError.js';
2
+ import { Application } from '../../../services/Application.js';
3
+ import { RequestHandler } from '../../RequestHandler.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 { LogRegistry } from '../../../registry/LogRegistry.js';
2
+ import { LogSerializer } from '../../../serializers/LogSerializer.js';
3
+ import { RequestHandler } from '../../RequestHandler.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={}] - Constructor 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 };
@@ -1,7 +1,7 @@
1
- import { RequestHandler } from './RequestHandler.js';
2
- import { NotFoundError } from '../exceptions/NotFoundError.js';
3
- import { JobRegistry } from '../registry/JobRegistry.js';
4
- import { JobSerializer } from '../serializers/JobSerializer.js';
1
+ import { JobRegistry } from '../../../background/JobRegistry.js';
2
+ import { NotFoundError } from '../../../exceptions/http/NotFoundError.js';
3
+ import { JobSerializer } from '../../../serializers/JobSerializer.js';
4
+ import { RequestHandler } from '../../RequestHandler.js';
5
5
 
6
6
  /**
7
7
  * Handles GET /job/:id.json requests.
@@ -0,0 +1,40 @@
1
+ import { JobRegistry } from '../../../background/JobRegistry.js';
2
+ import { ConflictError } from '../../../exceptions/http/ConflictError.js';
3
+ import { NotFoundError } from '../../../exceptions/http/NotFoundError.js';
4
+ import { RequestHandler } from '../../RequestHandler.js';
5
+
6
+ const RETRYABLE_STATUSES = new Set(['failed', 'dead']);
7
+
8
+ /**
9
+ * Handles PATCH /jobs/:id/retry requests.
10
+ * Moves a failed or dead job directly to the retry queue.
11
+ * Returns 404 if the job does not exist, 409 if it is not in a retryable state.
12
+ * @author darthjee
13
+ */
14
+ class JobRetryRequestHandler extends RequestHandler {
15
+ /**
16
+ * Creates a new JobRetryRequestHandler instance.
17
+ */
18
+ constructor() {
19
+ super();
20
+ }
21
+
22
+ /**
23
+ * Retries the specified job by moving it to the retry queue.
24
+ * @param {object} req - The Express request object.
25
+ * @param {object} res - The Express response object.
26
+ * @returns {void}
27
+ */
28
+ handle(req, res) {
29
+ const { id } = req.params;
30
+ const result = JobRegistry.jobById(id);
31
+
32
+ if (!result) throw new NotFoundError('Job not found');
33
+ if (!RETRYABLE_STATUSES.has(result.status)) throw new ConflictError();
34
+
35
+ JobRegistry.retryJob(id);
36
+ res.json({ status: 'enqueued' });
37
+ }
38
+ }
39
+
40
+ export { JobRetryRequestHandler };
@@ -1,6 +1,7 @@
1
- import { RequestHandler } from './RequestHandler.js';
2
- import { JobRegistry } from '../registry/JobRegistry.js';
3
- import { JobSerializer } from '../serializers/JobSerializer.js';
1
+ import { JobRegistry } from '../../../background/JobRegistry.js';
2
+ import { JobSerializer } from '../../../serializers/JobSerializer.js';
3
+ import { RequestHandler } from '../../RequestHandler.js';
4
+ import { JobsFilter } from '../JobsFilter.js';
4
5
 
5
6
  /**
6
7
  * Handles GET /jobs/:status.json requests.
@@ -23,7 +24,9 @@ class JobsRequestHandler extends RequestHandler {
23
24
  handle(req, res) {
24
25
  const { status } = req.params;
25
26
  const jobs = JobRegistry.jobsByStatus(status);
26
- res.json(JobSerializer.serialize(jobs, { status, view: 'index' }));
27
+ const filters = req.query.filters || {};
28
+ const filteredJobs = new JobsFilter(jobs, filters).filter();
29
+ res.json(JobSerializer.serialize(filteredJobs, { status, view: 'index' }));
27
30
  }
28
31
  }
29
32