1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
import path from "node:path";
import {
buildDBClient,
SqliteQueue as LQ,
Runner as LQRunner,
migrateDB,
} from "liteque";
import type { PluginProvider } from "@karakeep/shared/plugins";
import type {
DequeuedJob,
DequeuedJobError,
EnqueueOptions,
Queue,
QueueClient,
QueueOptions,
Runner,
RunnerFuncs,
RunnerOptions,
} from "@karakeep/shared/queueing";
import serverConfig from "@karakeep/shared/config";
class LitequeQueueWrapper<T> implements Queue<T> {
constructor(
private readonly _name: string,
private readonly lq: LQ<T>,
) {}
name(): string {
return this._name;
}
async enqueue(
payload: T,
options?: EnqueueOptions,
): Promise<string | undefined> {
const job = await this.lq.enqueue(payload, options);
// liteque returns a Job with numeric id
return job ? String(job.id) : undefined;
}
async stats() {
return this.lq.stats();
}
async cancelAllNonRunning(): Promise<number> {
return this.lq.cancelAllNonRunning();
}
// Internal accessor for runner
get _impl(): LQ<T> {
return this.lq;
}
}
class LitequeQueueClient implements QueueClient {
private db = buildDBClient(path.join(serverConfig.dataDir, "queue.db"), {
walEnabled: serverConfig.database.walMode,
});
private queues = new Map<string, LitequeQueueWrapper<unknown>>();
async init(): Promise<void> {
migrateDB(this.db);
}
createQueue<T>(name: string, options: QueueOptions): Queue<T> {
if (this.queues.has(name)) {
throw new Error(`Queue ${name} already exists`);
}
const lq = new LQ<T>(name, this.db, {
defaultJobArgs: { numRetries: options.defaultJobArgs.numRetries },
keepFailedJobs: options.keepFailedJobs,
});
const wrapper = new LitequeQueueWrapper<T>(name, lq);
this.queues.set(name, wrapper);
return wrapper;
}
createRunner<T>(
queue: Queue<T>,
funcs: RunnerFuncs<T>,
opts: RunnerOptions<T>,
): Runner<T> {
const name = queue.name();
let wrapper = this.queues.get(name);
if (!wrapper) {
throw new Error(`Queue ${name} not found`);
}
const runner = new LQRunner<T>(
wrapper._impl,
{
run: funcs.run,
onComplete: funcs.onComplete as
| ((job: DequeuedJob<T>) => Promise<void>)
| undefined,
onError: funcs.onError as
| ((job: DequeuedJobError<T>) => Promise<void>)
| undefined,
},
{
pollIntervalMs: opts.pollIntervalMs ?? 1000,
timeoutSecs: opts.timeoutSecs,
concurrency: opts.concurrency,
validator: opts.validator,
},
);
return {
run: () => runner.run(),
stop: () => runner.stop(),
runUntilEmpty: () => runner.runUntilEmpty(),
};
}
async shutdown(): Promise<void> {
// No-op for sqlite
}
}
export class LitequeQueueProvider implements PluginProvider<QueueClient> {
private client: QueueClient | null = null;
async getClient(): Promise<QueueClient | null> {
if (!this.client) {
const client = new LitequeQueueClient();
this.client = client;
}
return this.client;
}
}
|