@supergrowthai/tq 1.0.1-canary.03bf985

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.
Files changed (38) hide show
  1. package/README.md +538 -0
  2. package/dist/core/base/interfaces.d.ts +1 -0
  3. package/dist/core/base/interfaces.js +2 -0
  4. package/dist/core/base/interfaces.js.map +1 -0
  5. package/dist/core/base/interfaces.mjs +2 -0
  6. package/dist/core/base/interfaces.mjs.map +1 -0
  7. package/dist/index.d.ts +2 -0
  8. package/dist/index.js +1734 -0
  9. package/dist/index.js.map +1 -0
  10. package/dist/index.mjs +1734 -0
  11. package/dist/index.mjs.map +1 -0
  12. package/dist/src/adapters/IDatabaseAdapter.d.ts +61 -0
  13. package/dist/src/adapters/InMemoryAdapter.d.ts +24 -0
  14. package/dist/src/adapters/MongoDbAdapter.d.ts +34 -0
  15. package/dist/src/adapters/index.d.ts +4 -0
  16. package/dist/src/adapters/types.d.ts +9 -0
  17. package/dist/src/core/Actions.d.ts +33 -0
  18. package/dist/src/core/TaskHandler.d.ts +35 -0
  19. package/dist/src/core/TaskQueuesManager.d.ts +33 -0
  20. package/dist/src/core/TaskRunner.d.ts +26 -0
  21. package/dist/src/core/TaskStore.d.ts +63 -0
  22. package/dist/src/core/async/AsyncActions.d.ts +22 -0
  23. package/dist/src/core/async/AsyncTaskManager.d.ts +19 -0
  24. package/dist/src/core/async/async-task-manager.d.ts +25 -0
  25. package/dist/src/core/base/interfaces.d.ts +35 -0
  26. package/dist/src/core/environment.d.ts +6 -0
  27. package/dist/src/core/task-processor-types.d.ts +14 -0
  28. package/dist/src/index.d.ts +14 -0
  29. package/dist/src/task-registry.d.ts +4 -0
  30. package/dist/src/test/task-queue.test.d.ts +6 -0
  31. package/dist/src/types.d.ts +1 -0
  32. package/dist/src/utils/task-id-gen.d.ts +4 -0
  33. package/dist/types.d.ts +1 -0
  34. package/dist/types.js +2 -0
  35. package/dist/types.js.map +1 -0
  36. package/dist/types.mjs +2 -0
  37. package/dist/types.mjs.map +1 -0
  38. package/package.json +88 -0
