@rango-dev/queue-manager-core 0.1.10-next.69
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/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/manager.d.ts +210 -0
- package/dist/manager.d.ts.map +1 -0
- package/dist/persistor.d.ts +19 -0
- package/dist/persistor.d.ts.map +1 -0
- package/dist/queue-manager-core.cjs.development.js +1513 -0
- package/dist/queue-manager-core.cjs.development.js.map +1 -0
- package/dist/queue-manager-core.cjs.production.min.js +2 -0
- package/dist/queue-manager-core.cjs.production.min.js.map +1 -0
- package/dist/queue-manager-core.esm.js +1509 -0
- package/dist/queue-manager-core.esm.js.map +1 -0
- package/dist/queue.d.ts +182 -0
- package/dist/queue.d.ts.map +1 -0
- package/dist/types.d.ts +22 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +55 -0
- package/readme.md +3 -0
- package/src/index.ts +3 -0
- package/src/manager.ts +606 -0
- package/src/persistor.ts +63 -0
- package/src/queue.ts +623 -0
- package/src/types.ts +25 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,aAAa,CAAC;AACnD,cAAc,SAAS,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import Queue, { QueueEventHandlers, TaskEvent } from './queue';
|
|
2
|
+
import { QueueStorage, Status } from './types';
|
|
3
|
+
export declare type ManagerContext = object;
|
|
4
|
+
export declare type QueueName = string;
|
|
5
|
+
export declare type QueueID = string;
|
|
6
|
+
export declare type BlockedTask = {
|
|
7
|
+
queue_id: string;
|
|
8
|
+
task_id: string;
|
|
9
|
+
action: string;
|
|
10
|
+
reason: Record<string, unknown>;
|
|
11
|
+
storage: {
|
|
12
|
+
get: () => QueueStorage;
|
|
13
|
+
set: (data: QueueStorage) => QueueStorage;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
export declare type SetStorage<T> = (nextStorage: T) => T;
|
|
17
|
+
export interface ExecuterActions<T extends QueueStorage = QueueStorage, V extends string = string, C = ManagerContext> {
|
|
18
|
+
next: () => void;
|
|
19
|
+
retry: () => void;
|
|
20
|
+
failed: () => void;
|
|
21
|
+
schedule: (actionName: V) => void;
|
|
22
|
+
setStorage: SetStorage<T>;
|
|
23
|
+
getStorage: () => T;
|
|
24
|
+
block: (reason: Record<string, unknown>) => void;
|
|
25
|
+
unblock: () => void;
|
|
26
|
+
context: C;
|
|
27
|
+
}
|
|
28
|
+
export interface QueueDef<T extends QueueStorage = QueueStorage, V extends string = string, C = ManagerContext> {
|
|
29
|
+
name: QueueName;
|
|
30
|
+
actions: {
|
|
31
|
+
[K in V]: (actions: ExecuterActions<T, V, C>) => void | Promise<void>;
|
|
32
|
+
};
|
|
33
|
+
events?: Partial<QueueEventHandlers>;
|
|
34
|
+
run: V[];
|
|
35
|
+
whenTaskBlocked?: (event: any, params: {
|
|
36
|
+
queue_id: string;
|
|
37
|
+
queue: Queue;
|
|
38
|
+
context: C;
|
|
39
|
+
getBlockedTasks: () => BlockedTask[];
|
|
40
|
+
forceExecute: (queue_id: string, data?: object) => void;
|
|
41
|
+
retry: () => void;
|
|
42
|
+
manager: Manager;
|
|
43
|
+
}) => void;
|
|
44
|
+
}
|
|
45
|
+
export interface Events {
|
|
46
|
+
onCreateQueue: (queue: QueueInfo & {
|
|
47
|
+
id: QueueID;
|
|
48
|
+
}) => void;
|
|
49
|
+
onUpdateQueue: (queue_id: QueueID, queue: QueueInfo) => void;
|
|
50
|
+
onCreateTask: (queue_id: QueueID, event: TaskEvent) => void;
|
|
51
|
+
onUpdateTask: (queue_id: QueueID, event: TaskEvent) => void;
|
|
52
|
+
onStorageUpdate: (queue_id: QueueID, data: QueueStorage) => void;
|
|
53
|
+
onTaskBlock: (queue_id: QueueID) => void;
|
|
54
|
+
onPersistedDataLoaded: (manager: Manager) => void;
|
|
55
|
+
}
|
|
56
|
+
interface ManagerOptions {
|
|
57
|
+
events?: Partial<Events>;
|
|
58
|
+
queuesDefs: QueueDef[];
|
|
59
|
+
context?: ManagerContext;
|
|
60
|
+
isPaused?: boolean;
|
|
61
|
+
}
|
|
62
|
+
export interface QueueInfo {
|
|
63
|
+
name: QueueName;
|
|
64
|
+
createdAt: number;
|
|
65
|
+
status: Status;
|
|
66
|
+
list: Queue;
|
|
67
|
+
actions: {
|
|
68
|
+
run: () => void;
|
|
69
|
+
cancel: () => void;
|
|
70
|
+
setStorage: SetStorage<any>;
|
|
71
|
+
getStorage: () => any;
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
declare class Manager {
|
|
75
|
+
private queuesDefs;
|
|
76
|
+
private queues;
|
|
77
|
+
private events;
|
|
78
|
+
private persistor;
|
|
79
|
+
private context;
|
|
80
|
+
private isPaused;
|
|
81
|
+
/**
|
|
82
|
+
*
|
|
83
|
+
* Making an instance, initilize events, setup a persistor and try to recover the last state of the manager.
|
|
84
|
+
*
|
|
85
|
+
* */
|
|
86
|
+
constructor(options: ManagerOptions);
|
|
87
|
+
/**
|
|
88
|
+
*
|
|
89
|
+
* Reading persisted data from storage then brings into memory.
|
|
90
|
+
*
|
|
91
|
+
* Notes:
|
|
92
|
+
* - Reset the memory, so we can call this method whenever we want and not only on the initialize process.
|
|
93
|
+
* - All the events will be tirggered (like onCreateQueue, onCreateTask, onBlock, ....)
|
|
94
|
+
* - Try to `resume` if the status is `running`.
|
|
95
|
+
* - Trigger `onPersistedDataLoaded` event when queues recovered from storage.
|
|
96
|
+
*
|
|
97
|
+
*/
|
|
98
|
+
private sync;
|
|
99
|
+
/**
|
|
100
|
+
*
|
|
101
|
+
* Making a new instance from `Queue` and adds Manager's event handlers to it.
|
|
102
|
+
*
|
|
103
|
+
* @returns An instance of `Queue` with wrapped event handlers from Manager.
|
|
104
|
+
*
|
|
105
|
+
*/
|
|
106
|
+
private createQueue;
|
|
107
|
+
/**
|
|
108
|
+
* Go through all tasks (from all queues) and return a list of blocked tasks
|
|
109
|
+
*
|
|
110
|
+
* @returns a list of blocked tasks including enough information to get the queue and mutate the storage.
|
|
111
|
+
*
|
|
112
|
+
*/
|
|
113
|
+
private getBlockedTasks;
|
|
114
|
+
/**
|
|
115
|
+
*
|
|
116
|
+
* Add a queue to the manager to keep track of the queue and its state.
|
|
117
|
+
*
|
|
118
|
+
* @param id
|
|
119
|
+
* @param queue
|
|
120
|
+
* @returns
|
|
121
|
+
*/
|
|
122
|
+
private add;
|
|
123
|
+
/**
|
|
124
|
+
*
|
|
125
|
+
* Create a new queue by client.
|
|
126
|
+
*
|
|
127
|
+
* It will do the internal things to make a queue from definitions, and running using Manager.
|
|
128
|
+
*
|
|
129
|
+
* Notes:
|
|
130
|
+
* - After creating the queue, it will be run automatically.
|
|
131
|
+
*
|
|
132
|
+
* @returns an ID for queue so it can be used to get the created queue later by client.
|
|
133
|
+
*
|
|
134
|
+
*/
|
|
135
|
+
create(name: QueueName, storage: QueueStorage): Promise<string>;
|
|
136
|
+
/**
|
|
137
|
+
* Get a queue by its ID.
|
|
138
|
+
*
|
|
139
|
+
* @returns An object includes queue and its state in `Manager`.
|
|
140
|
+
*/
|
|
141
|
+
get(queue_id: QueueID): QueueInfo | undefined;
|
|
142
|
+
/**
|
|
143
|
+
* Get all queues from `Manager`
|
|
144
|
+
*
|
|
145
|
+
* @returns a list of queues includes all the queues and their states.
|
|
146
|
+
*/
|
|
147
|
+
getAll(): Map<string, QueueInfo>;
|
|
148
|
+
/**
|
|
149
|
+
*
|
|
150
|
+
* Ask from manager to run pending queues.
|
|
151
|
+
*
|
|
152
|
+
* It only try to run queues with `PENDING` status and ignore all the other statuses.
|
|
153
|
+
*
|
|
154
|
+
*/
|
|
155
|
+
execute(): void;
|
|
156
|
+
/**
|
|
157
|
+
*
|
|
158
|
+
* Try to find queues with `RUNNING` status to run them again.
|
|
159
|
+
*
|
|
160
|
+
* It's useful for recovering the queue at some certain points
|
|
161
|
+
* like running a currepted task (reloaded when it was running) or needs manual trigger from UI.
|
|
162
|
+
*
|
|
163
|
+
* @returns
|
|
164
|
+
*/
|
|
165
|
+
resume(): void;
|
|
166
|
+
/**
|
|
167
|
+
*
|
|
168
|
+
* Run all `BLOCKED` queues once again.
|
|
169
|
+
*
|
|
170
|
+
* If a queue has `BLOCKED` status and the last task is `BLOCKED` as well,
|
|
171
|
+
* The task will be run one more time.
|
|
172
|
+
*
|
|
173
|
+
* Useful for scenarios like we are blocking the queue under some conditions in task,
|
|
174
|
+
* We can use this method to ask the queue to run the blocked task one more time and
|
|
175
|
+
* maybe this time condtions are met and the queue can be proceed.
|
|
176
|
+
*
|
|
177
|
+
* @returns
|
|
178
|
+
*/
|
|
179
|
+
retry(): void;
|
|
180
|
+
/**
|
|
181
|
+
*
|
|
182
|
+
* Run a blocked task on a specific queue with the ability to pass more data.
|
|
183
|
+
*
|
|
184
|
+
* Useful when we have a custom logic for running queue and needs to pass some specific data
|
|
185
|
+
* to the task and try to run it manually with the provided data.
|
|
186
|
+
*
|
|
187
|
+
*/
|
|
188
|
+
forceExecute(queue_id: string, data?: object): void;
|
|
189
|
+
/**
|
|
190
|
+
*
|
|
191
|
+
* Sync in-memory state (of `Manager`) with storage (persist).
|
|
192
|
+
*
|
|
193
|
+
* Usually we call this method after a change detected in the state of manager.
|
|
194
|
+
*
|
|
195
|
+
*/
|
|
196
|
+
private handleUpdate;
|
|
197
|
+
/**
|
|
198
|
+
* Active readonly mode for manager, it means it doesn't run anythin,
|
|
199
|
+
* And only can be used to read the data.
|
|
200
|
+
*/
|
|
201
|
+
pause(): void;
|
|
202
|
+
/**
|
|
203
|
+
* Activate normal mode which means it will be able to run the queuese as well.
|
|
204
|
+
*/
|
|
205
|
+
run(): void;
|
|
206
|
+
private getContext;
|
|
207
|
+
private shouldExecute;
|
|
208
|
+
}
|
|
209
|
+
export { Manager };
|
|
210
|
+
//# sourceMappingURL=manager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"manager.d.ts","sourceRoot":"","sources":["src/manager.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,EAAE,kBAAkB,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAC/D,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAE/C,oBAAY,cAAc,GAAG,MAAM,CAAC;AACpC,oBAAY,SAAS,GAAG,MAAM,CAAC;AAC/B,oBAAY,OAAO,GAAG,MAAM,CAAC;AAC7B,oBAAY,WAAW,GAAG;IACxB,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAChC,OAAO,EAAE;QACP,GAAG,EAAE,MAAM,YAAY,CAAC;QACxB,GAAG,EAAE,CAAC,IAAI,EAAE,YAAY,KAAK,YAAY,CAAC;KAC3C,CAAC;CACH,CAAC;AAEF,oBAAY,UAAU,CAAC,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,KAAK,CAAC,CAAC;AAElD,MAAM,WAAW,eAAe,CAC9B,CAAC,SAAS,YAAY,GAAG,YAAY,EACrC,CAAC,SAAS,MAAM,GAAG,MAAM,EACzB,CAAC,GAAG,cAAc;IAElB,IAAI,EAAE,MAAM,IAAI,CAAC;IACjB,KAAK,EAAE,MAAM,IAAI,CAAC;IAClB,MAAM,EAAE,MAAM,IAAI,CAAC;IACnB,QAAQ,EAAE,CAAC,UAAU,EAAE,CAAC,KAAK,IAAI,CAAC;IAClC,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC;IAC1B,UAAU,EAAE,MAAM,CAAC,CAAC;IACpB,KAAK,EAAE,CAAC,MAAM,EAAE,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OAWG;IACU,MAAM,CAAC,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,YAAY;IA4D1D;;;;OAIG;IACI,GAAG,CAAC,QAAQ,EAAE,OAAO;IAI5B;;;;OAIG;IACI,MAAM;IAIb;;;;;;OAMG;IACI,OAAO;IAad;;;;;;;;OAQG;IACI,MAAM;IAiBb;;;;;;;;;;;;OAYG;IACI,KAAK;IAiBZ;;;;;;;OAOG;IACI,YAAY,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM;IAenD;;;;;;OAMG;IACH,OAAO,CAAC,YAAY;IAgBpB;;;OAGG;IACI,KAAK;IAIZ;;OAEG;IACI,GAAG;IAQV,OAAO,CAAC,UAAU;IAMlB,OAAO,CAAC,aAAa;CAMtB;AAED,OAAO,EAAE,OAAO,EAAE,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { DBSchema, IDBPDatabase } from 'idb';
|
|
2
|
+
import { QueueID } from './manager';
|
|
3
|
+
import { PersistedQueue } from './types';
|
|
4
|
+
declare type UpdatePersistedQueue = Partial<Pick<PersistedQueue, 'status' | 'state' | 'tasks' | 'storage'>>;
|
|
5
|
+
interface Database extends DBSchema {
|
|
6
|
+
queues: {
|
|
7
|
+
value: PersistedQueue;
|
|
8
|
+
key: string;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
declare class Persistor {
|
|
12
|
+
db: Promise<IDBPDatabase<Database>>;
|
|
13
|
+
constructor();
|
|
14
|
+
insertQueue(queue: PersistedQueue): Promise<void>;
|
|
15
|
+
updateQueue(id: QueueID, queue: UpdatePersistedQueue): Promise<void>;
|
|
16
|
+
getAll(): Promise<PersistedQueue[]>;
|
|
17
|
+
}
|
|
18
|
+
export default Persistor;
|
|
19
|
+
//# sourceMappingURL=persistor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"persistor.d.ts","sourceRoot":"","sources":["src/persistor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,QAAQ,EAAE,YAAY,EAAE,MAAM,KAAK,CAAC;AAErD,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAMzC,aAAK,oBAAoB,GAAG,OAAO,CACjC,IAAI,CAAC,cAAc,EAAE,QAAQ,GAAG,OAAO,GAAG,OAAO,GAAG,SAAS,CAAC,CAC/D,CAAC;AAEF,UAAU,QAAS,SAAQ,QAAQ;IACjC,MAAM,EAAE;QACN,KAAK,EAAE,cAAc,CAAC;QACtB,GAAG,EAAE,MAAM,CAAC;KACb,CAAC;CACH;AAED,cAAM,SAAS;IACb,EAAE,EAAE,OAAO,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC,CAAC;;IAQ9B,WAAW,CAAC,KAAK,EAAE,cAAc;IAUjC,WAAW,CAAC,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE,oBAAoB;IAepD,MAAM;CAMb;AAED,eAAe,SAAS,CAAC"}
|