hive-cycle 0.1.1 → 0.2.1
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/README.md +23 -1
- package/dist/src/HiveCycle.d.ts +4 -0
- package/dist/src/HiveCycle.d.ts.map +1 -0
- package/dist/src/HiveCycle.js +34 -0
- package/dist/src/MemoryQueue.d.ts +1 -0
- package/dist/src/MemoryQueue.d.ts.map +1 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/types.d.ts +5 -0
- package/dist/src/types.d.ts.map +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -11,6 +11,7 @@ A modular, type-safe TypeScript framework for running background task queues. `H
|
|
|
11
11
|
- **Modular**: Abstract `QueueAdapter` allowing you to swap the default In-Memory queue for Redis, RabbitMQ, or SQL/NoSQL databases.
|
|
12
12
|
- **Concurrency Control**: Limit how many tasks are processed simultaneously.
|
|
13
13
|
- **Recurring Tasks**: Built-in support for tasks that automatically requeue themselves (cron-like behavior).
|
|
14
|
+
- **Health Monitoring**: Optional HTTP endpoint to monitor service health and metrics.
|
|
14
15
|
|
|
15
16
|
## Installation
|
|
16
17
|
|
|
@@ -82,7 +83,7 @@ await app.enqueue(
|
|
|
82
83
|
{
|
|
83
84
|
requeue: true,
|
|
84
85
|
requeueDelay: 5000, // Run again 5 seconds after completion
|
|
85
|
-
}
|
|
86
|
+
},
|
|
86
87
|
);
|
|
87
88
|
```
|
|
88
89
|
|
|
@@ -101,11 +102,32 @@ const app = new HiveCycle({
|
|
|
101
102
|
// Custom logger (defaults to console)
|
|
102
103
|
logger: myLogger,
|
|
103
104
|
|
|
105
|
+
// Start a health check server on this port
|
|
106
|
+
healthPort: 3000,
|
|
107
|
+
|
|
104
108
|
// Custom Queue Adapter (defaults to MemoryQueue)
|
|
105
109
|
queue: new RedisQueueAdapter(),
|
|
106
110
|
});
|
|
107
111
|
```
|
|
108
112
|
|
|
113
|
+
### Health Check
|
|
114
|
+
|
|
115
|
+
If you provide a `healthPort` in the configuration, an HTTP server will start and expose a `/health` endpoint.
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
curl http://localhost:3000/health
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
**Response:**
|
|
122
|
+
|
|
123
|
+
```json
|
|
124
|
+
{
|
|
125
|
+
"status": "ok",
|
|
126
|
+
"running": true,
|
|
127
|
+
"activeCount": 2
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
109
131
|
### Custom Queue Adapter
|
|
110
132
|
|
|
111
133
|
To use a persistent store (like Redis), implement the `QueueAdapter` interface.
|
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,7 +23,10 @@ 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;
|
|
28
31
|
}
|
|
32
|
+
//# sourceMappingURL=HiveCycle.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"HiveCycle.d.ts","sourceRoot":"","sources":["../../src/HiveCycle.ts"],"names":[],"mappings":"AACA,OAAO,EACL,gBAAgB,EAEhB,IAAI,EACJ,WAAW,EAEZ,MAAM,SAAS,CAAC;AAcjB,qBAAa,SAAS,CACpB,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAEzD,OAAO,CAAC,KAAK,CAAe;IAC5B,OAAO,CAAC,QAAQ,CAAuC;IACvD,OAAO,CAAC,SAAS,CAAkB;IACnC,OAAO,CAAC,OAAO,CAEb;IACF,OAAO,CAAC,WAAW,CAAa;IAChC,OAAO,CAAC,MAAM,CAAC,CAAS;gBAEZ,OAAO,GAAE,gBAAqB;IAW1C;;OAEG;IACI,eAAe,CAAC,CAAC,SAAS,MAAM,OAAO,GAAG,MAAM,EACrD,IAAI,EAAE,CAAC,EACP,OAAO,EAAE,WAAW,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,GAC/B,IAAI;IAIP;;OAEG;IACU,OAAO,CAAC,CAAC,SAAS,MAAM,OAAO,GAAG,MAAM,EACnD,IAAI,EAAE,CAAC,EACP,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,EACnB,OAAO,CAAC,EAAE,OAAO,CAAC,IAAI,CAAC,GACtB,OAAO,CAAC,MAAM,CAAC;IAYlB;;OAEG;IACI,KAAK,IAAI,IAAI;IAYpB;;OAEG;IACI,IAAI,IAAI,IAAI;IAMnB,OAAO,CAAC,iBAAiB;IA0BzB,OAAO,CAAC,gBAAgB;YAOV,IAAI;YA0BJ,UAAU;IAyCxB,OAAO,CAAC,KAAK;CAGd"}
|
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) {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MemoryQueue.d.ts","sourceRoot":"","sources":["../../src/MemoryQueue.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAE7C,qBAAa,WAAY,YAAW,YAAY;IAC9C,OAAO,CAAC,KAAK,CAAc;IAC3B,OAAO,CAAC,UAAU,CAAgC;IAE5C,OAAO,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAIlC,OAAO,IAAI,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC;IAQ/B,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAI1C,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC;CAQ3D"}
|
package/dist/src/index.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC"}
|
package/dist/src/types.d.ts
CHANGED
|
@@ -34,9 +34,14 @@ 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;
|
|
40
44
|
error(message: string, ...args: any[]): void;
|
|
41
45
|
warn(message: string, ...args: any[]): void;
|
|
42
46
|
}
|
|
47
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.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