navi-hey 1.6.1 → 1.6.4

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 (39) hide show
  1. package/lib/enqueuers/ActionEnqueuer.js +1 -1
  2. package/lib/enqueuers/PaginatedActionEnqueuer.js +1 -1
  3. package/lib/jobs/ActionProcessingJob.js +1 -1
  4. package/lib/jobs/AssetDownloadJob.js +1 -1
  5. package/lib/jobs/HtmlParseJob.js +1 -1
  6. package/lib/jobs/PaginatedActionProcessingJob.js +1 -1
  7. package/lib/jobs/ResourceRequestJob.js +1 -2
  8. package/lib/models/configs/PageRange.js +37 -0
  9. package/lib/models/configs/PaginationConfig.js +7 -11
  10. package/lib/models/request/ResourceRequest.js +31 -1
  11. package/lib/models/request/ResourceRequestAction.js +1 -1
  12. package/lib/models/request/ResourceRequestPaginatedAction.js +15 -11
  13. package/lib/server/handlers/StatsHandler.js +1 -2
  14. package/lib/server/handlers/jobs/JobHandler.js +1 -1
  15. package/lib/server/handlers/jobs/JobRetryHandler.js +1 -1
  16. package/lib/server/handlers/jobs/JobsHandler.js +1 -1
  17. package/lib/services/ApplicationInstance.js +9 -7
  18. package/lib/services/FailureChecker.js +1 -1
  19. package/lib/utils/ResourceEnqueuer.js +1 -1
  20. package/package.json +2 -1
  21. package/lib/background/Job.js +0 -97
  22. package/lib/background/JobFactory.js +0 -82
  23. package/lib/background/JobRegistry.js +0 -160
  24. package/lib/background/JobRegistryInstance.js +0 -247
  25. package/lib/background/Worker.js +0 -59
  26. package/lib/background/WorkerFactory.js +0 -35
  27. package/lib/background/WorkersRegistry.js +0 -106
  28. package/lib/background/WorkersRegistryInstance.js +0 -139
  29. package/lib/factory/Factory.js +0 -79
  30. package/lib/services/Engine.js +0 -153
  31. package/lib/services/WorkersAllocator.js +0 -74
  32. package/lib/utils/collections/Collection.js +0 -26
  33. package/lib/utils/collections/IdentifyableCollection.js +0 -95
  34. package/lib/utils/collections/Queue.js +0 -53
  35. package/lib/utils/collections/SortedArrayMerger.js +0 -84
  36. package/lib/utils/collections/SortedArraySearcher.js +0 -101
  37. package/lib/utils/collections/SortedCollection.js +0 -169
  38. package/lib/utils/generators/IdGenerator.js +0 -79
  39. package/lib/utils/generators/UUidGenerator.js +0 -49
@@ -1,4 +1,4 @@
1
- import { JobRegistry as DefaultJobRegistry } from '../background/JobRegistry.js';
1
+ import { JobRegistry as DefaultJobRegistry } from 'deku-swarm';
2
2
  import { Application } from '../services/Application.js';
3
3
 
