hive-cycle 0.1.0 → 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/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2026 Jacob K Lewis
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Jacob K Lewis
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,134 +1,134 @@
1
- ![HiveCycle Logo](logo.png)
2
-
3
- # HiveCycle
4
-
5
- A modular, type-safe TypeScript framework for running background task queues. `HiveCycle` provides a robust backbone for processing queued jobs with concurrency control, automatic retries/requeues, and pluggable queue storage.
6
-
7
- ## Features
8
-
9
- - **Continuous Execution**: Runs a worker loop that constantly polls for new tasks.
10
- - **Type Safe**: leveraging TypeScript generics to strongly type your task payloads.
11
- - **Modular**: Abstract `QueueAdapter` allowing you to swap the default In-Memory queue for Redis, RabbitMQ, or SQL/NoSQL databases.
12
- - **Concurrency Control**: Limit how many tasks are processed simultaneously.
13
- - **Recurring Tasks**: Built-in support for tasks that automatically requeue themselves (cron-like behavior).
14
-
15
- ## Installation
16
-
17
- ```bash
18
- npm install hive-cycle
19
- ```
20
-
21
- ## Quick Start
22
-
23
- ### Basic Usage
24
-
25
- ```typescript
26
- import { HiveCycle } from "hive-cycle";
27
-
28
- const app = new HiveCycle();
29
-
30
- // 1. Register a handler
31
- app.registerHandler("email", async (task) => {
32
- console.log("Sending email to:", task.payload.to);
33
- // Perform async work here...
34
- });
35
-
36
- // 2. Start the engine
37
- app.start();
38
-
39
- // 3. Queue a task
40
- app.enqueue("email", { to: "user@example.com" });
41
- ```
42
-
43
- ### Type Safety
44
-
45
- Define your task map interface to get full autocomplete and type checking for payloads.
46
-
47
- ```typescript
48
- import { HiveCycle } from "hive-cycle";
49
-
50
- // Define your task types and their payloads
51
- interface MyTaskMap {
52
- "send-email": { to: string; subject: string; body: string };
53
- "generate-report": { reportId: string };
54
- }
55
-
56
- const app = new HiveCycle<MyTaskMap>();
57
-
58
- // ✅ Fully typed argument
59
- app.registerHandler("send-email", async (task) => {
60
- // task.payload is { to: string; subject: string; body: string }
61
- console.log(task.payload.subject);
62
- });
63
-
64
- // ✅ Type-checked enqueue
65
- app.enqueue("send-email", {
66
- to: "test@example.com",
67
- subject: "Welcome",
68
- body: "Hello World",
69
- });
70
- ```
71
-
72
- ## Advanced Usage
73
-
74
- ### Recurring Tasks
75
-
76
- You can schedule tasks to automatically requeue themselves after completion, creating a loop.
77
-
78
- ```typescript
79
- await app.enqueue(
80
- "cleanup-job",
81
- { key: "temp-files" },
82
- {
83
- requeue: true,
84
- requeueDelay: 5000, // Run again 5 seconds after completion
85
- }
86
- );
87
- ```
88
-
89
- ### Configuration
90
-
91
- You can pass options to the constructor to tune performance.
92
-
93
- ```typescript
94
- const app = new HiveCycle({
95
- // How many tasks to process in parallel
96
- maxConcurrency: 5,
97
-
98
- // How often to check for new tasks when queue is empty (ms)
99
- pollingInterval: 1000,
100
-
101
- // Custom logger (defaults to console)
102
- logger: myLogger,
103
-
104
- // Custom Queue Adapter (defaults to MemoryQueue)
105
- queue: new RedisQueueAdapter(),
106
- });
107
- ```
108
-
109
- ### Custom Queue Adapter
110
-
111
- To use a persistent store (like Redis), implement the `QueueAdapter` interface.
112
-
113
- ```typescript
114
- import { QueueAdapter, Task } from "hive-cycle";
115
-
116
- class MyRedisQueue implements QueueAdapter {
117
- async enqueue(task: Task): Promise<void> {
118
- /* ... */
119
- }
120
- async dequeue(): Promise<Task | null> {
121
- /* ... */
122
- }
123
- async acknowledge(taskId: string): Promise<void> {
124
- /* ... */
125
- }
126
- async reject(taskId: string, error?: Error): Promise<void> {
127
- /* ... */
128
- }
129
- }
130
- ```
131
-
132
- ## License
133
-
134
- MIT
1
+ ![HiveCycle Logo](logo.png)
2
+
3
+ # HiveCycle
4
+
5
+ A modular, type-safe TypeScript framework for running background task queues. `HiveCycle` provides a robust backbone for processing queued jobs with concurrency control, automatic retries/requeues, and pluggable queue storage.
6
+
7
+ ## Features
8
+
9
+ - **Continuous Execution**: Runs a worker loop that constantly polls for new tasks.
10
+ - **Type Safe**: leveraging TypeScript generics to strongly type your task payloads.
11
+ - **Modular**: Abstract `QueueAdapter` allowing you to swap the default In-Memory queue for Redis, RabbitMQ, or SQL/NoSQL databases.
12
+ - **Concurrency Control**: Limit how many tasks are processed simultaneously.
13
+ - **Recurring Tasks**: Built-in support for tasks that automatically requeue themselves (cron-like behavior).
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npm install hive-cycle
19
+ ```
20
+
21
+ ## Quick Start
22
+
23
+ ### Basic Usage
24
+
25
+ ```typescript
26
+ import { HiveCycle } from "hive-cycle";
27
+
28
+ const app = new HiveCycle();
29
+
30
+ // 1. Register a handler
31
+ app.registerHandler("email", async (task) => {
32
+ console.log("Sending email to:", task.payload.to);
33
+ // Perform async work here...
34
+ });
35
+
36
+ // 2. Start the engine
37
+ app.start();
38
+
39
+ // 3. Queue a task
40
+ app.enqueue("email", { to: "user@example.com" });
41
+ ```
42
+
43
+ ### Type Safety
44
+
45
+ Define your task map interface to get full autocomplete and type checking for payloads.
46
+
47
+ ```typescript
48
+ import { HiveCycle } from "hive-cycle";
49
+
50
+ // Define your task types and their payloads
51
+ interface MyTaskMap {
52
+ "send-email": { to: string; subject: string; body: string };
53
+ "generate-report": { reportId: string };
54
+ }
55
+
56
+ const app = new HiveCycle<MyTaskMap>();
57
+
58
+ // ✅ Fully typed argument
59
+ app.registerHandler("send-email", async (task) => {
60
+ // task.payload is { to: string; subject: string; body: string }
61
+ console.log(task.payload.subject);
62
+ });
63
+
64
+ // ✅ Type-checked enqueue
65
+ app.enqueue("send-email", {
66
+ to: "test@example.com",
67
+ subject: "Welcome",
68
+ body: "Hello World",
69
+ });
70
+ ```
71
+
72
+ ## Advanced Usage
73
+
74
+ ### Recurring Tasks
75
+
76
+ You can schedule tasks to automatically requeue themselves after completion, creating a loop.
77
+
78
+ ```typescript
79
+ await app.enqueue(
80
+ "cleanup-job",
81
+ { key: "temp-files" },
82
+ {
83
+ requeue: true,
84
+ requeueDelay: 5000, // Run again 5 seconds after completion
85
+ }
86
+ );
87
+ ```
88
+
89
+ ### Configuration
90
+
91
+ You can pass options to the constructor to tune performance.
92
+
93
+ ```typescript
94
+ const app = new HiveCycle({
95
+ // How many tasks to process in parallel
96
+ maxConcurrency: 5,
97
+
98
+ // How often to check for new tasks when queue is empty (ms)
99
+ pollingInterval: 1000,
100
+
101
+ // Custom logger (defaults to console)
102
+ logger: myLogger,
103
+
104
+ // Custom Queue Adapter (defaults to MemoryQueue)
105
+ queue: new RedisQueueAdapter(),
106
+ });
107
+ ```
108
+
109
+ ### Custom Queue Adapter
110
+
111
+ To use a persistent store (like Redis), implement the `QueueAdapter` interface.
112
+
113
+ ```typescript
114
+ import { QueueAdapter, Task } from "hive-cycle";
115
+
116
+ class MyRedisQueue implements QueueAdapter {
117
+ async enqueue(task: Task): Promise<void> {
118
+ /* ... */
119
+ }
120
+ async dequeue(): Promise<Task | null> {
121
+ /* ... */
122
+ }
123
+ async acknowledge(taskId: string): Promise<void> {
124
+ /* ... */
125
+ }
126
+ async reject(taskId: string, error?: Error): Promise<void> {
127
+ /* ... */
128
+ }
129
+ }
130
+ ```
131
+
132
+ ## License
133
+
134
+ MIT
@@ -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;
@@ -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) {
@@ -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;
package/package.json CHANGED
@@ -1,29 +1,30 @@
1
- {
2
- "name": "hive-cycle",
3
- "version": "0.1.0",
4
- "description": "A modular TypeScript framework for background task processing",
5
- "main": "dist/index.js",
6
- "types": "dist/index.d.ts",
7
- "files": [
8
- "dist/src",
9
- "README.md"
10
- ],
11
- "scripts": {
12
- "build": "tsc",
13
- "prepublishOnly": "npm run build"
14
- },
15
- "keywords": [
16
- "queue",
17
- "background-jobs",
18
- "task-runner",
19
- "framework",
20
- "typescript"
21
- ],
22
- "author": "Jacob K Lewis",
23
- "repository": "jacobklewis/Hive-Cycle",
24
- "license": "MIT",
25
- "devDependencies": {
26
- "typescript": "^5.0.0",
27
- "@types/node": "^20.0.0"
28
- }
1
+ {
2
+ "name": "hive-cycle",
3
+ "version": "0.2.0",
4
+ "description": "A modular TypeScript framework for background task processing",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist/src",
9
+ "README.md"
10
+ ],
11
+ "scripts": {
12
+ "build": "tsc",
13
+ "test": "echo \"No tests specified\" && exit 0",
14
+ "prepublishOnly": "npm run build"
15
+ },
16
+ "keywords": [
17
+ "queue",
18
+ "background-jobs",
19
+ "task-runner",
20
+ "framework",
21
+ "typescript"
22
+ ],
23
+ "author": "Jacob K Lewis",
24
+ "repository": "jacobklewis/Hive-Cycle",
25
+ "license": "MIT",
26
+ "devDependencies": {
27
+ "typescript": "^5.0.0",
28
+ "@types/node": "^20.0.0"
29
+ }
29
30
  }