hive-cycle 0.1.1 → 0.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.
- package/dist/src/HiveCycle.d.ts +3 -0
- package/dist/src/HiveCycle.js +34 -0
- package/dist/src/types.d.ts +4 -0
- package/package.json +1 -1
package/dist/src/HiveCycle.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export declare class HiveCycle<TaskMap extends Record<string, any> = Record<stri
|
|
|
5
5
|
private isRunning;
|
|
6
6
|
private options;
|
|
7
7
|
private activeCount;
|
|
8
|
+
private server?;
|
|
8
9
|
constructor(options?: HiveCycleOptions);
|
|
9
10
|
/**
|
|
10
11
|
* Register a handler for a specific task type.
|
|
@@ -22,6 +23,8 @@ export declare class HiveCycle<TaskMap extends Record<string, any> = Record<stri
|
|
|
22
23
|
* Stop the worker loop.
|
|
23
24
|
*/
|
|
24
25
|
stop(): void;
|
|
26
|
+
private startHealthServer;
|
|
27
|
+
private stopHealthServer;
|
|
25
28
|
private loop;
|
|
26
29
|
private handleTask;
|
|
27
30
|
private sleep;
|
package/dist/src/HiveCycle.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.HiveCycle = void 0;
|
|
4
|
+
const http_1 = require("http");
|
|
4
5
|
const MemoryQueue_1 = require("./MemoryQueue");
|
|
5
6
|
// I will implement a simple ID generator to avoid deps for now if I didn't install uuid.
|
|
6
7
|
// Wait, I haven't installed `uuid`. I should probably implement a simple random ID or install it later.
|
|
@@ -21,6 +22,7 @@ class HiveCycle {
|
|
|
21
22
|
pollingInterval: options.pollingInterval || 1000,
|
|
22
23
|
maxConcurrency: options.maxConcurrency || 1,
|
|
23
24
|
logger: options.logger || console,
|
|
25
|
+
healthPort: options.healthPort,
|
|
24
26
|
};
|
|
25
27
|
this.queue = this.options.queue;
|
|
26
28
|
}
|
|
@@ -52,6 +54,9 @@ class HiveCycle {
|
|
|
52
54
|
return;
|
|
53
55
|
this.isRunning = true;
|
|
54
56
|
this.options.logger.log("HiveCycle engine started.");
|
|
57
|
+
if (this.options.healthPort) {
|
|
58
|
+
this.startHealthServer(this.options.healthPort);
|
|
59
|
+
}
|
|
55
60
|
this.loop();
|
|
56
61
|
}
|
|
57
62
|
/**
|
|
@@ -60,6 +65,35 @@ class HiveCycle {
|
|
|
60
65
|
stop() {
|
|
61
66
|
this.isRunning = false;
|
|
62
67
|
this.options.logger.log("HiveCycle engine stopping...");
|
|
68
|
+
this.stopHealthServer();
|
|
69
|
+
}
|
|
70
|
+
startHealthServer(port) {
|
|
71
|
+
this.server = (0, http_1.createServer)((req, res) => {
|
|
72
|
+
if (req.url === "/health" && req.method === "GET") {
|
|
73
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
74
|
+
res.end(JSON.stringify({
|
|
75
|
+
status: "ok",
|
|
76
|
+
running: this.isRunning,
|
|
77
|
+
activeCount: this.activeCount,
|
|
78
|
+
}));
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
res.writeHead(404);
|
|
82
|
+
res.end();
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
this.server.listen(port, () => {
|
|
86
|
+
this.options.logger.log(`Health check server listening on port ${port}`);
|
|
87
|
+
});
|
|
88
|
+
this.server.on("error", (err) => {
|
|
89
|
+
this.options.logger.error("Health server error:", err);
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
stopHealthServer() {
|
|
93
|
+
if (this.server) {
|
|
94
|
+
this.server.close();
|
|
95
|
+
this.server = undefined;
|
|
96
|
+
}
|
|
63
97
|
}
|
|
64
98
|
async loop() {
|
|
65
99
|
while (this.isRunning) {
|
package/dist/src/types.d.ts
CHANGED
|
@@ -34,6 +34,10 @@ export interface HiveCycleOptions {
|
|
|
34
34
|
pollingInterval?: number;
|
|
35
35
|
maxConcurrency?: number;
|
|
36
36
|
logger?: Logger;
|
|
37
|
+
/**
|
|
38
|
+
* If provided, starts a health check HTTP server on this port.
|
|
39
|
+
*/
|
|
40
|
+
healthPort?: number;
|
|
37
41
|
}
|
|
38
42
|
export interface Logger {
|
|
39
43
|
log(message: string, ...args: any[]): void;
|