4
4
  /**
@@ -1,4 +1,4 @@
1
- import { JobRegistry as DefaultJobRegistry } from '../background/JobRegistry.js';
1
+ import { JobRegistry as DefaultJobRegistry } from 'deku-swarm';
2
2
  import { Application } from '../services/Application.js';
3
3
 
4
4
  /**
@@ -1,4 +1,4 @@
1
- import { Job } from '../background/Job.js';
1
+ import { Job } from 'deku-swarm';
2
2
 
3
3
  /**
4
4
  * ActionProcessingJob is a Job that processes a single Action for a given item.
@@ -1,4 +1,4 @@
1
- import { Job } from '../background/Job.js';
1
+ import { Job } from 'deku-swarm';
2
2
 
3
3
  /**
4
4
  * AssetDownloadJob is a Job that fetches a single fully-resolved asset URL and validates
@@ -1,4 +1,4 @@
1
- import { Job } from '../background/Job.js';
1
+ import { Job } from 'deku-swarm';
2
2
  import { AssetRequestEnqueuer } from '../enqueuers/AssetRequestEnqueuer.js';
3
3
 
4
4
  /**
@@ -1,4 +1,4 @@
1
- import { Job } from '../background/Job.js';
1
+ import { Job } from 'deku-swarm';
2
2
 
3
3
  /**
4
4
  * Processes a single paginated action for a given response wrapper.
@@ -1,5 +1,4 @@
1
- import { Job } from '../background/Job.js';
2
- import { JobRegistry } from '../background/JobRegistry.js';
1
+ import { Job, JobRegistry } from 'deku-swarm';
3
2
  import { ResponseWrapper } from '../models/response/ResponseWrapper.js';
4
3
 
5
4
  /**
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Iterates over a range of page numbers, honoring zero-indexing and an optional cap.
3
+ * @author darthjee
4
+ */
5
+ class PageRange {
6
+ #count;
7
+ #zeroIndexed;
8
+ #maxPage;
9
+
10
+ /**
11
+ * @param {object} params Range parameters.
12
+ * @param {number} params.count Total number of pages available.
13
+ * @param {boolean} [params.zeroIndexed=false] Whether pages start at 0 instead of 1.
14
+ * @param {number|null} [params.maxPage=null] Caps iteration to the first `maxPage` pages. `null` means unlimited.
15
+ */
16
+ constructor({ count, zeroIndexed = false, maxPage = null }) {
17
+ this.#count = count;
18
+ this.#zeroIndexed = zeroIndexed;
19
+ this.#maxPage = maxPage;
20
+ }
21
+
22
+ /**
23
+ * Invokes callback once per page number, in order, honoring zeroIndexed and maxPage.
24
+ * @param {Function} callback Called with each page number.
25
+ * @returns {void}
26
+ */
27
+ each(callback) {
28
+ const start = this.#zeroIndexed ? 0 : 1;
29
+ const total = this.#maxPage !== null ? Math.min(this.#count, this.#maxPage) : this.#count;
30
+
31
+ for (let i = 0; i < total; i++) {
32
+ callback(start + i);
33
+ }
34
+ }
35
+ }
36
+
37
+ export { PageRange };
@@ -29,6 +29,13 @@ class PaginationConfig {
29
29
  return this.#pageKey;
30
30
  }
31
31
 
32
+ /**
33
+ * @returns {boolean} Whether pages are zero-indexed.
34
+ */
35
+ get zeroIndexed() {
36
+ return this.#zeroIndexed;
37
+ }
38
+
32
39
  /**
33
40
  * Evaluates the `pages` expression against the response wrapper.
34
41
  * @param {ResponseWrapper} responseWrapper The response wrapper to resolve against.
@@ -38,17 +45,6 @@ class PaginationConfig {
38
45
  return this.#pagesResolver.resolve(responseWrapper);
39
46
  }
40
47
 
41
- /**
42
- * Returns an array of page numbers to iterate over.
43
- * @param {number} count Total number of pages.
44
- * @returns {Array<number>} Array of page numbers.
45
- */
46
- pageNumbers(count) {
47
- const start = this.#zeroIndexed ? 0 : 1;
48
- const end = this.#zeroIndexed ? count - 1 : count;
49
- return Array.from({ length: end - start + 1 }, (_, i) => start + i);
50
- }
51
-
52
48
  /**
53
49
  * Creates a PaginationConfig from a raw YAML list entry.
54
50
  * @param {Array<object>} list List of single-key maps from YAML.
@@ -1,9 +1,10 @@
1
+ import { JobRegistry as DefaultJobRegistry } from 'deku-swarm';
1
2
  import { AssetRequest } from './AssetRequest.js';
2
3
  import { ResourceRequestAction } from './ResourceRequestAction.js';
3
4
  import { ResourceRequestPaginatedAction } from './ResourceRequestPaginatedAction.js';
4
- import { JobRegistry as DefaultJobRegistry } from '../../background/JobRegistry.js';
5
5
  import { ActionsEnqueuer } from '../../enqueuers/ActionsEnqueuer.js';
6
6
  import { PaginatedActionsEnqueuer } from '../../enqueuers/PaginatedActionsEnqueuer.js';
7
+ import { LogRegistry } from '../../registry/LogRegistry.js';
7
8
  import { Application } from '../../services/Application.js';
8
9
 
9
10
  /**
@@ -15,6 +16,7 @@ class ResourceRequest {
15
16
  #clientNamespace;
16
17
  #namespace;
17
18
  #disabled;
19
+ #maxPage;
18
20
 
19
21
  /**
20
22
  * @param {object} attributes ResourceRequest attributes
@@ -30,6 +32,9 @@ class ResourceRequest {
30
32
  * when omitted. Ignored when `disabled` is `true`.
31
33
  * @param {boolean} [attributes.disabled] Whether this request is disabled. Takes precedence
32
34
  * over `enabled` when `true`.
35
+ * @param {number} [attributes.max_page] Caps how many pages of this resource are ever enqueued
36
+ * when it's the target of another resource's `paginated_actions`. Must be a positive integer;
37
+ * any other value (including omitted, `null`, or `0`) means unlimited.
33
38
  */
