document-drive 1.0.0-alpha.7 → 1.0.0-alpha.71
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 +1 -0
- package/package.json +36 -23
- package/src/cache/index.ts +2 -0
- package/src/cache/memory.ts +24 -0
- package/src/cache/redis.ts +29 -0
- package/src/cache/types.ts +9 -0
- package/src/queue/base.ts +258 -0
- package/src/queue/index.ts +2 -0
- package/src/queue/redis.ts +133 -0
- package/src/queue/types.ts +73 -0
- package/src/server/error.ts +17 -0
- package/src/server/index.ts +876 -423
- package/src/server/listener/manager.ts +161 -179
- package/src/server/listener/transmitter/index.ts +1 -0
- package/src/server/listener/transmitter/internal.ts +91 -0
- package/src/server/listener/transmitter/pull-responder.ts +66 -28
- package/src/server/listener/transmitter/switchboard-push.ts +3 -1
- package/src/server/types.ts +130 -18
- package/src/server/utils.ts +35 -0
- package/src/storage/browser.ts +34 -15
- package/src/storage/filesystem.ts +64 -14
- package/src/storage/memory.ts +30 -11
- package/src/storage/prisma.ts +415 -93
- package/src/storage/sequelize.ts +120 -44
- package/src/storage/types.ts +31 -5
- package/src/utils/document-helpers.ts +20 -0
- package/src/utils/graphql.ts +2 -1
- package/src/utils/index.ts +34 -27
- package/src/utils/logger.ts +44 -0
- package/src/server/listener/decorator.ts +0 -28
package/LICENSE
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "document-drive",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.71",
|
|
4
4
|
"license": "AGPL-3.0-only",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./src/index.ts",
|
|
@@ -13,8 +13,13 @@
|
|
|
13
13
|
"./storage/filesystem": "./src/storage/filesystem.ts",
|
|
14
14
|
"./storage/memory": "./src/storage/memory.ts",
|
|
15
15
|
"./storage/prisma": "./src/storage/prisma.ts",
|
|
16
|
+
"./cache/redis": "./src/cache/redis.ts",
|
|
17
|
+
"./cache/memory": "./src/cache/memory.ts",
|
|
18
|
+
"./queue/redis": "./src/queue/redis.ts",
|
|
19
|
+
"./queue/base": "./src/queue/base.ts",
|
|
16
20
|
"./utils": "./src/utils/index.ts",
|
|
17
|
-
"./utils/graphql": "./src/utils/graphql.ts"
|
|
21
|
+
"./utils/graphql": "./src/utils/graphql.ts",
|
|
22
|
+
"./logger": "./src/utils/logger.ts"
|
|
18
23
|
},
|
|
19
24
|
"files": [
|
|
20
25
|
"./src"
|
|
@@ -29,44 +34,52 @@
|
|
|
29
34
|
"test:watch": "vitest watch"
|
|
30
35
|
},
|
|
31
36
|
"peerDependencies": {
|
|
32
|
-
"
|
|
33
|
-
"document-model": "^1.0
|
|
34
|
-
|
|
37
|
+
"document-model": "^1.4.0",
|
|
38
|
+
"document-model-libs": "^1.57.0"
|
|
39
|
+
},
|
|
40
|
+
"optionalDependencies": {
|
|
41
|
+
"@prisma/client": "5.14.0",
|
|
35
42
|
"localforage": "^1.10.0",
|
|
43
|
+
"redis": "^4.6.13",
|
|
36
44
|
"sequelize": "^6.35.2",
|
|
37
45
|
"sqlite3": "^5.1.7"
|
|
38
46
|
},
|
|
39
47
|
"dependencies": {
|
|
48
|
+
"exponential-backoff": "^3.1.1",
|
|
40
49
|
"graphql": "^16.8.1",
|
|
41
50
|
"graphql-request": "^6.1.0",
|
|
42
51
|
"json-stringify-deterministic": "^1.0.12",
|
|
43
52
|
"nanoevents": "^9.0.0",
|
|
44
|
-
"sanitize-filename": "^1.6.3"
|
|
53
|
+
"sanitize-filename": "^1.6.3",
|
|
54
|
+
"uuid": "^9.0.1"
|
|
45
55
|
},
|
|
46
56
|
"devDependencies": {
|
|
47
57
|
"@commitlint/cli": "^18.6.1",
|
|
48
|
-
"@commitlint/config-conventional": "^18.6.
|
|
49
|
-
"@prisma/client": "5.
|
|
58
|
+
"@commitlint/config-conventional": "^18.6.3",
|
|
59
|
+
"@prisma/client": "5.11.0",
|
|
50
60
|
"@semantic-release/changelog": "^6.0.3",
|
|
51
61
|
"@semantic-release/git": "^10.0.1",
|
|
52
62
|
"@total-typescript/ts-reset": "^0.5.1",
|
|
53
|
-
"@types/node": "^20.
|
|
54
|
-
"@
|
|
55
|
-
"@typescript-eslint/
|
|
56
|
-
"@
|
|
57
|
-
"
|
|
58
|
-
"document-model
|
|
59
|
-
"
|
|
63
|
+
"@types/node": "^20.12.7",
|
|
64
|
+
"@types/uuid": "^9.0.8",
|
|
65
|
+
"@typescript-eslint/eslint-plugin": "^6.21.0",
|
|
66
|
+
"@typescript-eslint/parser": "^6.21.0",
|
|
67
|
+
"@vitest/coverage-v8": "^1.4.0",
|
|
68
|
+
"document-model": "^1.4.0",
|
|
69
|
+
"document-model-libs": "^1.57.0",
|
|
70
|
+
"eslint": "^8.57.0",
|
|
60
71
|
"eslint-config-prettier": "^9.1.0",
|
|
61
|
-
"fake-indexeddb": "^5.0.
|
|
72
|
+
"fake-indexeddb": "^5.0.2",
|
|
62
73
|
"localforage": "^1.10.0",
|
|
63
|
-
"msw": "^2.
|
|
64
|
-
"prettier": "^3.
|
|
74
|
+
"msw": "^2.2.13",
|
|
75
|
+
"prettier": "^3.2.5",
|
|
65
76
|
"prettier-plugin-organize-imports": "^3.2.4",
|
|
66
|
-
"
|
|
67
|
-
"
|
|
77
|
+
"prisma": "^5.14.0",
|
|
78
|
+
"semantic-release": "^23.0.8",
|
|
79
|
+
"sequelize": "^6.37.2",
|
|
68
80
|
"sqlite3": "^5.1.7",
|
|
69
|
-
"typescript": "^5.
|
|
70
|
-
"vitest": "^1.
|
|
71
|
-
}
|
|
81
|
+
"typescript": "^5.4.4",
|
|
82
|
+
"vitest": "^1.6.0"
|
|
83
|
+
},
|
|
84
|
+
"packageManager": "pnpm@9.1.4+sha256.30a1801ac4e723779efed13a21f4c39f9eb6c9fbb4ced101bce06b422593d7c9"
|
|
72
85
|
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Document } from "document-model/document";
|
|
2
|
+
import { ICache } from "./types";
|
|
3
|
+
|
|
4
|
+
class InMemoryCache implements ICache {
|
|
5
|
+
private cache = new Map<string, Map<string, Document>>();
|
|
6
|
+
|
|
7
|
+
async setDocument(drive: string, id: string, document: Document) {
|
|
8
|
+
if (!this.cache.has(drive)) {
|
|
9
|
+
this.cache.set(drive, new Map());
|
|
10
|
+
}
|
|
11
|
+
this.cache.get(drive)?.set(id, document);
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async deleteDocument(drive: string, id: string) {
|
|
16
|
+
return this.cache.get(drive)?.delete(id) ?? false;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async getDocument(drive: string, id: string) {
|
|
20
|
+
return this.cache.get(drive)?.get(id);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export default InMemoryCache;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Document } from "document-model/document";
|
|
2
|
+
import { ICache } from "./types";
|
|
3
|
+
import type { RedisClientType } from "redis";
|
|
4
|
+
import { logger } from "../utils/logger";
|
|
5
|
+
|
|
6
|
+
class RedisCache implements ICache {
|
|
7
|
+
private redis: RedisClientType;
|
|
8
|
+
|
|
9
|
+
constructor(redis: RedisClientType) {
|
|
10
|
+
this.redis = redis;
|
|
11
|
+
this.redis.flushAll().catch(logger.error);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async setDocument(drive: string, id: string, document: Document) {
|
|
15
|
+
return (await this.redis.hSet(drive, id, JSON.stringify(document))) > 0;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async getDocument(drive: string, id: string) {
|
|
19
|
+
const doc = await this.redis.hGet(drive, id);
|
|
20
|
+
|
|
21
|
+
return doc ? JSON.parse(doc) as Document : undefined;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async deleteDocument(drive: string, id: string) {
|
|
25
|
+
return (await this.redis.hDel(drive, id)) > 0;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export default RedisCache;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Document } from "document-model/document";
|
|
2
|
+
|
|
3
|
+
export interface ICache {
|
|
4
|
+
setDocument(drive: string, id: string, document: Document): Promise<boolean>
|
|
5
|
+
getDocument(drive: string, id: string): Promise<Document | undefined>
|
|
6
|
+
|
|
7
|
+
// @returns — true if a document existed and has been removed, or false if the document is not cached.
|
|
8
|
+
deleteDocument(drive: string, id: string): Promise<boolean>
|
|
9
|
+
}
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { IJob, IJobQueue, IQueue, IQueueManager, IServerDelegate, JobId, Job, QueueEvents, isOperationJob } from "./types";
|
|
2
|
+
import { generateUUID } from "../utils";
|
|
3
|
+
import { createNanoEvents, Unsubscribe } from 'nanoevents';
|
|
4
|
+
import { Action } from "document-model/document";
|
|
5
|
+
import { AddFileInput, DeleteNodeInput } from "document-model-libs/document-drive";
|
|
6
|
+
|
|
7
|
+
export class MemoryQueue<T, R> implements IQueue<T, R> {
|
|
8
|
+
private id: string;
|
|
9
|
+
private blocked = false;
|
|
10
|
+
private deleted = false;
|
|
11
|
+
private items: IJob<T>[] = [];
|
|
12
|
+
private dependencies = new Array<IJob<Job>>();
|
|
13
|
+
|
|
14
|
+
constructor(id: string) {
|
|
15
|
+
this.id = id;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async setDeleted(deleted: boolean) {
|
|
19
|
+
this.deleted = deleted;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async isDeleted() {
|
|
23
|
+
return this.deleted;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async addJob(data: IJob<T>) {
|
|
27
|
+
this.items.push(data);
|
|
28
|
+
return Promise.resolve();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async getNextJob() {
|
|
32
|
+
const job = this.items.shift();
|
|
33
|
+
return Promise.resolve(job);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async amountOfJobs() {
|
|
37
|
+
return Promise.resolve(this.items.length);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
getId() {
|
|
41
|
+
return this.id;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async setBlocked(blocked: boolean) {
|
|
45
|
+
this.blocked = blocked;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async isBlocked() {
|
|
49
|
+
return this.blocked;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async getJobs() {
|
|
53
|
+
return this.items;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async addDependencies(job: IJob<Job>) {
|
|
57
|
+
if (!this.dependencies.find(j => j.jobId === job.jobId)) {
|
|
58
|
+
this.dependencies.push(job);
|
|
59
|
+
}
|
|
60
|
+
if (!this.isBlocked()) {
|
|
61
|
+
this.setBlocked(true);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async removeDependencies(job: IJob<Job>) {
|
|
66
|
+
this.dependencies = this.dependencies.filter((j) => j.jobId !== job.jobId && j.driveId !== job.driveId);
|
|
67
|
+
if (this.dependencies.length === 0) {
|
|
68
|
+
await this.setBlocked(false);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export class BaseQueueManager implements IQueueManager {
|
|
74
|
+
protected emitter = createNanoEvents<QueueEvents>();
|
|
75
|
+
protected ticker = 0;
|
|
76
|
+
protected queues: IJobQueue[] = [];
|
|
77
|
+
protected workers: number;
|
|
78
|
+
protected timeout: number;
|
|
79
|
+
private delegate: IServerDelegate | undefined;
|
|
80
|
+
|
|
81
|
+
constructor(workers = 3, timeout = 0) {
|
|
82
|
+
this.workers = workers;
|
|
83
|
+
this.timeout = timeout;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async init(delegate: IServerDelegate, onError: (error: Error) => void): Promise<void> {
|
|
87
|
+
this.delegate = delegate;
|
|
88
|
+
for (let i = 0; i < this.workers; i++) {
|
|
89
|
+
setTimeout(() => this.processNextJob.bind(this)().catch(onError), 100 * i);
|
|
90
|
+
}
|
|
91
|
+
return Promise.resolve()
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async addJob(job: Job): Promise<JobId> {
|
|
95
|
+
if (!this.delegate) {
|
|
96
|
+
throw new Error("No server delegate defined");
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const jobId = generateUUID();
|
|
100
|
+
const queue = this.getQueue(job.driveId, job.documentId);
|
|
101
|
+
|
|
102
|
+
if (await queue.isDeleted()) {
|
|
103
|
+
throw new Error("Queue is deleted")
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// checks if the job is for a document that doesn't exist in storage yet
|
|
107
|
+
const newDocument = job.documentId && !(await this.delegate.checkDocumentExists(job.driveId, job.documentId));
|
|
108
|
+
// if it is a new document and queue is not yet blocked then
|
|
109
|
+
// blocks it so the jobs are not processed until it's ready
|
|
110
|
+
if (newDocument && !(await queue.isBlocked())) {
|
|
111
|
+
await queue.setBlocked(true);
|
|
112
|
+
|
|
113
|
+
// checks if there any job in the queue adding the file and adds as dependency
|
|
114
|
+
const driveQueue = this.getQueue(job.driveId);
|
|
115
|
+
const jobs = await driveQueue.getJobs();
|
|
116
|
+
for (const driveJob of jobs) {
|
|
117
|
+
const actions = isOperationJob(driveJob) ? driveJob.operations : driveJob.actions;
|
|
118
|
+
const op = actions.find((j: Action) => {
|
|
119
|
+
const input = j.input as AddFileInput;
|
|
120
|
+
return j.type === "ADD_FILE" && input.id === job.documentId
|
|
121
|
+
})
|
|
122
|
+
if (op) {
|
|
123
|
+
await queue.addDependencies(driveJob);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// if it has ADD_FILE operations then adds the job as
|
|
129
|
+
// a dependency to the corresponding document queues
|
|
130
|
+
const actions = isOperationJob(job) ? job.operations : job.actions;
|
|
131
|
+
const addFileOps = actions.filter((j: Action) => j.type === "ADD_FILE");
|
|
132
|
+
for (const addFileOp of addFileOps) {
|
|
133
|
+
const input = addFileOp.input as AddFileInput;
|
|
134
|
+
const q = this.getQueue(job.driveId, input.id)
|
|
135
|
+
await q.addDependencies({ jobId, ...job });
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// remove document if operations contains delete_node
|
|
139
|
+
const removeFileOps = actions.filter((j: Action) => j.type === "DELETE_NODE");
|
|
140
|
+
for (const removeFileOp of removeFileOps) {
|
|
141
|
+
const input = removeFileOp.input as DeleteNodeInput;
|
|
142
|
+
const queue = this.getQueue(job.driveId, input.id);
|
|
143
|
+
await queue.setDeleted(true);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
await queue.addJob({ jobId, ...job });
|
|
147
|
+
|
|
148
|
+
return jobId;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
getQueue(driveId: string, documentId?: string) {
|
|
152
|
+
const queueId = this.getQueueId(driveId, documentId);
|
|
153
|
+
let queue = this.queues.find((q) => q.getId() === queueId);
|
|
154
|
+
|
|
155
|
+
if (!queue) {
|
|
156
|
+
queue = new MemoryQueue(queueId);
|
|
157
|
+
this.queues.push(queue);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return queue;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
removeQueue(driveId: string, documentId?: string) {
|
|
164
|
+
const queueId = this.getQueueId(driveId, documentId);
|
|
165
|
+
this.queues = this.queues.filter((q) => q.getId() !== queueId);
|
|
166
|
+
this.emit("queueRemoved", queueId)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
getQueueByIndex(index: number) {
|
|
170
|
+
const queue = this.queues[index];
|
|
171
|
+
if (queue) {
|
|
172
|
+
return queue;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
getQueues() {
|
|
179
|
+
return Object.keys(new Array(this.queues));
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
private retryNextJob() {
|
|
183
|
+
const retry = this.timeout === 0 && typeof setImmediate !== "undefined" ? setImmediate : (fn: () => void) => setTimeout(fn, this.timeout);
|
|
184
|
+
return retry(() => this.processNextJob());
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
private async processNextJob() {
|
|
188
|
+
if (!this.delegate) {
|
|
189
|
+
throw new Error("No server delegate defined");
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (this.queues.length === 0) {
|
|
193
|
+
this.retryNextJob();
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const queue = this.queues[this.ticker];
|
|
198
|
+
this.ticker = this.ticker === this.queues.length - 1 ? 0 : this.ticker + 1;
|
|
199
|
+
if (!queue) {
|
|
200
|
+
this.ticker = 0;
|
|
201
|
+
this.retryNextJob();
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const amountOfJobs = await queue.amountOfJobs();
|
|
206
|
+
if (amountOfJobs === 0) {
|
|
207
|
+
this.retryNextJob();
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const isBlocked = await queue.isBlocked();
|
|
212
|
+
if (isBlocked) {
|
|
213
|
+
this.retryNextJob();
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
await queue.setBlocked(true);
|
|
218
|
+
const nextJob = await queue.getNextJob();
|
|
219
|
+
if (!nextJob) {
|
|
220
|
+
this.retryNextJob();
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
try {
|
|
225
|
+
const result = await this.delegate.processJob(nextJob);
|
|
226
|
+
|
|
227
|
+
// unblock the document queues of each add_file operation
|
|
228
|
+
const actions = isOperationJob(nextJob) ? nextJob.operations : nextJob.actions;
|
|
229
|
+
const addFileActions = actions.filter((op) => op.type === "ADD_FILE");
|
|
230
|
+
if (addFileActions.length > 0) {
|
|
231
|
+
for (const addFile of addFileActions) {
|
|
232
|
+
const documentQueue = this.getQueue(nextJob.driveId, (addFile.input as AddFileInput).id);
|
|
233
|
+
await documentQueue.removeDependencies(nextJob);
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
this.emit("jobCompleted", nextJob, result);
|
|
237
|
+
} catch (e) {
|
|
238
|
+
this.emit("jobFailed", nextJob, e as Error);
|
|
239
|
+
} finally {
|
|
240
|
+
await queue.setBlocked(false);
|
|
241
|
+
await this.processNextJob();
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
protected emit<K extends keyof QueueEvents>(
|
|
246
|
+
event: K,
|
|
247
|
+
...args: Parameters<QueueEvents[K]>
|
|
248
|
+
) {
|
|
249
|
+
this.emitter.emit(event, ...args);
|
|
250
|
+
}
|
|
251
|
+
on<K extends keyof QueueEvents>(this: this, event: K, cb: QueueEvents[K]): Unsubscribe {
|
|
252
|
+
return this.emitter.on(event, cb);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
protected getQueueId(driveId: string, documentId?: string) {
|
|
256
|
+
return `${driveId}${documentId ? `:${documentId}` : ''}`;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { RedisClientType } from "redis";
|
|
2
|
+
import { IJob, IQueue, IQueueManager, IServerDelegate, OperationJob } from "./types";
|
|
3
|
+
import { BaseQueueManager } from "./base";
|
|
4
|
+
|
|
5
|
+
export class RedisQueue<T, R> implements IQueue<T, R> {
|
|
6
|
+
private id: string;
|
|
7
|
+
private client: RedisClientType;
|
|
8
|
+
|
|
9
|
+
constructor(id: string, client: RedisClientType) {
|
|
10
|
+
this.client = client;
|
|
11
|
+
this.id = id;
|
|
12
|
+
this.client.hSet("queues", id, "true");
|
|
13
|
+
this.client.hSet(this.id, "blocked", "false");
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async addJob(data: any) {
|
|
17
|
+
await this.client.lPush(this.id + "-jobs", JSON.stringify(data));
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async getNextJob() {
|
|
21
|
+
const job = await this.client.rPop(this.id + "-jobs");
|
|
22
|
+
if (!job) {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
return JSON.parse(job) as IJob<T>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async amountOfJobs() {
|
|
29
|
+
return this.client.lLen(this.id + "-jobs");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async setBlocked(blocked: boolean) {
|
|
33
|
+
if (blocked) {
|
|
34
|
+
await this.client.hSet(this.id, "blocked", "true");
|
|
35
|
+
} else {
|
|
36
|
+
await this.client.hSet(this.id, "blocked", "false");
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async isBlocked() {
|
|
41
|
+
const blockedResult = await this.client.hGet(this.id, "blocked");
|
|
42
|
+
if (blockedResult === "true") {
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
getId() {
|
|
50
|
+
return this.id;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async getJobs() {
|
|
54
|
+
const entries = await this.client.lRange(this.id + "-jobs", 0, -1)
|
|
55
|
+
return entries.map(e => JSON.parse(e) as IJob<T>);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async addDependencies(job: IJob<OperationJob>) {
|
|
59
|
+
if (await this.hasDependency(job)) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
await this.client.lPush(this.id + "-deps", JSON.stringify(job));
|
|
63
|
+
await this.setBlocked(true);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async hasDependency(job: IJob<OperationJob>) {
|
|
67
|
+
const deps = await this.client.lRange(this.id + "-deps", 0, -1);
|
|
68
|
+
return deps.some(d => d === JSON.stringify(job));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async removeDependencies(job: IJob<OperationJob>) {
|
|
72
|
+
await this.client.lRem(this.id + "-deps", 1, JSON.stringify(job));
|
|
73
|
+
const allDeps = await this.client.lLen(this.id + "-deps");
|
|
74
|
+
if (allDeps > 0) {
|
|
75
|
+
await this.setBlocked(true);
|
|
76
|
+
} else {
|
|
77
|
+
await this.setBlocked(false);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async isDeleted() {
|
|
82
|
+
const active = await this.client.hGet("queues", this.id);
|
|
83
|
+
return active === "false";
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async setDeleted(deleted: boolean) {
|
|
87
|
+
if (deleted) {
|
|
88
|
+
await this.client.hSet("queues", this.id, "false");
|
|
89
|
+
} else {
|
|
90
|
+
await this.client.hSet("queues", this.id, "true");
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export class RedisQueueManager extends BaseQueueManager implements IQueueManager {
|
|
96
|
+
|
|
97
|
+
private client: RedisClientType;
|
|
98
|
+
|
|
99
|
+
constructor(workers = 3, timeout = 0, client: RedisClientType) {
|
|
100
|
+
super(workers, timeout);
|
|
101
|
+
this.client = client;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async init(delegate: IServerDelegate, onError: (error: Error) => void): Promise<void> {
|
|
105
|
+
await super.init(delegate, onError);
|
|
106
|
+
const queues = await this.client.hGetAll("queues");
|
|
107
|
+
for (const queueId in queues) {
|
|
108
|
+
const active = await this.client.hGet("queues", queueId);
|
|
109
|
+
if (active === "true") {
|
|
110
|
+
this.queues.push(new RedisQueue(queueId, this.client));
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
getQueue(driveId: string, documentId?: string) {
|
|
116
|
+
const queueId = this.getQueueId(driveId, documentId);
|
|
117
|
+
let queue = this.queues.find((q) => q.getId() === queueId);
|
|
118
|
+
|
|
119
|
+
if (!queue) {
|
|
120
|
+
queue = new RedisQueue(queueId, this.client);
|
|
121
|
+
this.queues.push(queue);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return queue;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
removeQueue(driveId: string, documentId?: string | undefined): void {
|
|
128
|
+
super.removeQueue(driveId, documentId);
|
|
129
|
+
|
|
130
|
+
const queueId = this.getQueueId(driveId, documentId);
|
|
131
|
+
this.client.hDel("queues", queueId);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Action, Operation } from "document-model/document";
|
|
2
|
+
import { IOperationResult } from "../server";
|
|
3
|
+
import type { Unsubscribe } from "nanoevents";
|
|
4
|
+
|
|
5
|
+
export interface BaseJob {
|
|
6
|
+
driveId: string;
|
|
7
|
+
documentId?: string
|
|
8
|
+
actions?: Action[]
|
|
9
|
+
forceSync?: boolean
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface OperationJob extends BaseJob {
|
|
13
|
+
operations: Operation[]
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface ActionJob extends BaseJob {
|
|
17
|
+
actions: Action[]
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type Job = OperationJob | ActionJob;
|
|
21
|
+
|
|
22
|
+
export type JobId = string;
|
|
23
|
+
|
|
24
|
+
export interface QueueEvents {
|
|
25
|
+
jobCompleted: (job: IJob<Job>, result: IOperationResult) => void;
|
|
26
|
+
jobFailed: (job: IJob<Job>, error: Error) => void;
|
|
27
|
+
queueRemoved: (queueId: string) => void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface IServerDelegate {
|
|
31
|
+
checkDocumentExists: (driveId: string, documentId: string) => Promise<boolean>;
|
|
32
|
+
processJob: (job: Job) => Promise<IOperationResult>;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export interface IQueueManager {
|
|
36
|
+
addJob(job: Job): Promise<JobId>;
|
|
37
|
+
getQueue(driveId: string, document?: string): IQueue<Job, IOperationResult>;
|
|
38
|
+
removeQueue(driveId: string, documentId?: string): void;
|
|
39
|
+
getQueueByIndex(index: number): IQueue<Job, IOperationResult> | null;
|
|
40
|
+
getQueues(): string[];
|
|
41
|
+
init(delegate: IServerDelegate, onError: (error: Error) => void): Promise<void>;
|
|
42
|
+
on<K extends keyof QueueEvents>(
|
|
43
|
+
this: this,
|
|
44
|
+
event: K,
|
|
45
|
+
cb: QueueEvents[K]
|
|
46
|
+
): Unsubscribe;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export type IJob<T> = { jobId: JobId } & T;
|
|
50
|
+
|
|
51
|
+
export interface IQueue<T, R> {
|
|
52
|
+
addJob(data: IJob<T>): Promise<void>;
|
|
53
|
+
getNextJob(): Promise<IJob<T> | undefined>;
|
|
54
|
+
amountOfJobs(): Promise<number>;
|
|
55
|
+
getId(): string;
|
|
56
|
+
setBlocked(blocked: boolean): Promise<void>;
|
|
57
|
+
isBlocked(): Promise<boolean>;
|
|
58
|
+
isDeleted(): Promise<boolean>;
|
|
59
|
+
setDeleted(deleted: boolean): Promise<void>;
|
|
60
|
+
getJobs(): Promise<IJob<T>[]>;
|
|
61
|
+
addDependencies(job: IJob<Job>): Promise<void>;
|
|
62
|
+
removeDependencies(job: IJob<Job>): Promise<void>;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export type IJobQueue = IQueue<Job, IOperationResult>;
|
|
66
|
+
|
|
67
|
+
export function isOperationJob(job: Job): job is OperationJob {
|
|
68
|
+
return "operations" in job;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function isActionJob(job: Job): job is ActionJob {
|
|
72
|
+
return "actions" in job;
|
|
73
|
+
}
|
package/src/server/error.ts
CHANGED
|
@@ -16,3 +16,20 @@ export class OperationError extends Error {
|
|
|
16
16
|
this.operation = operation;
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
|
+
|
|
20
|
+
export class ConflictOperationError extends OperationError {
|
|
21
|
+
constructor(existingOperation: Operation, newOperation: Operation) {
|
|
22
|
+
super(
|
|
23
|
+
'CONFLICT',
|
|
24
|
+
newOperation,
|
|
25
|
+
`Conflicting operation on index ${newOperation.index}`,
|
|
26
|
+
{ existingOperation, newOperation }
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export class MissingOperationError extends OperationError {
|
|
32
|
+
constructor(index: number, operation: Operation) {
|
|
33
|
+
super('MISSING', operation, `Missing operation on index ${index}`);
|
|
34
|
+
}
|
|
35
|
+
}
|