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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
import { z } from "zod";
import {
EnqueueOptions,
getQueueClient,
Queue,
QueueClient,
QueueOptions,
} from "@karakeep/shared/queueing";
import { zRuleEngineEventSchema } from "@karakeep/shared/types/rules";
import { loadAllPlugins } from ".";
// Lazy client initialization - plugins are loaded on first access
// We cache the promise to ensure only one initialization happens even with concurrent calls
let clientPromise: Promise<QueueClient> | null = null;
function getClient(): Promise<QueueClient> {
if (!clientPromise) {
clientPromise = (async () => {
await loadAllPlugins();
return await getQueueClient();
})();
}
return clientPromise;
}
/**
* Creates a deferred queue that initializes lazily on first use.
* This allows the module to be imported without requiring plugins to be loaded.
*/
function createDeferredQueue<T>(name: string, options: QueueOptions): Queue<T> {
// Cache the promise to ensure only one queue is created even with concurrent calls
let queuePromise: Promise<Queue<T>> | null = null;
const ensureQueue = (): Promise<Queue<T>> => {
if (!queuePromise) {
queuePromise = (async () => {
const client = await getClient();
return client.createQueue<T>(name, options);
})();
}
return queuePromise;
};
return {
opts: options,
name: () => name,
ensureInit: async () => {
await ensureQueue();
},
async enqueue(payload: T, opts?: EnqueueOptions) {
return (await ensureQueue()).enqueue(payload, opts);
},
async stats() {
return (await ensureQueue()).stats();
},
async cancelAllNonRunning() {
const q = await ensureQueue();
return q.cancelAllNonRunning?.() ?? 0;
},
};
}
export async function prepareQueue() {
const client = await getClient();
await client.prepare();
}
export async function startQueue() {
const client = await getClient();
await client.start();
}
// Link Crawler
export const zCrawlLinkRequestSchema = z.object({
bookmarkId: z.string(),
runInference: z.boolean().optional(),
archiveFullPage: z.boolean().optional().default(false),
storePdf: z.boolean().optional().default(false),
});
export type ZCrawlLinkRequest = z.input<typeof zCrawlLinkRequestSchema>;
export const LinkCrawlerQueue = createDeferredQueue<ZCrawlLinkRequest>(
"link_crawler_queue",
{
defaultJobArgs: {
numRetries: 5,
},
keepFailedJobs: false,
},
);
// Inference Worker
export const zOpenAIRequestSchema = z.object({
bookmarkId: z.string(),
type: z.enum(["summarize", "tag"]).default("tag"),
});
export type ZOpenAIRequest = z.infer<typeof zOpenAIRequestSchema>;
export const OpenAIQueue = createDeferredQueue<ZOpenAIRequest>("openai_queue", {
defaultJobArgs: {
numRetries: 3,
},
keepFailedJobs: false,
});
// Search Indexing Worker
export const zSearchIndexingRequestSchema = z.object({
bookmarkId: z.string(),
type: z.enum(["index", "delete"]),
});
export type ZSearchIndexingRequest = z.infer<
typeof zSearchIndexingRequestSchema
>;
export const SearchIndexingQueue = createDeferredQueue<ZSearchIndexingRequest>(
"searching_indexing",
{
defaultJobArgs: {
numRetries: 5,
},
keepFailedJobs: false,
},
);
// Admin maintenance worker
export const zTidyAssetsRequestSchema = z.object({
cleanDanglingAssets: z.boolean().optional().default(false),
syncAssetMetadata: z.boolean().optional().default(false),
});
export type ZTidyAssetsRequest = z.infer<typeof zTidyAssetsRequestSchema>;
export const zAdminMaintenanceTaskSchema = z.discriminatedUnion("type", [
z.object({
type: z.literal("tidy_assets"),
args: zTidyAssetsRequestSchema,
}),
z.object({
type: z.literal("migrate_large_link_html"),
}),
]);
export type ZAdminMaintenanceTask = z.infer<typeof zAdminMaintenanceTaskSchema>;
export type ZAdminMaintenanceTaskType = ZAdminMaintenanceTask["type"];
export type ZAdminMaintenanceTidyAssetsTask = Extract<
ZAdminMaintenanceTask,
{ type: "tidy_assets" }
>;
export type ZAdminMaintenanceMigrateLargeLinkHtmlTask = Extract<
ZAdminMaintenanceTask,
{ type: "migrate_large_link_html" }
>;
export const AdminMaintenanceQueue = createDeferredQueue<ZAdminMaintenanceTask>(
"admin_maintenance_queue",
{
defaultJobArgs: {
numRetries: 1,
},
keepFailedJobs: false,
},
);
export async function triggerSearchReindex(
bookmarkId: string,
opts?: Omit<EnqueueOptions, "idempotencyKey">,
) {
await SearchIndexingQueue.enqueue(
{
bookmarkId,
type: "index",
},
{
...opts,
idempotencyKey: `index:${bookmarkId}`,
},
);
}
export const zvideoRequestSchema = z.object({
bookmarkId: z.string(),
url: z.string(),
});
export type ZVideoRequest = z.infer<typeof zvideoRequestSchema>;
export const VideoWorkerQueue = createDeferredQueue<ZVideoRequest>(
"video_queue",
{
defaultJobArgs: {
numRetries: 5,
},
keepFailedJobs: false,
},
);
// Feed Worker
export const zFeedRequestSchema = z.object({
feedId: z.string(),
});
export type ZFeedRequestSchema = z.infer<typeof zFeedRequestSchema>;
export const FeedQueue = createDeferredQueue<ZFeedRequestSchema>("feed_queue", {
defaultJobArgs: {
// One retry is enough for the feed queue given that it's periodic
numRetries: 1,
},
keepFailedJobs: false,
});
// Preprocess Assets
export const zAssetPreprocessingRequestSchema = z.object({
bookmarkId: z.string(),
fixMode: z.boolean().optional().default(false),
});
export type AssetPreprocessingRequest = z.infer<
typeof zAssetPreprocessingRequestSchema
>;
export const AssetPreprocessingQueue =
createDeferredQueue<AssetPreprocessingRequest>("asset_preprocessing_queue", {
defaultJobArgs: {
numRetries: 2,
},
keepFailedJobs: false,
});
// Webhook worker
export const zWebhookRequestSchema = z.object({
bookmarkId: z.string(),
operation: z.enum(["crawled", "created", "edited", "ai tagged", "deleted"]),
userId: z.string().optional(),
});
export type ZWebhookRequest = z.infer<typeof zWebhookRequestSchema>;
export const WebhookQueue = createDeferredQueue<ZWebhookRequest>(
"webhook_queue",
{
defaultJobArgs: {
numRetries: 3,
},
keepFailedJobs: false,
},
);
export async function triggerWebhook(
bookmarkId: string,
operation: ZWebhookRequest["operation"],
userId?: string,
opts?: EnqueueOptions,
) {
await WebhookQueue.enqueue(
{
bookmarkId,
userId,
operation,
},
opts,
);
}
// RuleEngine worker
export const zRuleEngineRequestSchema = z.object({
bookmarkId: z.string(),
events: z.array(zRuleEngineEventSchema),
});
export type ZRuleEngineRequest = z.infer<typeof zRuleEngineRequestSchema>;
export const RuleEngineQueue = createDeferredQueue<ZRuleEngineRequest>(
"rule_engine_queue",
{
defaultJobArgs: {
numRetries: 1,
},
keepFailedJobs: false,
},
);
export async function triggerRuleEngineOnEvent(
bookmarkId: string,
events: z.infer<typeof zRuleEngineEventSchema>[],
opts?: EnqueueOptions,
) {
await RuleEngineQueue.enqueue(
{
events,
bookmarkId,
},
opts,
);
}
// Backup worker
export const zBackupRequestSchema = z.object({
userId: z.string(),
backupId: z.string().optional(),
});
export type ZBackupRequest = z.infer<typeof zBackupRequestSchema>;
export const BackupQueue = createDeferredQueue<ZBackupRequest>("backup_queue", {
defaultJobArgs: {
numRetries: 2,
},
keepFailedJobs: false,
});
|