34
39
  constructor({
35
40
  url,
@@ -41,11 +46,13 @@ class ResourceRequest {
41
46
  paginated_actions = [],
42
47
  enabled,
43
48
  disabled,
49
+ max_page,
44
50
  }) {
45
51
  this.url = url;
46
52
  this.status = status;
47
53
  this.#namespace = namespace;
48
54
  this.#disabled = disabled === true || enabled === false;
55
+ this.#maxPage = this.#sanitizeMaxPage(max_page);
49
56
 
50
57
  const parsedClient = ResourceRequest.#parseClient(clientName);
51
58
  this.#clientName = parsedClient.name;
@@ -93,6 +100,15 @@ class ResourceRequest {
93
100
  return this.#disabled;
94
101
  }
95
102
 
103
+ /**
104
+ * Returns the cap on how many pages of this resource are ever enqueued when it's the
105
+ * target of another resource's `paginated_actions`. `null` means unlimited.
106
+ * @returns {number|null} The positive integer cap, or null when unlimited.
107
+ */
108
+ get maxPage() {
109
+ return this.#maxPage;
110
+ }
111
+
96
112
  /**
97
113
  * Enqueues one ActionProcessingJob per action associated with the resource request.
98
114
  * Returns immediately if there are no actions.
@@ -191,6 +207,20 @@ class ResourceRequest {
191
207
  }
192
208
  return { name: client, namespace: null };
193
209
  }
210
+
211
+ /**
212
+ * Sanitizes the raw `max_page` value into a positive integer or `null`.
213
+ * Logs a warning when a present value is invalid.
214
+ * @param {*} value The raw `max_page` value.
215
+ * @returns {number|null} The sanitized positive integer, or null when unlimited.
216
+ */
217
+ #sanitizeMaxPage(value) {
218
+ if (value === undefined || value === null || value === 0) return null;
219
+ if (Number.isInteger(value) && value > 0) return value;
220
+
221
+ LogRegistry.warn(`Ignoring invalid max_page value: ${JSON.stringify(value)} — treating as unlimited`);
222
+ return null;
223
+ }
194
224
  }
195
225
 
196
226
  export { ResourceRequest };
@@ -1,4 +1,4 @@
1
- import { JobRegistry as DefaultJobRegistry } from '../../background/JobRegistry.js';
1
+ import { JobRegistry as DefaultJobRegistry } from 'deku-swarm';
2
2
  import { MissingActionResource } from '../../exceptions/registry/MissingActionResource.js';
3
3
  import { LogRegistry } from '../../registry/LogRegistry.js';
4
4
  import { NamespaceMap as DefaultNamespaceMap } from '../../registry/NamespaceMap.js';