@@ -0,0 +1,63 @@
1
+ import { CronTask, IDatabaseAdapter } from '../adapters/index.js';
2
+ declare class TaskStore<ID = any> {
3
+ private databaseAdapter;
4
+ constructor(databaseAdapter: IDatabaseAdapter<ID>);
5
+ /**
6
+ * Adds multiple tasks to the scheduled queue
7
+ */
8
+ addTasksToScheduled(tasks: CronTask<any>[]): Promise<any>;
9
+ /**
10
+ * Gets mature tasks that are ready to be processed
11
+ * Implements a two-phase approach:
12
+ * 1. Reset stale processing tasks
13
+ * 2. Fetch and mark new mature tasks
14
+ */
15
+ getMatureTasks(timestamp: number): Promise<CronTask<any>[]>;
16
+ /**
17
+ * Marks tasks as processing with current timestamp
18
+ */
19
+ markTasksAsProcessing(taskIds: ID[]): Promise<void>;
20
+ /**
21
+ * Marks tasks as executed/completed
22
+ */
23
+ markTasksAsExecuted(taskIds: ID[]): Promise<void>;
24
+ /**
25
+ * Marks tasks as failed and increments retry count
26
+ */
27
+ markTasksAsFailed(taskIds: ID[]): Promise<void>;
28
+ /**
29
+ * Updates multiple tasks with specific updates
30
+ */
31
+ updateTasks(updates: Array<{
32
+ id: ID;
33
+ updates: Partial<CronTask<any>>;
34
+ }>): Promise<void>;
35
+ /**
36
+ * Gets tasks by their IDs
37
+ */
38
+ getTasksByIds(taskIds: ID[]): Promise<CronTask<any>[]>;
39
+ /**
40
+ * Gets cleanup statistics
41
+ */
42
+ getCleanupStats(): Promise<{
43
+ orphanedTasks: number;
44
+ expiredTasks: number;
45
+ }>;
46
+ /**
47
+ * Cleans up orphaned and expired tasks
48
+ */
49
+ cleanupTasks(): Promise<void>;
50
+ /**
51
+ * Updates tasks for retry with new execution time and retry count
52
+ */
53
+ updateTasksForRetry(tasks: CronTask<any>[]): Promise<void>;
54
+ /**
55
+ * Marks tasks as successful/completed
56
+ */
57
+ markTasksAsSuccess(tasks: CronTask<any>[]): Promise<void>;
58
+ /**
59
+ * Marks tasks as ignored (same as failed for now)
60
+ */
61
+ markTasksAsIgnored(tasks: CronTask<any>[]): Promise<void>;
62
+ }
63
+ export { TaskStore };
@@ -0,0 +1,22 @@
1
+ import { TaskStore } from '../TaskStore.js';
2
+ import { Actions } from '../Actions.js';
3
+ import { IMessageQueue } from '@supergrowthai/mq';
4
+ import { CronTask } from '../../adapters';
5
+ import { TaskQueuesManager } from '../TaskQueuesManager.js';
6
+ export declare class AsyncActions<ID = any> {
7
+ private messageQueue;
8
+ private taskStore;
9
+ private taskQueue;
10
+ private generateId;
11
+ private readonly actions;
12
+ private readonly taskId;
13
+ constructor(messageQueue: IMessageQueue, taskStore: TaskStore<ID>, taskQueue: TaskQueuesManager, actions: Actions, task: CronTask<any>, generateId: () => ID);
14
+ /**
15
+ * Called when the async promise completes to execute the collected actions
16
+ */
17
+ onPromiseFulfilled(): Promise<void>;
18
+ /**
19
+ * Schedule new tasks - replicates the logic from task-handler's addTasks
20
+ */
21
+ private scheduleNewTasks;
22
+ }
@@ -0,0 +1,19 @@
1
+ import { CronTask } from '../../adapters/index.js';
2
+ import { IAsyncTaskManager } from './async-task-manager';
3
+ export declare class AsyncTaskManager implements IAsyncTaskManager {
4
+ private asyncTasks;
5
+ private handedOffTaskIds;
6
+ private readonly maxTasks;
7
+ private totalHandedOff;
8
+ constructor(maxTasks?: number);
9
+ handoffTask(task: CronTask<any>, runningPromise: Promise<void>): boolean;
10
+ shutdown(): Promise<void>;
11
+ getMetrics(): {
12
+ activeTaskCount: number;
13
+ totalHandedOff: number;
14
+ maxTasks: number;
15
+ utilizationPercent: number;
16
+ };
17
+ isHandedOff(taskId: string): boolean;
18
+ canAcceptTask(): boolean;
19
+ }
@@ -0,0 +1,25 @@
1
+ import { CronTask } from '../../adapters';
2
+ /**
3
+ * Interface for managing async tasks that exceed timeout thresholds
4
+ * Implementation should be in tq package to access executor configs
5
+ */
6
+ export interface IAsyncTaskManager<T = any, ID = any> {
7
+ /**
8
+ * Hand off a running task to async management
9
+ * @param message The message that is still being processed
10
+ * @param runningPromise The promise of the still-running task
11
+ * @returns true if accepted, false if queue is full
12
+ */
13
+ handoffTask(message: CronTask<T, ID>, runningPromise: Promise<void>): boolean;
14
+ /**
15
+ * Gracefully shutdown the async task manager
16
+ */
17
+ shutdown(): Promise<void>;
18
+ isHandedOff(taskId: ID): boolean;
19
+ canAcceptTask(): boolean;
20
+ getMetrics(): {
21
+ activeTaskCount: number;
22
+ totalHandedOff: number;
23
+ [key: string]: unknown;
24
+ };
25
+ }
@@ -0,0 +1,35 @@
1
+ import { CronTask } from '../../adapters/types.js';
2
+ export type ExecutorActions<T> = {
3
+ addTasks(task: CronTask<any>[]): void;
4
+ fail(task: CronTask<T>): void;
5
+ success(task: CronTask<T>): void;
6
+ };
7
+ interface IBaseExecutor<T> {
8
+ multiple: boolean;
9
+ default_retries?: number;
10
+ store_on_failure: boolean;
11
+ asyncConfig?: {
12
+ handoffTimeout: number;
13
+ maxConcurrentAsync?: number;
14
+ };
15
+ }
16
+ export interface IMultiTaskExecutor<T> extends IBaseExecutor<T> {
17
+ multiple: true;
18
+ onTasks(tasks: CronTask<T>[], action: ExecutorActions<T>): Promise<void>;
19
+ }
20
+ export interface ISingleTaskExecutor<T> extends IBaseExecutor<T> {
21
+ parallel: boolean;
22
+ multiple: false;
23
+ onTask(task: CronTask<T>, action: ExecutorActions<T>): Promise<void>;
24
+ }
25
+ export interface ISingleTaskNonParallel<T> extends ISingleTaskExecutor<T> {
26
+ parallel: false;
27
+ multiple: false;
28
+ }
29
+ export interface ISingleTaskParallel<T> extends ISingleTaskExecutor<T> {
30
+ chunkSize: number;
31
+ parallel: true;
32
+ multiple: false;
33
+ }
34
+ export type TaskExecutor<T> = IMultiTaskExecutor<T> | ISingleTaskNonParallel<T> | ISingleTaskParallel<T>;
35
+ export {};
@@ -0,0 +1,6 @@
1
+ import { QueueName } from '@supergrowthai/mq';
2
+ /**
3
+ * Gets all queue names for the current environment
4
+ * @returns Array of full queue names including environment suffix
5
+ */
6
+ export declare function getEnabledQueues(): QueueName[];
@@ -0,0 +1,14 @@
1
+ import { CronTask } from '../adapters/index.js';
2
+ /**
3
+ * Result from processing tasks - specific to TQ (Task Queue) logic
4
+ */
5
+ export interface ProcessedTaskResult<PAYLOAD = any, ID = any> {
6
+ failedTasks: CronTask<PAYLOAD, ID>[];
7
+ newTasks: CronTask<PAYLOAD, ID>[];
8
+ successTasks: CronTask<PAYLOAD, ID>[];
9
+ }
10
+ /**
11
+ * Task processor function type that returns task execution results
12
+ * This is different from MQ's MessageProcessor which just consumes messages
13
+ */
14
+ export type TaskProcessor<PAYLOAD = any, ID = any> = (queueId: string, tasks: CronTask<PAYLOAD, ID>[]) => Promise<ProcessedTaskResult<PAYLOAD, ID>>;
@@ -0,0 +1,14 @@
1
+ export * from './types.js';
2
+ export * from './adapters/index.js';
3
+ export { TaskStore } from './core/TaskStore.js';
4
+ export { TaskHandler } from './core/TaskHandler.js';
5
+ export { TaskRunner } from './core/TaskRunner.js';
6
+ export { TaskQueuesManager } from './core/TaskQueuesManager.js';
7
+ export { getEnabledQueues } from './core/environment.js';
8
+ export { Actions } from './core/Actions.js';
9
+ export { AsyncActions } from './core/async/AsyncActions.js';
10
+ export { AsyncTaskManager } from './core/async/AsyncTaskManager.js';
11
+ export * from './core/base/interfaces.js';
12
+ export * from './core/task-processor-types.js';
13
+ export * from './task-registry.js';
14
+ export { setKeyGenerator } from './utils/task-id-gen.js';
@@ -0,0 +1,4 @@
1
+ type TasksTypeExt = string;
2
+ export type TasksType = TasksTypeExt;
3
+ declare const _default: {};
4
+ export default _default;
@@ -0,0 +1,6 @@
1
+ declare module "@supergrowthai/mq" {
2
+ interface QueueRegistry {
3
+ "test-tq-queue": "test-tq-queue";
4
+ }
5
+ }
6
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ import { CronTask } from '../adapters/index.js';
2
+ import { CacheKeyGenerator } from 'memoose-js';
3
+ export declare function tId<T>(task: CronTask<T>): any;
4
+ export declare function setKeyGenerator(generator: CacheKeyGenerator): void;
@@ -0,0 +1 @@
1
+ export {}
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
package/dist/types.mjs ADDED
@@ -0,0 +1,2 @@
1
+
2
+ //# sourceMappingURL=types.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,88 @@
1
+ {
2
+ "name": "@supergrowthai/tq",
3
+ "version": "1.0.1-canary.03bf985",
4
+ "description": "Task queue management library with multiple executor types and async task handling",
5
+ "repository": "https://github.com/captadexp/next-blog",
6
+ "homepage": "https://github.com/captadexp/next-blog/tree/main#readme",
7
+ "type": "module",
8
+ "main": "./dist/index.js",
9
+ "module": "./dist/index.mjs",
10
+ "types": "./dist/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/index.d.ts",
14
+ "import": "./dist/index.mjs",
15
+ "require": "./dist/index.js"
16
+ },
17
+ "./types": {
18
+ "types": "./dist/types.d.ts",
19
+ "import": "./dist/types.mjs",
20
+ "require": "./dist/types.js"
21
+ },
22
+ "./adapters": {
23
+ "types": "./dist/adapters/index.d.ts",
24
+ "import": "./dist/adapters/index.mjs",
25
+ "require": "./dist/adapters/index.js"
26
+ },
27
+ "./core/base/interfaces": {
28
+ "types": "./dist/core/base/interfaces.d.ts",
29
+ "import": "./dist/core/base/interfaces.mjs",
30
+ "require": "./dist/core/base/interfaces.js"
31
+ },
32
+ "./core/actions": {
33
+ "types": "./dist/core/Actions.d.ts",
34
+ "import": "./dist/core/Actions.mjs",
35
+ "require": "./dist/core/Actions.js"
36
+ },
37
+ "./core/actions/async": {
38
+ "types": "./dist/core/async/AsyncActions.d.ts",
39
+ "import": "./dist/core/async/AsyncActions.mjs",
40
+ "require": "./dist/core/async/AsyncActions.js"
41
+ },
42
+ "./core/async": {
43
+ "types": "./dist/core/async/AsyncTaskManager.d.ts",
44
+ "import": "./dist/core/async/AsyncTaskManager.mjs",
45
+ "require": "./dist/core/async/AsyncTaskManager.js"
46
+ },
47
+ "./utils": {
48
+ "types": "./dist/utils/task-id-gen.d.ts",
49
+ "import": "./dist/utils/task-id-gen.mjs",
50
+ "require": "./dist/utils/task-id-gen.js"
51
+ }
52
+ },
53
+ "scripts": {
54
+ "clean": "rm -rf dist",
55
+ "dev": "vite build --watch",
56
+ "build": "vite build",
57
+ "typecheck": "tsc --noEmit",
58
+ "test": "bun src/test",
59
+ "prepublishOnly": "npm run build"
60
+ },
61
+ "files": [
62
+ "dist",
63
+ "README.md",
64
+ "package.json"
65
+ ],
66
+ "author": "Capt ADExp <tech@developer.toys>",
67
+ "license": "MIT",
68
+ "dependencies": {
69
+ "@supergrowthai/mq": "1.0.1-canary.03bf985",
70
+ "lodash": "^4.17.21",
71
+ "moment": "^2.30.1",
72
+ "mongodb": "^6.16.0"
73
+ },
74
+ "devDependencies": {
75
+ "@supergrowthai/utils": "1.0.1",
76
+ "@types/bun": "^1.3.1",
77
+ "@types/node": "^24.5.2",
78
+ "typescript": "^5.9.2",
79
+ "vite": "^7.1.5",
80
+ "vite-plugin-dts": "^4.5.4"
81
+ },
82
+ "publishConfig": {
83
+ "access": "public"
84
+ },
85
+ "peerDependencies": {
86
+ "memoose-js": "^1.1.2"
87
+ }
88
+ }