navi-hey 0.4.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/bin/navi.js +9 -0
- package/lib/exceptions/AppError.js +13 -0
- package/lib/exceptions/ClientNotFound.js +16 -0
- package/lib/exceptions/ConfigurationFileNotFound.js +10 -0
- package/lib/exceptions/ConfigurationFileNotProvided.js +10 -0
- package/lib/exceptions/InvalidResponseBody.js +19 -0
- package/lib/exceptions/ItemNotFound.js +16 -0
- package/lib/exceptions/LockedByOtherWorker.js +14 -0
- package/lib/exceptions/MissingActionResource.js +13 -0
- package/lib/exceptions/MissingClientsConfig.js +14 -0
- package/lib/exceptions/MissingMappingVariable.js +17 -0
- package/lib/exceptions/MissingResourceConfig.js +14 -0
- package/lib/exceptions/MissingTopLevelConfgKey.js +15 -0
- package/lib/exceptions/NullResponse.js +13 -0
- package/lib/exceptions/RequestFailed.js +16 -0
- package/lib/exceptions/ResourceNotFound.js +16 -0
- package/lib/factories/Factory.js +79 -0
- package/lib/factories/JobFactory.js +82 -0
- package/lib/factories/WorkerFactory.js +35 -0
- package/lib/models/ActionEnqueuer.js +34 -0
- package/lib/models/ActionProcessingJob.js +51 -0
- package/lib/models/ActionsEnqueuer.js +42 -0
- package/lib/models/Config.js +68 -0
- package/lib/models/Job.js +87 -0
- package/lib/models/ParametersMapper.js +43 -0
- package/lib/models/PathResolver.js +77 -0
- package/lib/models/PathSegmentTraverser.js +82 -0
- package/lib/models/Resource.js +47 -0
- package/lib/models/ResourceRequest.js +80 -0
- package/lib/models/ResourceRequestAction.js +73 -0
- package/lib/models/ResourceRequestJob.js +61 -0
- package/lib/models/ResponseParser.js +36 -0
- package/lib/models/ResponseWrapper.js +86 -0
- package/lib/models/WebConfig.js +15 -0
- package/lib/models/Worker.js +57 -0
- package/lib/models/WorkersConfig.js +18 -0
- package/lib/registry/ClientRegistry.js +92 -0
- package/lib/registry/JobRegistry.js +125 -0
- package/lib/registry/JobRegistryInstance.js +149 -0
- package/lib/registry/NamedRegistry.js +73 -0
- package/lib/registry/ResourceRegistry.js +91 -0
- package/lib/registry/WorkersRegistry.js +106 -0
- package/lib/registry/WorkersRegistryInstance.js +139 -0
- package/lib/server/RequestHandler.js +15 -0
- package/lib/server/RouteRegister.js +27 -0
- package/lib/server/Router.js +44 -0
- package/lib/server/StatsRequestHandler.js +31 -0
- package/lib/server/WebServer.js +45 -0
- package/lib/services/Application.js +113 -0
- package/lib/services/ArgumentsParser.js +36 -0
- package/lib/services/Client.js +108 -0
- package/lib/services/ConfigLoader.js +75 -0
- package/lib/services/ConfigParser.js +131 -0
- package/lib/services/Engine.js +67 -0
- package/lib/services/WorkersAllocator.js +74 -0
- package/lib/utils/EnvResolver.js +53 -0
- package/lib/utils/PromiseAggregator.js +37 -0
- package/lib/utils/ResourceRequestCollector.js +35 -0
- package/lib/utils/collections/Collection.js +17 -0
- package/lib/utils/collections/IdentifyableCollection.js +86 -0
- package/lib/utils/collections/Queue.js +45 -0
- package/lib/utils/collections/SortedArrayMerger.js +84 -0
- package/lib/utils/collections/SortedArraySearcher.js +101 -0
- package/lib/utils/collections/SortedCollection.js +169 -0
- package/lib/utils/generators/IdGenerator.js +79 -0
- package/lib/utils/generators/IncrementalIdGenerator.js +25 -0
- package/lib/utils/generators/UUidGenerator.js +49 -0
- package/lib/utils/index.js +2 -0
- package/lib/utils/logging/BaseLogger.js +93 -0
- package/lib/utils/logging/BufferedLogger.js +91 -0
- package/lib/utils/logging/ConsoleLogger.js +19 -0
- package/lib/utils/logging/Log.js +70 -0
- package/lib/utils/logging/LogBuffer.js +101 -0
- package/lib/utils/logging/LogFactory.js +32 -0
- package/lib/utils/logging/Logger.js +134 -0
- package/lib/utils/logging/LoggerGroup.js +100 -0
- package/package.json +78 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { Logger } from '../utils/logging/Logger.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Worker processes jobs pulled from a JobRegistry.
|
|
5
|
+
* @author darthjee
|
|
6
|
+
*/
|
|
7
|
+
class Worker {
|
|
8
|
+
#jobRegistry;
|
|
9
|
+
#workersRegistry;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new Worker instance.
|
|
13
|
+
* @param {object} params - The parameters for creating a Worker instance.
|
|
14
|
+
* @param {string|number} params.id - The unique identifier for this worker.
|
|
15
|
+
* @param {JobRegistry} params.jobRegistry - The job registry to report job outcomes to.
|
|
16
|
+
* @param {WorkersRegistry} params.workersRegistry - The workers registry to report idle status to.
|
|
17
|
+
*/
|
|
18
|
+
constructor({ id, jobRegistry, workersRegistry }) {
|
|
19
|
+
this.id = id;
|
|
20
|
+
this.#jobRegistry = jobRegistry;
|
|
21
|
+
this.#workersRegistry = workersRegistry;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Assigns a job to this worker.
|
|
26
|
+
* @param {Job} job - The job to assign.
|
|
27
|
+
* @returns {void}
|
|
28
|
+
*/
|
|
29
|
+
assign(job) {
|
|
30
|
+
this.job = job;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Performs the assigned job. If no job is assigned, an error is thrown.
|
|
35
|
+
* Any errors that occur during job execution are caught and logged.
|
|
36
|
+
* @returns {Promise<void>}
|
|
37
|
+
* @see Job.perform() for the actual job execution logic.
|
|
38
|
+
*/
|
|
39
|
+
async perform() {
|
|
40
|
+
if (!this.job) {
|
|
41
|
+
throw new Error('No job assigned to worker');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
await this.job.perform();
|
|
46
|
+
this.#jobRegistry.finish(this.job);
|
|
47
|
+
} catch (error) {
|
|
48
|
+
Logger.error(`Error occurred while performing job: #${this.job.id} - ${error}`);
|
|
49
|
+
this.#jobRegistry.fail(this.job);
|
|
50
|
+
} finally {
|
|
51
|
+
this.job = undefined;
|
|
52
|
+
this.#workersRegistry.setIdle(this.id);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export { Worker };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents the configuration for workers in the application.
|
|
3
|
+
* @author darthjee
|
|
4
|
+
*/
|
|
5
|
+
class WorkersConfig {
|
|
6
|
+
/**
|
|
7
|
+
* Creates an instance of WorkersConfig.
|
|
8
|
+
* @param {object} [config={}] - The configuration object.
|
|
9
|
+
* @param {number} [config.quantity=1] - The number of worker threads.
|
|
10
|
+
* @param {number} [config.retry_cooldown=2000] - The cooldown after a job has failed before retry.
|
|
11
|
+
*/
|
|
12
|
+
constructor({ quantity = 1, retry_cooldown: retryCooldown = 2000 } = {}) {
|
|
13
|
+
this.quantity = quantity;
|
|
14
|
+
this.retryCooldown = retryCooldown;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { WorkersConfig };
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { NamedRegistry } from './NamedRegistry.js';
|
|
2
|
+
import { ClientNotFound } from '../exceptions/ClientNotFound.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* ClientRegistry is a helper class that manages client retrieval logic for Config.
|
|
6
|
+
* It encapsulates the logic for fetching clients by name and determining the default client.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const clientRegistry = new ClientRegistry({
|
|
10
|
+
* default: new Client({ name: 'default', baseUrl: 'https://example.com' }),
|
|
11
|
+
* other: new Client({ name: 'other', baseUrl: 'https://other.com' }),
|
|
12
|
+
* });
|
|
13
|
+
*
|
|
14
|
+
* clientRegistry.getClient('default'); // returns the default client
|
|
15
|
+
* clientRegistry.getClient('other'); // returns the other client
|
|
16
|
+
* clientRegistry.getClient(); // returns the default client
|
|
17
|
+
* clientRegistry.getClient('nonexistent'); // throws ClientNotFound error
|
|
18
|
+
*
|
|
19
|
+
*
|
|
20
|
+
* @example only one client registered
|
|
21
|
+
* const clientRegistry = new ClientRegistry({
|
|
22
|
+
* only: new Client({ name: 'only', baseUrl: 'https://only.com' }),
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* clientRegistry.getClient(); // returns the only client as default
|
|
26
|
+
* clientRegistry.getClient('only'); // returns the only client
|
|
27
|
+
* clientRegistry.getClient('default'); // returns the only client as default
|
|
28
|
+
*
|
|
29
|
+
* @author darthjee
|
|
30
|
+
*/
|
|
31
|
+
class ClientRegistry extends NamedRegistry {
|
|
32
|
+
/**
|
|
33
|
+
* The exception class to throw when a client is not found.
|
|
34
|
+
* @see ClientNotFound
|
|
35
|
+
* @type {class}
|
|
36
|
+
* @see NamedRegistry#notFound
|
|
37
|
+
* @see NamedRegistry#notFoundException
|
|
38
|
+
*/
|
|
39
|
+
static notFoundException = ClientNotFound;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Retrieves a client by name or the default client if no name is provided.
|
|
43
|
+
*
|
|
44
|
+
* When `name` is `"default"` or not provided, the method first tries to
|
|
45
|
+
* return the client registered under the `"default"` key. If no such
|
|
46
|
+
* client exists but exactly one client is registered (under any name),
|
|
47
|
+
* that single client is returned instead.
|
|
48
|
+
*
|
|
49
|
+
* @param {string} [name] The name of the client to retrieve.
|
|
50
|
+
* @returns {Client} The requested client instance.
|
|
51
|
+
* @throws {ClientNotFound} If the client with the specified name does not exist.
|
|
52
|
+
*/
|
|
53
|
+
getClient(name) {
|
|
54
|
+
if (name && name !== 'default') {
|
|
55
|
+
return this.getItem(name);
|
|
56
|
+
}
|
|
57
|
+
return this.#getDefaultClient();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Retrieves the default client.
|
|
62
|
+
*
|
|
63
|
+
* The default client is determined as follows:
|
|
64
|
+
* 1. If a client is registered under the "default" key, that client is returned.
|
|
65
|
+
* 2. If no client is registered under "default" but exactly one client is registered, that single client is returned.
|
|
66
|
+
* 3. If neither of the above conditions are met, a ClientNotFound error is thrown for "default".
|
|
67
|
+
*
|
|
68
|
+
* @returns {Client} The default client instance.
|
|
69
|
+
* @throws {ClientNotFound} If no default client exists.
|
|
70
|
+
*/
|
|
71
|
+
#getDefaultClient() {
|
|
72
|
+
return this.#fetchDefaultClient() || this.getItem('default');
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Fetches the default client.
|
|
77
|
+
*
|
|
78
|
+
* This method first checks if there is exactly one client registered and returns it as the default.
|
|
79
|
+
* If there are multiple clients, it does not assume a default and returns undefined.
|
|
80
|
+
*
|
|
81
|
+
* @returns {Client|undefined} The default client instance if found, otherwise undefined.
|
|
82
|
+
*/
|
|
83
|
+
#fetchDefaultClient() {
|
|
84
|
+
const clientValues = Object.values(this.items);
|
|
85
|
+
|
|
86
|
+
if (clientValues.length === 1) {
|
|
87
|
+
return clientValues[0];
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export { ClientRegistry };
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { JobRegistryInstance } from './JobRegistryInstance.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* JobRegistry is a static singleton facade for managing the application's job queues.
|
|
5
|
+
*
|
|
6
|
+
* Call `JobRegistry.build(options)` once during application bootstrap.
|
|
7
|
+
* Use `JobRegistry.reset()` in tests to restore a clean state between examples.
|
|
8
|
+
* @author darthjee
|
|
9
|
+
*/
|
|
10
|
+
class JobRegistry {
|
|
11
|
+
static #instance = null;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Creates and stores the singleton instance.
|
|
15
|
+
* @param {object} [options={}] - Forwarded to `JobRegistryInstance` constructor.
|
|
16
|
+
* @returns {JobRegistryInstance} The created instance.
|
|
17
|
+
* @throws {Error} If `build()` has already been called without a preceding `reset()`.
|
|
18
|
+
*/
|
|
19
|
+
static build(options = {}) {
|
|
20
|
+
if (JobRegistry.#instance) {
|
|
21
|
+
throw new Error('JobRegistry.build() has already been called. Call reset() first.');
|
|
22
|
+
}
|
|
23
|
+
JobRegistry.#instance = new JobRegistryInstance(options);
|
|
24
|
+
return JobRegistry.#instance;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Destroys the singleton instance. Intended for test teardown.
|
|
29
|
+
* @returns {void}
|
|
30
|
+
*/
|
|
31
|
+
static reset() {
|
|
32
|
+
JobRegistry.#instance = null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Enqueues a new job using the factory registered under the given key.
|
|
37
|
+
* @param {string} factoryKey - The factory key to use.
|
|
38
|
+
* @param {object} [params={}] - Build params forwarded to the factory.
|
|
39
|
+
* @returns {Job} The created and enqueued Job instance.
|
|
40
|
+
*/
|
|
41
|
+
static enqueue(factoryKey, params = {}) {
|
|
42
|
+
return JobRegistry.#getInstance().enqueue(factoryKey, params);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Marks a job as failed.
|
|
47
|
+
* @param {Job} job - The job to mark as failed.
|
|
48
|
+
* @returns {void}
|
|
49
|
+
*/
|
|
50
|
+
static fail(job) {
|
|
51
|
+
return JobRegistry.#getInstance().fail(job);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Marks a job as finished.
|
|
56
|
+
* @param {Job} job - The job to mark as finished.
|
|
57
|
+
* @returns {void}
|
|
58
|
+
*/
|
|
59
|
+
static finish(job) {
|
|
60
|
+
return JobRegistry.#getInstance().finish(job);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Picks the next ready job and moves it to processing.
|
|
65
|
+
* @returns {Job|undefined} The next ready job, or undefined if none are ready.
|
|
66
|
+
*/
|
|
67
|
+
static pick() {
|
|
68
|
+
return JobRegistry.#getInstance().pick();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Moves a job from processing back to the enqueued queue.
|
|
73
|
+
* @param {Job} job - The job to re-enqueue.
|
|
74
|
+
* @returns {void}
|
|
75
|
+
*/
|
|
76
|
+
static requeue(job) {
|
|
77
|
+
return JobRegistry.#getInstance().requeue(job);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Promotes cooling-down failed jobs that are ready to retry.
|
|
82
|
+
* @returns {void}
|
|
83
|
+
*/
|
|
84
|
+
static promoteReadyJobs() {
|
|
85
|
+
return JobRegistry.#getInstance().promoteReadyJobs();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Returns whether any jobs exist (including those in cooldown).
|
|
90
|
+
* @returns {boolean} True if any jobs exist.
|
|
91
|
+
*/
|
|
92
|
+
static hasJob() {
|
|
93
|
+
return JobRegistry.#getInstance().hasJob();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Returns whether any jobs are immediately ready to be picked.
|
|
98
|
+
* @returns {boolean} True if any jobs are ready.
|
|
99
|
+
*/
|
|
100
|
+
static hasReadyJob() {
|
|
101
|
+
return JobRegistry.#getInstance().hasReadyJob();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Returns counts of jobs in each state.
|
|
106
|
+
* @returns {{ enqueued: number, processing: number, failed: number, retryQueue: number, finished: number, dead: number }} Job counts per state.
|
|
107
|
+
*/
|
|
108
|
+
static stats() {
|
|
109
|
+
return JobRegistry.#getInstance().stats();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Returns the singleton instance, throwing if not yet built.
|
|
114
|
+
* @returns {JobRegistryInstance} The singleton instance.
|
|
115
|
+
* @throws {Error} If `build()` has not been called.
|
|
116
|
+
*/
|
|
117
|
+
static #getInstance() {
|
|
118
|
+
if (!JobRegistry.#instance) {
|
|
119
|
+
throw new Error('JobRegistry has not been built. Call JobRegistry.build() first.');
|
|
120
|
+
}
|
|
121
|
+
return JobRegistry.#instance;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export { JobRegistry };
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { JobFactory } from '../factories/JobFactory.js';
|
|
2
|
+
import { IdentifyableCollection } from '../utils/collections/IdentifyableCollection.js';
|
|
3
|
+
import { Queue } from '../utils/collections/Queue.js';
|
|
4
|
+
import { SortedCollection } from '../utils/collections/SortedCollection.js';
|
|
5
|
+
|
|
6
|
+
const FAILED_SORT_BY = job => job.readyBy;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Holds the actual job queues for the JobRegistry singleton.
|
|
10
|
+
* Not exported — use the static `JobRegistry` facade instead.
|
|
11
|
+
* @author darthjee
|
|
12
|
+
*/
|
|
13
|
+
class JobRegistryInstance {
|
|
14
|
+
#enqueued;
|
|
15
|
+
#failed;
|
|
16
|
+
#retryQueue;
|
|
17
|
+
#finished;
|
|
18
|
+
#dead;
|
|
19
|
+
#processing;
|
|
20
|
+
#cooldown;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @param {object} [options={}] - Construction options.
|
|
24
|
+
* @param {Queue} [options.queue] - Initial enqueued jobs queue.
|
|
25
|
+
* @param {SortedCollection} [options.failed] - Initial failed jobs collection.
|
|
26
|
+
* @param {Queue} [options.retryQueue] - Initial retry queue.
|
|
27
|
+
* @param {IdentifyableCollection} [options.finished] - Initial finished jobs collection.
|
|
28
|
+
* @param {IdentifyableCollection} [options.dead] - Initial dead jobs collection.
|
|
29
|
+
* @param {IdentifyableCollection} [options.processing] - Initial processing jobs collection.
|
|
30
|
+
* @param {number} [options.cooldown=5000] - Cooldown in milliseconds before a failed job is retried.
|
|
31
|
+
*/
|
|
32
|
+
constructor({ queue, failed, retryQueue, finished, dead, processing, cooldown = 5000 } = {}) {
|
|
33
|
+
this.#enqueued = queue || new Queue();
|
|
34
|
+
this.#failed = failed || new SortedCollection([], { sortBy: FAILED_SORT_BY });
|
|
35
|
+
this.#retryQueue = retryQueue || new Queue();
|
|
36
|
+
this.#finished = finished || new IdentifyableCollection();
|
|
37
|
+
this.#dead = dead || new IdentifyableCollection();
|
|
38
|
+
this.#processing = processing || new IdentifyableCollection();
|
|
39
|
+
this.#cooldown = cooldown;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Enqueues a new job using the factory registered under the given key.
|
|
44
|
+
* @param {string} factoryKey - The factory key to use.
|
|
45
|
+
* @param {object} [params={}] - Build params forwarded to the factory.
|
|
46
|
+
* @returns {Job} The created and enqueued Job instance.
|
|
47
|
+
*/
|
|
48
|
+
enqueue(factoryKey, params = {}) {
|
|
49
|
+
const job = JobFactory.get(factoryKey).build(params);
|
|
50
|
+
this.#enqueued.push(job);
|
|
51
|
+
return job;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Marks a job as failed.
|
|
56
|
+
* @param {Job} job - The job to mark as failed.
|
|
57
|
+
* @returns {void}
|
|
58
|
+
*/
|
|
59
|
+
fail(job) {
|
|
60
|
+
if (!job) return;
|
|
61
|
+
this.#processing.remove(job.id);
|
|
62
|
+
if (job.exhausted()) {
|
|
63
|
+
this.#dead.push(job);
|
|
64
|
+
} else {
|
|
65
|
+
job.applyCooldown(this.#cooldown);
|
|
66
|
+
this.#failed.push(job);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Marks a job as finished.
|
|
72
|
+
* @param {Job} job - The job to mark as finished.
|
|
73
|
+
* @returns {void}
|
|
74
|
+
*/
|
|
75
|
+
finish(job) {
|
|
76
|
+
if (!job) return;
|
|
77
|
+
this.#processing.remove(job.id);
|
|
78
|
+
this.#finished.push(job);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Picks (removes and returns) the first ready job and adds it to processing.
|
|
83
|
+
* @returns {Job|undefined} The first available job, or undefined if none are ready.
|
|
84
|
+
*/
|
|
85
|
+
pick() {
|
|
86
|
+
const job = this.#enqueued.pick() || this.#retryQueue.pick();
|
|
87
|
+
if (job) {
|
|
88
|
+
this.#processing.push(job);
|
|
89
|
+
}
|
|
90
|
+
return job;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Moves a job from processing back to the enqueued queue.
|
|
95
|
+
* @param {Job} job - The job to re-enqueue.
|
|
96
|
+
* @returns {void}
|
|
97
|
+
*/
|
|
98
|
+
requeue(job) {
|
|
99
|
+
if (!job) return;
|
|
100
|
+
this.#processing.remove(job.id);
|
|
101
|
+
this.#enqueued.push(job);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Promotes jobs from the failed queue to the retryQueue once their cooldown has elapsed.
|
|
106
|
+
* @returns {void}
|
|
107
|
+
*/
|
|
108
|
+
promoteReadyJobs() {
|
|
109
|
+
const now = Date.now();
|
|
110
|
+
const ready = this.#failed.upTo(now);
|
|
111
|
+
const waiting = this.#failed.after(now);
|
|
112
|
+
|
|
113
|
+
this.#failed = new SortedCollection(waiting, { sortBy: FAILED_SORT_BY });
|
|
114
|
+
ready.forEach(job => this.#retryQueue.push(job));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Returns whether the registry has any jobs pending (including those in cooldown).
|
|
119
|
+
* @returns {boolean} True if any of enqueued, failed, or retryQueue is non-empty.
|
|
120
|
+
*/
|
|
121
|
+
hasJob() {
|
|
122
|
+
return this.#enqueued.hasAny() || this.#failed.hasAny() || this.#retryQueue.hasAny();
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Returns whether there is a job ready to be picked by a worker right now.
|
|
127
|
+
* @returns {boolean} True if enqueued or retryQueue is non-empty.
|
|
128
|
+
*/
|
|
129
|
+
hasReadyJob() {
|
|
130
|
+
return this.#enqueued.hasAny() || this.#retryQueue.hasAny();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Returns counts of jobs in each state.
|
|
135
|
+
* @returns {{ enqueued: number, processing: number, failed: number, retryQueue: number, finished: number, dead: number }} Counts of jobs in each state.
|
|
136
|
+
*/
|
|
137
|
+
stats() {
|
|
138
|
+
return {
|
|
139
|
+
enqueued: this.#enqueued.size(),
|
|
140
|
+
processing: this.#processing.size(),
|
|
141
|
+
failed: this.#failed.size(),
|
|
142
|
+
retryQueue: this.#retryQueue.size(),
|
|
143
|
+
finished: this.#finished.size(),
|
|
144
|
+
dead: this.#dead.size(),
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export { JobRegistryInstance };
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { ItemNotFound } from '../exceptions/ItemNotFound.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* NamedRegistry is a base class for registries that manage named items.
|
|
5
|
+
* It provides a common interface for retrieving items by name and handling not-found cases.
|
|
6
|
+
* Subclasses should define the specific type of items they manage and the appropriate not-found exception.
|
|
7
|
+
* @author darthjee
|
|
8
|
+
*/
|
|
9
|
+
class NamedRegistry {
|
|
10
|
+
/*
|
|
11
|
+
* The exception class to throw when an item is not found.
|
|
12
|
+
* Subclasses should override this property to specify the appropriate exception type.
|
|
13
|
+
* @see ItemNotFound
|
|
14
|
+
* @see #notFound
|
|
15
|
+
* @type {class}
|
|
16
|
+
*/
|
|
17
|
+
static notFoundException = ItemNotFound;
|
|
18
|
+
|
|
19
|
+
#size;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @param {object} items An object mapping item names to their corresponding instances.
|
|
23
|
+
*/
|
|
24
|
+
constructor(items) {
|
|
25
|
+
this.items = items;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Retrieves an item by name.
|
|
30
|
+
* @param {string} name The name of the item to retrieve.
|
|
31
|
+
* @returns {*} The item associated with the given name.
|
|
32
|
+
* @throws {ItemNotFound} If the item with the specified name does not exist.
|
|
33
|
+
*/
|
|
34
|
+
getItem(name) {
|
|
35
|
+
if (name in this.items) {
|
|
36
|
+
return this.items[name];
|
|
37
|
+
}
|
|
38
|
+
this.#notFound(name);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Filters the items in the registry based on a predicate function.
|
|
43
|
+
* @param {function} predicate The predicate function to test each item.
|
|
44
|
+
* @returns {Array} An array of items that match the predicate.
|
|
45
|
+
*/
|
|
46
|
+
filter(predicate) {
|
|
47
|
+
return Object.values(this.items).filter(predicate);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Returns the number of items in the registry.
|
|
52
|
+
* @returns {number} The number of items in the registry.
|
|
53
|
+
*/
|
|
54
|
+
size() {
|
|
55
|
+
if (this.#size === undefined) {
|
|
56
|
+
this.#size = Object.keys(this.items).length;
|
|
57
|
+
}
|
|
58
|
+
return this.#size;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Throws a not-found exception for the specified item name.
|
|
63
|
+
* @param {string} name The name of the item that was not found.
|
|
64
|
+
* @throws {ItemNotFound} Always throws an exception indicating the item was not found.
|
|
65
|
+
* @see #notFoundException
|
|
66
|
+
*/
|
|
67
|
+
#notFound(name) {
|
|
68
|
+
const Ex = this.constructor.notFoundException;
|
|
69
|
+
throw new Ex(name);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export { NamedRegistry };
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { NamedRegistry } from './NamedRegistry.js';
|
|
2
|
+
import { ResourceNotFound } from '../exceptions/ResourceNotFound.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Registry of named Resource instances.
|
|
6
|
+
*
|
|
7
|
+
* Implements a static singleton facade: call `ResourceRegistry.build(items)` once
|
|
8
|
+
* during application bootstrap, then use static delegation methods (`getItem`,
|
|
9
|
+
* `filter`, `size`) to access the registry. Use `ResourceRegistry.reset()` in
|
|
10
|
+
* tests to restore a clean state between examples.
|
|
11
|
+
* @author darthjee
|
|
12
|
+
* @augments NamedRegistry
|
|
13
|
+
*/
|
|
14
|
+
class ResourceRegistry extends NamedRegistry {
|
|
15
|
+
/**
|
|
16
|
+
* The exception class to throw when a resource is not found.
|
|
17
|
+
* @see ResourceNotFound
|
|
18
|
+
* @type {class}
|
|
19
|
+
* @see NamedRegistry#notFound
|
|
20
|
+
* @see NamedRegistry#notFoundException
|
|
21
|
+
*/
|
|
22
|
+
static notFoundException = ResourceNotFound;
|
|
23
|
+
|
|
24
|
+
static #instance = null;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Creates and stores the singleton instance.
|
|
28
|
+
* @param {object} items - An object mapping resource names to Resource instances.
|
|
29
|
+
* @returns {ResourceRegistry} The created instance.
|
|
30
|
+
* @throws {Error} If `build()` has already been called without a preceding `reset()`.
|
|
31
|
+
*/
|
|
32
|
+
static build(items) {
|
|
33
|
+
if (ResourceRegistry.#instance) {
|
|
34
|
+
throw new Error('ResourceRegistry.build() has already been called. Call reset() first.');
|
|
35
|
+
}
|
|
36
|
+
ResourceRegistry.#instance = new ResourceRegistry(items);
|
|
37
|
+
return ResourceRegistry.#instance;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Destroys the singleton instance. Intended for test teardown.
|
|
42
|
+
* @returns {void}
|
|
43
|
+
*/
|
|
44
|
+
static reset() {
|
|
45
|
+
ResourceRegistry.#instance = null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Retrieves a resource by name from the singleton instance.
|
|
50
|
+
* @param {string} name - The name of the resource to retrieve.
|
|
51
|
+
* @returns {*} The resource associated with the given name.
|
|
52
|
+
* @throws {ResourceNotFound} If the resource with the specified name does not exist.
|
|
53
|
+
* @throws {Error} If `build()` has not been called.
|
|
54
|
+
*/
|
|
55
|
+
static getItem(name) {
|
|
56
|
+
return ResourceRegistry.#getInstance().getItem(name);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Filters the resources in the singleton instance based on a predicate function.
|
|
61
|
+
* @param {function} predicate - The predicate function to test each resource.
|
|
62
|
+
* @returns {Array} An array of resources that match the predicate.
|
|
63
|
+
* @throws {Error} If `build()` has not been called.
|
|
64
|
+
*/
|
|
65
|
+
static filter(predicate) {
|
|
66
|
+
return ResourceRegistry.#getInstance().filter(predicate);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Returns the number of resources in the singleton instance.
|
|
71
|
+
* @returns {number} The number of resources in the registry.
|
|
72
|
+
* @throws {Error} If `build()` has not been called.
|
|
73
|
+
*/
|
|
74
|
+
static size() {
|
|
75
|
+
return ResourceRegistry.#getInstance().size();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Returns the singleton instance, throwing if not yet built.
|
|
80
|
+
* @returns {ResourceRegistry} The singleton instance.
|
|
81
|
+
* @throws {Error} If `build()` has not been called.
|
|
82
|
+
*/
|
|
83
|
+
static #getInstance() {
|
|
84
|
+
if (!ResourceRegistry.#instance) {
|
|
85
|
+
throw new Error('ResourceRegistry has not been built. Call ResourceRegistry.build() first.');
|
|
86
|
+
}
|
|
87
|
+
return ResourceRegistry.#instance;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export { ResourceRegistry };
|