@@ -1,8 +1,9 @@
1
- import { JobRegistry as DefaultJobRegistry } from '../../background/JobRegistry.js';
1
+ import { JobRegistry as DefaultJobRegistry } from 'deku-swarm';
2
2
  import { MissingActionResource } from '../../exceptions/registry/MissingActionResource.js';
3
3
  import { LogRegistry } from '../../registry/LogRegistry.js';
4
4
  import { NamespaceMap as DefaultNamespaceMap } from '../../registry/NamespaceMap.js';
5
5
  import { Application } from '../../services/Application.js';
6
+ import { PageRange } from '../configs/PageRange.js';
6
7
  import { PaginationConfig } from '../configs/PaginationConfig.js';
7
8
  import { ParametersMapper } from '../response/ParametersMapper.js';
8
9
 
@@ -92,21 +93,24 @@ class ResourceRequestPaginatedAction {
92
93
  if (Application.isStopped()) return;
93
94
 
94
95
  const count = this.#pagination.resolvePages(responseWrapper);
95
- const pages = this.#pagination.pageNumbers(count);
96
96
  const resource = this.#namespaceMap.getResource(this.#originNamespace, this.#resource, this.#namespace);
97
97
  const mappedParameters = this.#mapper.map(responseWrapper);
98
98
 
99
- for (const page of pages) {
100
- const pageParameters = {
101
- ...parameters,
102
- ...mappedParameters,
103
- [this.#pagination.pageKey]: page,
104
- };
105
- for (const resourceRequest of resource.resourceRequests) {
106
- if (resourceRequest.disabled) continue;
99
+ for (const resourceRequest of resource.resourceRequests) {
100
+ if (resourceRequest.disabled) continue;
107
101
 
102
+ new PageRange({
103
+ count,
104
+ zeroIndexed: this.#pagination.zeroIndexed,
105
+ maxPage: resourceRequest.maxPage,
106
+ }).each((page) => {
107
+ const pageParameters = {
108
+ ...parameters,
109
+ ...mappedParameters,
110
+ [this.#pagination.pageKey]: page,
111
+ };
108
112
  this.#jobRegistry.enqueue('ResourceRequestJob', { resourceRequest, parameters: pageParameters });
109
- }
113
+ });
110
114
  }
111
115
  }
112
116
 
@@ -1,5 +1,4 @@
1
- import { JobRegistry } from '../../background/JobRegistry.js';
2
- import { WorkersRegistry } from '../../background/WorkersRegistry.js';
1
+ import { JobRegistry, WorkersRegistry } from 'deku-swarm';
3
2
  import { RequestHandler } from '../../common/server/RequestHandler.js';
4
3
 
5
4
  /**
@@ -1,4 +1,4 @@
1
- import { JobRegistry } from '../../../background/JobRegistry.js';
1
+ import { JobRegistry } from 'deku-swarm';
2
2
  import { RequestHandler } from '../../../common/server/RequestHandler.js';
3
3
  import { NotFoundError } from '../../../exceptions/http/NotFoundError.js';
4
4
  import { JobSerializer } from '../../../serializers/JobSerializer.js';
@@ -1,4 +1,4 @@
1
- import { JobRegistry } from '../../../background/JobRegistry.js';
1
+ import { JobRegistry } from 'deku-swarm';
2
2
  import { RequestHandler } from '../../../common/server/RequestHandler.js';
3
3
  import { ConflictError } from '../../../exceptions/http/ConflictError.js';
4
4
  import { NotFoundError } from '../../../exceptions/http/NotFoundError.js';
@@ -1,4 +1,4 @@
1
- import { JobRegistry } from '../../../background/JobRegistry.js';
1
+ import { JobRegistry } from 'deku-swarm';
2
2
  import { RequestHandler } from '../../../common/server/RequestHandler.js';
3
3
  import { JobSerializer } from '../../../serializers/JobSerializer.js';
4
4
  import { JobsFilter } from '../JobsFilter.js';
@@ -1,20 +1,19 @@
1
+ import { Engine, JobFactory, JobRegistry, WorkerFactory, WorkersRegistry } from 'deku-swarm';
1
2
  import { ConfigIncluder } from './ConfigIncluder.js';
2
- import { Engine } from './Engine.js';
3
3
  import { EngineEvents } from './EngineEvents.js';
4
4
  import { FailureChecker } from './FailureChecker.js';
5
5
  import { RunSummary } from './RunSummary.js';
6
- import { JobFactory } from '../background/JobFactory.js';
7
- import { JobRegistry } from '../background/JobRegistry.js';
8
- import { WorkersRegistry } from '../background/WorkersRegistry.js';
9
6
  import { ConfigurationFileNotProvided } from '../exceptions/config/ConfigurationFileNotProvided.js';
10
7
  import { ActionProcessingJob } from '../jobs/ActionProcessingJob.js';
11
8
  import { AssetDownloadJob } from '../jobs/AssetDownloadJob.js';
12
9
  import { HtmlParseJob } from '../jobs/HtmlParseJob.js';
13
10
  import { PaginatedActionProcessingJob } from '../jobs/PaginatedActionProcessingJob.js';
11
+ import { ResourceRequestJob } from '../jobs/ResourceRequestJob.js';
14
12
  import { Config } from '../models/configs/Config.js';
15
13
  import { LogRegistry } from '../registry/LogRegistry.js';
16
14
  import { NamespaceMap } from '../registry/NamespaceMap.js';
17
15
  import { WebServer } from '../server/WebServer.js';
16
+ import { LogContext } from '../utils/logging/LogContext.js';
18
17
  import { PromiseAggregator } from '../utils/PromiseAggregator.js';
19
18
  import { ResourceEnqueuer } from '../utils/ResourceEnqueuer.js';
20
19
 
@@ -95,6 +94,8 @@ class ApplicationInstance {
95
94
  */
96
95
  buildEngine() {
97
96
  return new Engine({
97
+ jobRegistry: JobRegistry,
98
+ workersRegistry: WorkersRegistry,
98
99
  sleepMs: this.#sleepMs ?? this.config.workersConfig.sleep,
99
100
  keepAlive: !!this.config.webConfig,
100
101
  idleTimeoutMs: (this.config.webConfig?.idleTimeout ?? 0) * 1000,
@@ -318,7 +319,7 @@ class ApplicationInstance {
318
319
  * @returns {void}
319
320
  */
320
321
  #initRegistries() {
321
- JobFactory.build('ResourceRequestJob', { attributes: { clients: this.config.namespaceMap } });
322
+ JobFactory.build('ResourceRequestJob', { klass: ResourceRequestJob, attributes: { clients: this.config.namespaceMap } });
322
323
  JobFactory.build('Action', { klass: ActionProcessingJob });
323
324
  JobFactory.build('PaginatedAction', { klass: PaginatedActionProcessingJob });
324
325
  JobFactory.build('HtmlParse', { klass: HtmlParseJob, attributes: { jobRegistry: JobRegistry, clientRegistry: this.config.namespaceMap } });
@@ -326,10 +327,11 @@ class ApplicationInstance {
326
327
 
327
328
  JobRegistry.build({ cooldown: this.config.workersConfig.retryCooldown, maxRetries: this.config.workersConfig.maxRetries });
328
329
 
330
+ const loggerFactory = ({ workerId, jobId }) => new LogContext({ workerId, jobId });
331
+
329
332
  WorkersRegistry.build({
330
333
  workers: this.#workers,
331
- jobRegistry: JobRegistry,
332
- workersRegistry: WorkersRegistry,
334
+ factory: new WorkerFactory({ jobRegistry: JobRegistry, workersRegistry: WorkersRegistry, loggerFactory }),
333
335
  ...this.config.workersConfig,
334
336
  });
335
337
  WorkersRegistry.initWorkers();
@@ -1,4 +1,4 @@
1
- import { JobRegistry } from '../background/JobRegistry.js';
1
+ import { JobRegistry } from 'deku-swarm';
2
2
  import { LogRegistry } from '../registry/LogRegistry.js';
3
3
 
4
4
  /**
@@ -1,5 +1,5 @@
1
+ import { JobRegistry } from 'deku-swarm';
1
2
  import { ResourceRequestCollector } from './ResourceRequestCollector.js';
2
- import { JobRegistry } from '../background/JobRegistry.js';
3
3
  import { NamespaceNotFound } from '../exceptions/registry/NamespaceNotFound.js';
4
4
  import { ResourceNotFound } from '../exceptions/registry/ResourceNotFound.js';
5
5
  import { NamespaceMap } from '../registry/NamespaceMap.js';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "navi-hey",
3
3
  "type": "module",
4
- "version": "1.6.1",
4
+ "version": "1.6.4",
5
5
  "description": "Navi cache warmer CLI",
6
6
  "readme": "README.md",
7
7
  "main": "index.js",
@@ -28,6 +28,7 @@
28
28
  "license": "MIT",
29
29
  "dependencies": {
30
30
  "axios": "^1.13.0",
31
+ "deku-swarm": "^worker",
31
32
  "express": "5.2.1",
32
33
  "node-html-parser": "7.1.0",
33
34
  "yaml": "^2.8.2"
@@ -1,97 +0,0 @@
1
- /**
2
- * Job is the abstract base class for all units of work processed by a Worker.
3
- * Subclasses must implement the `perform` method.
4
- * @author darthjee
5
- */
6
- class Job {
7
- #attempts;
8
- #readyBy;
9
-
10
- /**
11
- * Creates a new Job instance.
12
- * @param {object} params - The parameters for creating a Job instance.
13
- * @param {string} params.id - The unique identifier for this job.
14
- */
15
- constructor({ id }) {
16
- this.id = id;
17
- this.#attempts = 0;
18
- this.#readyBy = 0;
19
- }
20
-
21
- /**
22
- * Performs the job. Must be overridden by subclasses.
23
- * @throws {Error} Always throws — subclasses must implement this method.
24
- * @returns {Promise} A promise that resolves when the job is complete.
25
- */
26
- async perform() {
27
- throw new Error('You must implement the perform method in a subclass');
28
- }
29
-
30
- /**
31
- * Returns the timestamp after which the job is eligible for retry.
32
- * @returns {number} The readyBy timestamp in milliseconds.
33
- */
34
- get readyBy() {
35
- return this.#readyBy;
36
- }
37
-
38
- /**
39
- * Sets the cooldown duration in milliseconds after which the job is eligible for retry.
40
- * Stores the absolute timestamp (Date.now() + ms) internally.
41
- * @param {number} ms - Cooldown duration in milliseconds. Use a negative value to mark ready immediately.
42
- * @returns {void}
43
- */
44
- applyCooldown(ms) {
45
- this.#readyBy = Date.now() + ms;
46
- }
47
-
48
- /**
49
- * Checks whether the job's cooldown period has elapsed relative to the given time.
50
- * @param {number} currentTime - The current timestamp in milliseconds.
51
- * @returns {boolean} True if the job can be retried at the given time.
52
- */
53
- isReadyBy(currentTime) {
54
- return currentTime >= this.#readyBy;
55
- }
56
-
57
- /**
58
- * Returns the maximum number of retries allowed for this job.
59
- * Subclasses may override this to return a different value.
60
- * @returns {number} The maximum number of retries.
61
- */
62
- get maxRetries() {
63
- return 3;
64
- }
65
-
66
- /**
67
- * Checks if the job has been exhausted (i.e., has reached the maximum number of attempts).
68
- * @param {number} [maxRetries] - Maximum number of retries before the job is considered dead.
69
- * @returns {boolean} True if the job is exhausted, false otherwise.
70
- */
71
- exhausted(maxRetries = this.maxRetries) {
72
- return this.#attempts >= maxRetries;
73
- }
74
-
75
- /**
76
- * Returns the number of failed attempts so far.
77
- * @returns {number} The current attempt count.
78
- * @protected
79
- */
80
- get _attempts() {
81
- return this.#attempts;
82
- }
83
-
84
- /**
85
- * Handles a failed job attempt.
86
- * @param {Error} error - The error that caused the job to fail.
87
- * @throws {Error} Re-throws the error after recording the failure.
88
- * @protected
89
- */
90
- _fail(error) {
91
- this.#attempts += 1;
92
- this.lastError = error;
93
- throw error;
94
- }
95
- }
96
-
97
- export { Job };
@@ -1,82 +0,0 @@
1
- import { Factory } from '../factory/Factory.js';
2
- import { ResourceRequestJob } from '../jobs/ResourceRequestJob.js';
3
- import { IdGenerator } from '../utils/generators/IdGenerator.js';
4
-
5
- /**
6
- * JobFactory is responsible for creating Job instances with unique identifiers.
7
- * Constructor-time attributes are merged with build-time params on every build call.
8
- * @author darthjee
9
- */
10
- class JobFactory extends Factory {
11
- static #factories = new Map();
12
-
13
- #attributes;
14
-
15
- /**
16
- * Creates a new JobFactory instance.
17
- * @param {object} options - Configuration options for the factory.
18
- * @param {class} options.klass - The class to instantiate (default is ResourceRequestJob).
19
- * @param {object} options.attributesGenerator - The generator for unique attributes (default is IdGenerator).
20
- * @param {object} options.attributes - Attributes injected into every built instance (e.g. { clients }).
21
- */
22
- constructor({ klass = ResourceRequestJob, attributesGenerator = new IdGenerator(), attributes = {} } = {}) {
23
- super({ klass, attributesGenerator });
24
- this.#attributes = attributes;
25
- }
26
-
27
- /**
28
- * Builds a new Job instance, merging constructor-level attributes with the given params.
29
- * @param {object} params - The parameters for building a Job instance.
30
- * @param {ResourceRequest} params.resourceRequest - The resource request associated with the Job.
31
- * @param {object} params.parameters - Additional parameters for the Job.
32
- * @returns {Job} A new Job instance.
33
- * @override
34
- */
35
- build(params) {
36
- return super.build({ ...this.#attributes, ...params });
37
- }
38
-
39
- /**
40
- * Creates and registers a new JobFactory instance under the given name.
41
- * @param {string} name - The name to register the factory under.
42
- * @param {object} attributes - Options forwarded to the JobFactory constructor.
43
- * @param {class} [attributes.klass] - The class to instantiate (default is ResourceRequestJob).
44
- * @param {object} [attributes.attributesGenerator] - Generator for unique attributes (default is IdGenerator).
45
- * @param {object} [attributes.attributes] - Attributes injected into every built instance (e.g. { clients }).
46
- * @returns {JobFactory} The created and registered factory instance.
47
- */
48
- static build(name, attributes) {
49
- const factory = new JobFactory(attributes);
50
- JobFactory.registry(name, factory);
51
- return factory;
52
- }
53
-
54
- /**
55
- * Registers a factory instance under the given name.
56
- * @param {string} name - The name to register the factory under.
57
- * @param {JobFactory} factory - The factory instance to register.
58
- * @returns {void}
59
- */
60
- static registry(name, factory) {
61
- JobFactory.#factories.set(name, factory);
62
- }
63
-
64
- /**
65
- * Retrieves a registered factory by name.
66
- * @param {string} name - The name of the factory to retrieve.
67
- * @returns {JobFactory|undefined} The registered factory, or undefined if not found.
68
- */
69
- static get(name) {
70
- return JobFactory.#factories.get(name);
71
- }
72
-
73
- /**
74
- * Removes all registered factories. Useful for test isolation.
75
- * @returns {void}
76
- */
77
- static reset() {
78
- JobFactory.#factories.clear();
79
- }
80
- }
81
-
82
- export { JobFactory };