navi-hey 1.3.2 → 1.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.
@@ -10,12 +10,14 @@ class WebConfig {
10
10
  * @param {number} config.port - The port number to listen on.
11
11
  * @param {number} [config.logs_page_size=20] - Maximum number of log entries per page.
12
12
  * @param {boolean} [config.enable_shutdown=true] - Whether the shutdown button is enabled.
13
+ * @param {boolean} [config.autostart=true] - Whether the engine starts processing immediately at boot.
13
14
  * @param {Array<string|object>} [config.links=[]] - Links shown in the web UI.
14
15
  */
15
- constructor({ port, logs_page_size: logsPageSize = 20, enable_shutdown: enableShutdown = true, links = [] }) {
16
+ constructor({ port, logs_page_size: logsPageSize = 20, enable_shutdown: enableShutdown = true, autostart = true, links = [] }) {
16
17
  this.port = port;
17
18
  this.logsPageSize = logsPageSize;
18
19
  this.enableShutdown = enableShutdown;
20
+ this.autostart = autostart;
19
21
  this.links = links.map((link) => Link.fromObject(link));
20
22
  }
21
23
  }
@@ -38,6 +38,15 @@ class NamedRegistry {
38
38
  this.#notFound(name);
39
39
  }
40
40
 
41
+ /**
42
+ * Checks whether an item with the given name exists in the registry.
43
+ * @param {string} name The name to check.
44
+ * @returns {boolean} True if an item with the given name exists.
45
+ */
46
+ has(name) {
47
+ return name in this.items;
48
+ }
49
+
41
50
  /**
42
51
  * Filters the items in the registry based on a predicate function.
43
52
  * @param {function} predicate The predicate function to test each item.
@@ -56,6 +56,16 @@ class ResourceRegistry extends NamedRegistry {
56
56
  return ResourceRegistry.#getInstance().getItem(name);
57
57
  }
58
58
 
59
+ /**
60
+ * Checks whether a resource with the given name exists in the singleton instance.
61
+ * @param {string} name - The name of the resource to check.
62
+ * @returns {boolean} True if a resource with the given name exists.
63
+ * @throws {Error} If `build()` has not been called.
64
+ */
65
+ static has(name) {
66
+ return ResourceRegistry.#getInstance().has(name);
67
+ }
68
+
59
69
  /**
60
70
  * Filters the resources in the singleton instance based on a predicate function.
61
71
  * @param {function} predicate - The predicate function to test each resource.
@@ -52,6 +52,8 @@ class Router {
52
52
  const router = ExpressRouter();
53
53
  const register = new RouteRegister(router);
54
54
 
55
+ router.use(express.json());
56
+
55
57
  const GET_ROUTES = {
56
58
  '/settings.json': new HandlerConfig(SettingsHandler, this.#webConfig.enableShutdown),
57
59
  '/stats.json': new HandlerConfig(StatsHandler),
@@ -7,25 +7,42 @@ import { Application } from '../../../services/Application.js';
7
7
  * @author darthjee
8
8
  */
9
9
  class EngineStartHandler extends RequestHandler {
10
+ #request;
10
11
  #response;
11
12
 
12
13
  /**
13
- * @param {object} _request - The Express request object.
14
+ * @param {object} request - The Express request object.
14
15
  * @param {object} response - The Express response object.
15
16
  */
16
- constructor(_request, response) {
17
+ constructor(request, response) {
17
18
  super();
19
+ this.#request = request;
18
20
  this.#response = response;
19
21
  }
20
22
 
21
23
  /**
22
- * Starts engine processing from a stopped state.
24
+ * Starts engine processing from a stopped state, or pushes resources into an
25
+ * already-running engine. The request body may name which resources to
26
+ * enqueue (`{ resources: [...] }`); when omitted, all parameter-free
27
+ * resources are enqueued.
23
28
  * @returns {Promise<void>}
24
29
  */
25
30
  async handle() {
26
- if (!Application.isStopped()) throw new ConflictError();
27
- await Application.start();
28
- this.#response.json({ status: 'running' });
31
+ const resources = Array.isArray(this.#request.body?.resources) ? this.#request.body.resources : [];
32
+
33
+ if (Application.isStopped()) {
34
+ const result = await Application.start(resources);
35
+ this.#response.json({ status: 'running', ...result });
36
+ return;
37
+ }
38
+
39
+ if (Application.isRunning()) {
40
+ const result = Application.enqueueResources(resources);
41
+ this.#response.json({ status: 'running', ...result });
42
+ return;
43
+ }
44
+
45
+ throw new ConflictError();
29
46
  }
30
47
  }
31
48
 
@@ -112,6 +112,15 @@ class Application {
112
112
  return Application.#getInstance().enqueueFirstJobs();
113
113
  }
114
114
 
115
+ /**
116
+ * Enqueues resources by name, or all parameter-free resources when no names are given.
117
+ * @param {Array<string>} [names] - Resource names to enqueue; omit/empty for the default set.
118
+ * @returns {{enqueued: Array<string>, skippedResources: Array<object>}}
119
+ */
120
+ static enqueueResources(names) {
121
+ return Application.#getInstance().enqueueResources(names);
122
+ }
123
+
115
124
  /**
116
125
  * Gets the buffered logger from the singleton instance.
117
126
  * @returns {BufferedLogger} The buffered logger instance.
@@ -146,10 +155,11 @@ class Application {
146
155
 
147
156
  /**
148
157
  * Starts processing from a stopped state.
149
- * @returns {Promise<void>}
158
+ * @param {Array<string>} [names] - Resource names to enqueue; omit/empty for the default set.
159
+ * @returns {Promise<{enqueued: Array<string>, skippedResources: Array<object>}|undefined>} The enqueue result, or undefined when not stopped.
150
160
  */
151
- static async start() {
152
- return Application.#getInstance().start();
161
+ static async start(names) {
162
+ return Application.#getInstance().start(names);
153
163
  }
154
164
 
155
165
  /**
@@ -14,6 +14,7 @@ import { Config } from '../models/configs/Config.js';
14
14
  import { LogRegistry } from '../registry/LogRegistry.js';
15
15
  import { WebServer } from '../server/WebServer.js';
16
16
  import { PromiseAggregator } from '../utils/PromiseAggregator.js';
17
+ import { ResourceEnqueuer } from '../utils/ResourceEnqueuer.js';
17
18
  import { ResourceRequestCollector } from '../utils/ResourceRequestCollector.js';
18
19
 
19
20
  const DEFAULT_POLL_SLEEP_MS = 10;
@@ -68,9 +69,15 @@ class ApplicationInstance {
68
69
 
69
70
  this.engine = this.buildEngine();
70
71
  this.webServer = this.buildWebServer();
71
- this.enqueueFirstJobs();
72
72
 
73
- this.#engineStatus = 'running';
73
+ if (this.#shouldAutostart()) {
74
+ this.enqueueFirstJobs();
75
+ this.#engineStatus = 'running';
76
+ } else {
77
+ this.engine.pause();
78
+ this.#engineStatus = 'stopped';
79
+ }
80
+
74
81
  this.#aggregator.add(this.webServer?.start());
75
82
  this.#enginePromise = this.engine.start();
76
83
  this.#aggregator.add(this.#enginePromise);
@@ -120,6 +127,22 @@ class ApplicationInstance {
120
127
  });
121
128
  }
122
129
 
130
+ /**
131
+ * Enqueues resources by name, or all parameter-free resources when no names are given.
132
+ * Unknown resource names, or resources with any request that needs parameters, are
133
+ * skipped entirely and reported back rather than failing the whole call.
134
+ * @param {Array<string>} [names=[]] - Resource names to enqueue; omit/empty for the default set.
135
+ * @returns {{enqueued: Array<string>, skippedResources: Array<{name: string, reason: string}>}} The enqueued names and any skipped resources.
136
+ */
137
+ enqueueResources(names = []) {
138
+ if (!names.length) {
139
+ this.enqueueFirstJobs();
140
+ return { enqueued: [], skippedResources: [] };
141
+ }
142
+
143
+ return new ResourceEnqueuer().enqueue(names);
144
+ }
145
+
123
146
  /**
124
147
  * Returns the current engine status string.
125
148
  * @returns {string|undefined} The current status.
@@ -199,17 +222,19 @@ class ApplicationInstance {
199
222
  }
200
223
 
201
224
  /**
202
- * Starts processing from a stopped state by calling engine.resume() and re-enqueueing initial jobs.
225
+ * Starts processing from a stopped state by calling engine.resume() and enqueueing resources.
203
226
  * No new engine is created; the existing loop continues.
204
227
  * Only valid when status is 'stopped'.
205
- * @returns {Promise<void>}
228
+ * @param {Array<string>} [names=[]] - Resource names to enqueue; omit/empty for the default set.
229
+ * @returns {Promise<{enqueued: Array<string>, skippedResources: Array<object>}|undefined>} The enqueue result, or undefined when not stopped.
206
230
  */
207
- async start() {
208
- if (this.#engineStatus !== 'stopped') return;
231
+ async start(names = []) {
232
+ if (this.#engineStatus !== 'stopped') return undefined;
209
233
  this.engine.resume();
210
- this.enqueueFirstJobs();
234
+ const result = this.enqueueResources(names);
211
235
  this.#engineStatus = 'running';
212
236
  EngineEvents.emit('start');
237
+ return result;
213
238
  }
214
239
 
215
240
  /**
@@ -236,6 +261,15 @@ class ApplicationInstance {
236
261
  this.engine.stop();
237
262
  }
238
263
 
264
+ /**
265
+ * Determines whether the engine should start processing immediately at boot.
266
+ * Defaults to true when there is no web configuration or no explicit setting.
267
+ * @returns {boolean} True if the engine should auto-start.
268
+ */
269
+ #shouldAutostart() {
270
+ return this.config.webConfig?.autostart ?? true;
271
+ }
272
+
239
273
  /**
240
274
  * Polls until all workers are idle.
241
275
  * @returns {Promise<void>}
@@ -0,0 +1,42 @@
1
+ import { JobRegistry } from '../background/JobRegistry.js';
2
+ import { ResourceRegistry } from '../registry/ResourceRegistry.js';
3
+
4
+ /**
5
+ * Resolves resource names against the ResourceRegistry and enqueues the
6
+ * ResourceRequestJobs for each, skipping (rather than partially enqueueing)
7
+ * any resource that is unknown or has a request needing parameters.
8
+ * @author darthjee
9
+ */
10
+ class ResourceEnqueuer {
11
+ /**
12
+ * Enqueues the given resource names.
13
+ * @param {Array<string>} names - Resource names to enqueue.
14
+ * @returns {{enqueued: Array<string>, skippedResources: Array<{name: string, reason: string}>}} The enqueued names and any skipped resources.
15
+ */
16
+ enqueue(names) {
17
+ const enqueued = [];
18
+ const skippedResources = [];
19
+
20
+ names.forEach((name) => {
21
+ if (!ResourceRegistry.has(name)) {
22
+ skippedResources.push({ name, reason: 'not_found' });
23
+ return;
24
+ }
25
+
26
+ const resource = ResourceRegistry.getItem(name);
27
+ if (resource.resourceRequests.some((request) => request.needsParams())) {
28
+ skippedResources.push({ name, reason: 'needs_params' });
29
+ return;
30
+ }
31
+
32
+ resource.resourceRequests.forEach((resourceRequest) => {
33
+ JobRegistry.enqueue('ResourceRequestJob', { resourceRequest, parameters: {} });
34
+ });
35
+ enqueued.push(name);
36
+ });
37
+
38
+ return { enqueued, skippedResources };
39
+ }
40
+ }
41
+
42
+ export { ResourceEnqueuer };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "navi-hey",
3
3
  "type": "module",
4
- "version": "1.3.2",
4
+ "version": "1.4.0",
5
5
  "description": "Navi cache warmer CLI",
6
6
  "readme": "README.md",
7
7
  "main": "index.js",