hive-cycle 0.1.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 ADDED
@@ -0,0 +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.
package/README.md ADDED
@@ -0,0 +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
@@ -0,0 +1,28 @@
1
+ import { HiveCycleOptions, Task, TaskHandler } from "./types";
2
+ export declare class HiveCycle<TaskMap extends Record<string, any> = Record<string, any>> {
3
+ private queue;
4
+ private handlers;
5
+ private isRunning;
6
+ private options;
7
+ private activeCount;
8
+ constructor(options?: HiveCycleOptions);
9
+ /**
10
+ * Register a handler for a specific task type.
11
+ */
12
+ registerHandler<K extends keyof TaskMap & string>(type: K, handler: TaskHandler<TaskMap[K]>): void;
13
+ /**
14
+ * Enqueue a new task.
15
+ */
16
+ enqueue<K extends keyof TaskMap & string>(type: K, payload: TaskMap[K], options?: Partial<Task>): Promise<string>;
17
+ /**
18
+ * Start the worker loop.
19
+ */
20
+ start(): void;
21
+ /**
22
+ * Stop the worker loop.
23
+ */
24
+ stop(): void;
25
+ private loop;
26
+ private handleTask;
27
+ private sleep;
28
+ }
@@ -0,0 +1,134 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HiveCycle = void 0;
4
+ const MemoryQueue_1 = require("./MemoryQueue");
5
+ // I will implement a simple ID generator to avoid deps for now if I didn't install uuid.
6
+ // Wait, I haven't installed `uuid`. I should probably implement a simple random ID or install it later.
7
+ // User didn't ask for uuid specifically, so I'll use crypto.randomUUID if available or Math.random shim.
8
+ function generateId() {
9
+ if (typeof crypto !== "undefined" && crypto.randomUUID) {
10
+ return crypto.randomUUID();
11
+ }
12
+ return Math.random().toString(36).substring(2, 15);
13
+ }
14
+ class HiveCycle {
15
+ constructor(options = {}) {
16
+ this.handlers = new Map();
17
+ this.isRunning = false;
18
+ this.activeCount = 0;
19
+ this.options = {
20
+ queue: options.queue || new MemoryQueue_1.MemoryQueue(),
21
+ pollingInterval: options.pollingInterval || 1000,
22
+ maxConcurrency: options.maxConcurrency || 1,
23
+ logger: options.logger || console,
24
+ };
25
+ this.queue = this.options.queue;
26
+ }
27
+ /**
28
+ * Register a handler for a specific task type.
29
+ */
30
+ registerHandler(type, handler) {
31
+ this.handlers.set(type, handler);
32
+ }
33
+ /**
34
+ * Enqueue a new task.
35
+ */
36
+ async enqueue(type, payload, options) {
37
+ const task = {
38
+ id: generateId(),
39
+ type,
40
+ payload,
41
+ timestamp: Date.now(),
42
+ ...options,
43
+ };
44
+ await this.queue.enqueue(task);
45
+ return task.id;
46
+ }
47
+ /**
48
+ * Start the worker loop.
49
+ */
50
+ start() {
51
+ if (this.isRunning)
52
+ return;
53
+ this.isRunning = true;
54
+ this.options.logger.log("HiveCycle engine started.");
55
+ this.loop();
56
+ }
57
+ /**
58
+ * Stop the worker loop.
59
+ */
60
+ stop() {
61
+ this.isRunning = false;
62
+ this.options.logger.log("HiveCycle engine stopping...");
63
+ }
64
+ async loop() {
65
+ while (this.isRunning) {
66
+ if (this.activeCount < this.options.maxConcurrency) {
67
+ try {
68
+ const task = await this.queue.dequeue();
69
+ if (task) {
70
+ this.handleTask(task);
71
+ // If we have concurrency, we might want to immediately try to fetch another one
72
+ // instead of waiting specifically. But to prevent tight CPU looping in async:
73
+ // we continue the loop.
74
+ continue;
75
+ }
76
+ else {
77
+ // Queue empty
78
+ await this.sleep(this.options.pollingInterval);
79
+ }
80
+ }
81
+ catch (err) {
82
+ this.options.logger.error("Error in main loop:", err);
83
+ await this.sleep(this.options.pollingInterval);
84
+ }
85
+ }
86
+ else {
87
+ // Max concurrency reached
88
+ await this.sleep(100);
89
+ }
90
+ }
91
+ }
92
+ async handleTask(task) {
93
+ this.activeCount++;
94
+ try {
95
+ const handler = this.handlers.get(task.type);
96
+ if (!handler) {
97
+ throw new Error(`No handler registered for task type: ${task.type}`);
98
+ }
99
+ await handler(task);
100
+ await this.queue.acknowledge(task.id);
101
+ if (task.requeue) {
102
+ const nextRun = async () => {
103
+ try {
104
+ await this.enqueue(task.type, task.payload, {
105
+ requeue: true,
106
+ requeueDelay: task.requeueDelay,
107
+ });
108
+ }
109
+ catch (e) {
110
+ this.options.logger.error(`Failed to automatically requeue task ${task.type}`, e);
111
+ }
112
+ };
113
+ if (task.requeueDelay && task.requeueDelay > 0) {
114
+ setTimeout(nextRun, task.requeueDelay);
115
+ }
116
+ else {
117
+ await nextRun();
118
+ }
119
+ }
120
+ }
121
+ catch (err) {
122
+ this.options.logger.error(`Task ${task.id} failed:`, err);
123
+ // Depending on the queue implementation, reject might be handled differently
124
+ await this.queue.reject(task.id, err);
125
+ }
126
+ finally {
127
+ this.activeCount--;
128
+ }
129
+ }
130
+ sleep(ms) {
131
+ return new Promise((resolve) => setTimeout(resolve, ms));
132
+ }
133
+ }
134
+ exports.HiveCycle = HiveCycle;
@@ -0,0 +1,9 @@
1
+ import { QueueAdapter, Task } from "./types";
2
+ export declare class MemoryQueue implements QueueAdapter {
3
+ private queue;
4
+ private processing;
5
+ enqueue(task: Task): Promise<void>;
6
+ dequeue(): Promise<Task | null>;
7
+ acknowledge(taskId: string): Promise<void>;
8
+ reject(taskId: string, error?: Error): Promise<void>;
9
+ }
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MemoryQueue = void 0;
4
+ class MemoryQueue {
5
+ constructor() {
6
+ this.queue = [];
7
+ this.processing = new Map();
8
+ }
9
+ async enqueue(task) {
10
+ this.queue.push(task);
11
+ }
12
+ async dequeue() {
13
+ const task = this.queue.shift();
14
+ if (!task)
15
+ return null;
16
+ this.processing.set(task.id, task);
17
+ return task;
18
+ }
19
+ async acknowledge(taskId) {
20
+ this.processing.delete(taskId);
21
+ }
22
+ async reject(taskId, error) {
23
+ const task = this.processing.get(taskId);
24
+ if (task) {
25
+ this.processing.delete(taskId);
26
+ // Re-queue the task for retry
27
+ this.queue.push(task);
28
+ }
29
+ }
30
+ }
31
+ exports.MemoryQueue = MemoryQueue;
@@ -0,0 +1,3 @@
1
+ export * from "./types";
2
+ export * from "./HiveCycle";
3
+ export * from "./MemoryQueue";
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./types"), exports);
18
+ __exportStar(require("./HiveCycle"), exports);
19
+ __exportStar(require("./MemoryQueue"), exports);
@@ -0,0 +1,42 @@
1
+ export interface Task<T = any> {
2
+ id: string;
3
+ type: string;
4
+ payload: T;
5
+ retries?: number;
6
+ timestamp?: number;
7
+ requeue?: boolean;
8
+ requeueDelay?: number;
9
+ }
10
+ export interface QueueAdapter {
11
+ /**
12
+ * Add a task to the queue
13
+ */
14
+ enqueue(task: Task): Promise<void>;
15
+ /**
16
+ * Retrieve the next task from the queue.
17
+ * Should return null if the queue is empty.
18
+ */
19
+ dequeue(): Promise<Task | null>;
20
+ /**
21
+ * Mark a task as successfully completed.
22
+ */
23
+ acknowledge(taskId: string): Promise<void>;
24
+ /**
25
+ * Handle a task failure (e.g., re-queue or move to DLQ).
26
+ */
27
+ reject(taskId: string, error?: Error): Promise<void>;
28
+ }
29
+ export interface TaskHandler<T = any> {
30
+ (task: Task<T>): Promise<void>;
31
+ }
32
+ export interface HiveCycleOptions {
33
+ queue?: QueueAdapter;
34
+ pollingInterval?: number;
35
+ maxConcurrency?: number;
36
+ logger?: Logger;
37
+ }
38
+ export interface Logger {
39
+ log(message: string, ...args: any[]): void;
40
+ error(message: string, ...args: any[]): void;
41
+ warn(message: string, ...args: any[]): void;
42
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json ADDED
@@ -0,0 +1,29 @@
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
+ }
29
+ }