@maydotinc/q-studio 0.1.0 → 0.8.0

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.
@@ -1,2490 +0,0 @@
1
- // src/core/queue-manager.ts
2
- import { FlowProducer } from "bullmq";
3
- import { LRUCache } from "lru-cache";
4
- var QueueManager = class {
5
- queues = /* @__PURE__ */ new Map();
6
- queueGroupByName = /* @__PURE__ */ new Map();
7
- queueGroups = [];
8
- tagFields = [];
9
- flowProducer = null;
10
- // LRU cache for expensive operations
11
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
12
- cache = new LRUCache({
13
- max: 100,
14
- // Max 100 entries
15
- ttl: 1e3 * 60,
16
- // Default 1 minute TTL
17
- allowStale: false,
18
- // Don't return stale entries
19
- updateAgeOnGet: true
20
- // Reset TTL on access
21
- });
22
- CACHE_TTL = {
23
- metrics: 5 * 60 * 1e3,
24
- // 5 minutes - metrics are expensive
25
- overview: 2 * 60 * 1e3,
26
- // 2 minutes
27
- queues: 2 * 60 * 1e3,
28
- // 2 minutes
29
- flows: 2 * 60 * 1e3,
30
- // 2 minutes
31
- activity: 5 * 60 * 1e3
32
- // 5 minutes - activity timeline
33
- };
34
- constructor(queues, tagFields = [], queueGroups = []) {
35
- for (const queue of queues) {
36
- this.queues.set(queue.name, queue);
37
- }
38
- this.queueGroups = queueGroups.map((group) => ({
39
- name: group.name,
40
- queues: group.queues.map((queue) => queue.name)
41
- }));
42
- for (const group of this.queueGroups) {
43
- for (const queueName of group.queues) {
44
- this.queueGroupByName.set(queueName, group.name);
45
- }
46
- }
47
- this.tagFields = tagFields;
48
- const firstQueue = queues[0];
49
- if (firstQueue) {
50
- const connection = firstQueue.opts?.connection;
51
- if (connection) {
52
- this.flowProducer = new FlowProducer({ connection });
53
- }
54
- }
55
- }
56
- /**
57
- * Get cached value or compute and cache
58
- */
59
- async cached(key, ttl, compute) {
60
- const cached = this.cache.get(key);
61
- if (cached !== void 0) {
62
- return cached;
63
- }
64
- const data = await compute();
65
- this.cache.set(key, data, { ttl });
66
- return data;
67
- }
68
- /**
69
- * Execute a promise with a timeout
70
- */
71
- async withTimeout(promise, timeoutMs, errorMessage) {
72
- let timeoutId;
73
- const timeoutPromise = new Promise((_, reject) => {
74
- timeoutId = setTimeout(() => reject(new Error(errorMessage)), timeoutMs);
75
- });
76
- try {
77
- return await Promise.race([promise, timeoutPromise]);
78
- } finally {
79
- clearTimeout(timeoutId);
80
- }
81
- }
82
- /**
83
- * Get jobs by time range using Redis sorted sets (ZRANGEBYSCORE)
84
- * This is more efficient than fetching all jobs and filtering in memory
85
- */
86
- async getJobsByTimeRange(queue, status, startTime, endTime, limit) {
87
- try {
88
- const client = queue.client;
89
- if (!client) {
90
- const jobs2 = await queue.getJobs([status], 0, limit * 2);
91
- return jobs2.filter(
92
- (job) => job.finishedOn && job.finishedOn >= startTime && job.finishedOn <= endTime
93
- );
94
- }
95
- const queueKey = `bull:${queue.name}:${status}`;
96
- const jobIds = await client.zrangebyscore(
97
- queueKey,
98
- startTime,
99
- endTime,
100
- "LIMIT",
101
- 0,
102
- limit
103
- );
104
- if (!jobIds || jobIds.length === 0) {
105
- return [];
106
- }
107
- const jobPromises = jobIds.map((jobId) => queue.getJob(jobId));
108
- const jobs = await Promise.all(jobPromises);
109
- return jobs.filter(
110
- (job) => job !== null && job !== void 0
111
- );
112
- } catch (_error) {
113
- const jobs = await queue.getJobs([status], 0, limit * 2);
114
- return jobs.filter(
115
- (job) => job.finishedOn && job.finishedOn >= startTime && job.finishedOn <= endTime
116
- );
117
- }
118
- }
119
- /**
120
- * Cache for job state lookups to avoid repeated Redis calls
121
- */
122
- jobStateCache = new LRUCache({
123
- max: 1e3,
124
- ttl: 1e3 * 30
125
- // 30 second TTL - job states don't change frequently
126
- });
127
- /**
128
- * Cache for job counts to avoid repeated Redis calls
129
- * Short TTL since counts change frequently but are expensive to fetch
130
- */
131
- countCache = new LRUCache({
132
- max: 100,
133
- // Cache counts for up to 100 queues
134
- ttl: 1e3 * 5
135
- // 5 second TTL - counts change but not instantly
136
- });
137
- /**
138
- * Get job counts with caching
139
- */
140
- async getCachedJobCounts(queue) {
141
- const cacheKey = queue.name;
142
- const cached = this.countCache.get(cacheKey);
143
- if (cached !== void 0) {
144
- return cached;
145
- }
146
- const counts = await queue.getJobCounts();
147
- this.countCache.set(cacheKey, counts);
148
- return counts;
149
- }
150
- /**
151
- * Invalidate caches related to a job or queue
152
- */
153
- invalidateJobCache(queueName, jobId) {
154
- this.countCache.delete(queueName);
155
- if (jobId) {
156
- const stateCacheKey = `${queueName}:${jobId}`;
157
- this.jobStateCache.delete(stateCacheKey);
158
- }
159
- this.cache.delete("metrics");
160
- this.cache.delete("overview");
161
- this.cache.delete("activity");
162
- }
163
- /**
164
- * Clear cache (useful after mutations)
165
- */
166
- clearCache(prefix) {
167
- if (prefix) {
168
- for (const key of this.cache.keys()) {
169
- if (key.startsWith(prefix)) {
170
- this.cache.delete(key);
171
- }
172
- }
173
- } else {
174
- this.cache.clear();
175
- }
176
- }
177
- /**
178
- * Get quick job counts across all queues (lightweight, for smart polling)
179
- * Returns total counts per status - cached and very fast
180
- */
181
- async getQuickCounts() {
182
- return this.cached("quick-counts", 2e3, async () => {
183
- const totals = {
184
- waiting: 0,
185
- active: 0,
186
- completed: 0,
187
- failed: 0,
188
- delayed: 0,
189
- total: 0,
190
- timestamp: Date.now()
191
- };
192
- await Promise.all(
193
- Array.from(this.queues.values()).map(async (queue) => {
194
- const counts = await this.getCachedJobCounts(queue);
195
- totals.waiting += counts.waiting || 0;
196
- totals.active += counts.active || 0;
197
- totals.completed += counts.completed || 0;
198
- totals.failed += counts.failed || 0;
199
- totals.delayed += counts.delayed || 0;
200
- })
201
- );
202
- totals.total = totals.waiting + totals.active + totals.completed + totals.failed + totals.delayed;
203
- return totals;
204
- });
205
- }
206
- /**
207
- * Get configured tag field names
208
- */
209
- getTagFields() {
210
- return this.tagFields;
211
- }
212
- /**
213
- * Get just queue names (very fast, no Redis calls)
214
- * Used for sidebar initial render
215
- */
216
- getQueueNames() {
217
- return Array.from(this.queues.keys());
218
- }
219
- /**
220
- * Get configured queue groups (very fast, no Redis calls)
221
- */
222
- getQueueGroups() {
223
- return this.queueGroups;
224
- }
225
- /**
226
- * Get a queue by name
227
- */
228
- getQueue(name) {
229
- return this.queues.get(name);
230
- }
231
- /**
232
- * Get information for all queues (cached)
233
- */
234
- async getQueues() {
235
- return this.cached("queues", this.CACHE_TTL.queues, async () => {
236
- const queueEntries = Array.from(this.queues.entries());
237
- const results = await Promise.all(
238
- queueEntries.map(async ([name, queue]) => {
239
- const [counts, isPaused] = await Promise.all([
240
- this.getCachedJobCounts(queue),
241
- queue.isPaused()
242
- ]);
243
- return {
244
- name,
245
- group: this.queueGroupByName.get(name),
246
- counts: {
247
- waiting: counts.waiting || 0,
248
- active: counts.active || 0,
249
- completed: counts.completed || 0,
250
- failed: counts.failed || 0,
251
- delayed: counts.delayed || 0,
252
- paused: counts.paused || 0
253
- },
254
- isPaused
255
- };
256
- })
257
- );
258
- return results;
259
- });
260
- }
261
- /**
262
- * Get overview statistics (cached)
263
- */
264
- async getOverview() {
265
- return this.cached("overview", this.CACHE_TTL.overview, async () => {
266
- const queues = await this.getQueues();
267
- let totalJobs = 0;
268
- let activeJobs = 0;
269
- let failedJobs = 0;
270
- for (const queue of queues) {
271
- totalJobs += queue.counts.waiting + queue.counts.active + queue.counts.delayed;
272
- activeJobs += queue.counts.active;
273
- failedJobs += queue.counts.failed;
274
- }
275
- const completedToday = queues.reduce(
276
- (sum, q) => sum + q.counts.completed,
277
- 0
278
- );
279
- return {
280
- totalJobs,
281
- activeJobs,
282
- failedJobs,
283
- completedToday,
284
- avgDuration: 0,
285
- // Would need metrics tracking
286
- queues
287
- };
288
- });
289
- }
290
- // ─────────────────────────────────────────────────────────────────────────────
291
- // Queue Control (Pause/Resume)
292
- // ─────────────────────────────────────────────────────────────────────────────
293
- /**
294
- * Pause a queue - stops processing new jobs
295
- */
296
- async pauseQueue(queueName) {
297
- const queue = this.queues.get(queueName);
298
- if (!queue) {
299
- throw new Error(`Queue "${queueName}" not found`);
300
- }
301
- await queue.pause();
302
- }
303
- /**
304
- * Resume a paused queue
305
- */
306
- async resumeQueue(queueName) {
307
- const queue = this.queues.get(queueName);
308
- if (!queue) {
309
- throw new Error(`Queue "${queueName}" not found`);
310
- }
311
- await queue.resume();
312
- }
313
- /**
314
- * Check if a queue is paused
315
- */
316
- async isQueuePaused(queueName) {
317
- const queue = this.queues.get(queueName);
318
- if (!queue) {
319
- throw new Error(`Queue "${queueName}" not found`);
320
- }
321
- return queue.isPaused();
322
- }
323
- // ─────────────────────────────────────────────────────────────────────────────
324
- // Metrics
325
- // ─────────────────────────────────────────────────────────────────────────────
326
- /**
327
- * Get metrics for the last 24 hours (cached - expensive operation)
328
- */
329
- async getMetrics() {
330
- return this.cached("metrics", this.CACHE_TTL.metrics, async () => {
331
- return this.withTimeout(
332
- (async () => {
333
- const now = Date.now();
334
- const twentyFourHoursAgo = now - 24 * 60 * 60 * 1e3;
335
- const createEmptyBuckets = () => {
336
- const buckets = [];
337
- const startHour = Math.floor(twentyFourHoursAgo / (60 * 60 * 1e3)) * (60 * 60 * 1e3);
338
- for (let i = 0; i < 24; i++) {
339
- buckets.push({
340
- hour: startHour + i * 60 * 60 * 1e3,
341
- completed: 0,
342
- failed: 0,
343
- avgDuration: 0,
344
- avgWaitTime: 0
345
- });
346
- }
347
- return buckets;
348
- };
349
- const queueMetricsMap = /* @__PURE__ */ new Map();
350
- for (const queueName of this.queues.keys()) {
351
- queueMetricsMap.set(queueName, {
352
- buckets: createEmptyBuckets(),
353
- durations: Array.from({ length: 24 }, () => []),
354
- waitTimes: Array.from({ length: 24 }, () => [])
355
- });
356
- }
357
- const allJobs = [];
358
- const jobTypeStats = /* @__PURE__ */ new Map();
359
- const queueEntries = Array.from(this.queues.entries());
360
- const queueChecks = await Promise.all(
361
- queueEntries.map(async ([queueName, queue]) => {
362
- const counts = await this.getCachedJobCounts(queue);
363
- return {
364
- queueName,
365
- queue,
366
- hasRelevantJobs: (counts.completed || 0) > 0 || (counts.failed || 0) > 0
367
- };
368
- })
369
- );
370
- const relevantQueues = queueChecks.filter((q) => q.hasRelevantJobs);
371
- const queueResults = await Promise.all(
372
- relevantQueues.map(async ({ queueName, queue }) => {
373
- const [completedJobs, failedJobs] = await Promise.all([
374
- this.getJobsByTimeRange(
375
- queue,
376
- "completed",
377
- twentyFourHoursAgo,
378
- now,
379
- 100
380
- // Reduced from 200 - only recent jobs needed
381
- ),
382
- this.getJobsByTimeRange(
383
- queue,
384
- "failed",
385
- twentyFourHoursAgo,
386
- now,
387
- 100
388
- // Reduced from 200 - only recent jobs needed
389
- )
390
- ]);
391
- return { queueName, completedJobs, failedJobs };
392
- })
393
- );
394
- for (const { queueName, completedJobs, failedJobs } of queueResults) {
395
- const metrics = queueMetricsMap.get(queueName);
396
- for (const job of completedJobs) {
397
- if (!job?.finishedOn || job.finishedOn < twentyFourHoursAgo)
398
- continue;
399
- const bucketIndex = Math.floor(
400
- (job.finishedOn - (metrics.buckets[0]?.hour || 0)) / (60 * 60 * 1e3)
401
- );
402
- if (bucketIndex >= 0 && bucketIndex < 24) {
403
- metrics.buckets[bucketIndex].completed++;
404
- const duration = job.processedOn ? job.finishedOn - job.processedOn : 0;
405
- const waitTime = job.processedOn ? job.processedOn - job.timestamp : 0;
406
- if (duration > 0) {
407
- metrics.durations[bucketIndex].push(duration);
408
- allJobs.push({
409
- name: job.name,
410
- queueName,
411
- duration,
412
- jobId: job.id || ""
413
- });
414
- }
415
- if (waitTime > 0) {
416
- metrics.waitTimes[bucketIndex].push(waitTime);
417
- }
418
- }
419
- const key = `${queueName}:${job.name}`;
420
- const stats = jobTypeStats.get(key) || {
421
- name: job.name,
422
- queueName,
423
- completed: 0,
424
- failed: 0
425
- };
426
- stats.completed++;
427
- jobTypeStats.set(key, stats);
428
- }
429
- for (const job of failedJobs) {
430
- if (!job?.finishedOn || job.finishedOn < twentyFourHoursAgo)
431
- continue;
432
- const bucketIndex = Math.floor(
433
- (job.finishedOn - (metrics.buckets[0]?.hour || 0)) / (60 * 60 * 1e3)
434
- );
435
- if (bucketIndex >= 0 && bucketIndex < 24) {
436
- metrics.buckets[bucketIndex].failed++;
437
- }
438
- const key = `${queueName}:${job.name}`;
439
- const stats = jobTypeStats.get(key) || {
440
- name: job.name,
441
- queueName,
442
- completed: 0,
443
- failed: 0
444
- };
445
- stats.failed++;
446
- jobTypeStats.set(key, stats);
447
- }
448
- }
449
- for (const metrics of queueMetricsMap.values()) {
450
- for (let i = 0; i < 24; i++) {
451
- const durations = metrics.durations[i];
452
- const waitTimes = metrics.waitTimes[i];
453
- if (durations.length > 0) {
454
- metrics.buckets[i].avgDuration = Math.round(
455
- durations.reduce((a, b) => a + b, 0) / durations.length
456
- );
457
- }
458
- if (waitTimes.length > 0) {
459
- metrics.buckets[i].avgWaitTime = Math.round(
460
- waitTimes.reduce((a, b) => a + b, 0) / waitTimes.length
461
- );
462
- }
463
- }
464
- }
465
- const aggregateBuckets = createEmptyBuckets();
466
- const aggregateDurations = Array.from(
467
- { length: 24 },
468
- () => []
469
- );
470
- const aggregateWaitTimes = Array.from(
471
- { length: 24 },
472
- () => []
473
- );
474
- for (const metrics of queueMetricsMap.values()) {
475
- for (let i = 0; i < 24; i++) {
476
- aggregateBuckets[i].completed += metrics.buckets[i].completed;
477
- aggregateBuckets[i].failed += metrics.buckets[i].failed;
478
- aggregateDurations[i].push(...metrics.durations[i]);
479
- aggregateWaitTimes[i].push(...metrics.waitTimes[i]);
480
- }
481
- }
482
- for (let i = 0; i < 24; i++) {
483
- if (aggregateDurations[i].length > 0) {
484
- aggregateBuckets[i].avgDuration = Math.round(
485
- aggregateDurations[i].reduce((a, b) => a + b, 0) / aggregateDurations[i].length
486
- );
487
- }
488
- if (aggregateWaitTimes[i].length > 0) {
489
- aggregateBuckets[i].avgWaitTime = Math.round(
490
- aggregateWaitTimes[i].reduce((a, b) => a + b, 0) / aggregateWaitTimes[i].length
491
- );
492
- }
493
- }
494
- const totalCompleted = aggregateBuckets.reduce(
495
- (sum, b) => sum + b.completed,
496
- 0
497
- );
498
- const totalFailed = aggregateBuckets.reduce(
499
- (sum, b) => sum + b.failed,
500
- 0
501
- );
502
- const allDurations = aggregateDurations.flat();
503
- const allWaitTimes = aggregateWaitTimes.flat();
504
- const slowestJobs = allJobs.sort((a, b) => b.duration - a.duration).slice(0, 10);
505
- const mostFailingTypes = Array.from(jobTypeStats.values()).filter((s) => s.failed > 0).map((s) => ({
506
- name: s.name,
507
- queueName: s.queueName,
508
- failCount: s.failed,
509
- totalCount: s.completed + s.failed,
510
- errorRate: s.failed / (s.completed + s.failed)
511
- })).sort((a, b) => b.failCount - a.failCount).slice(0, 10);
512
- return {
513
- queues: [],
514
- // Empty - per-queue metrics not used by frontend
515
- aggregate: {
516
- queueName: "all",
517
- buckets: aggregateBuckets,
518
- summary: {
519
- totalCompleted,
520
- totalFailed,
521
- errorRate: totalCompleted + totalFailed > 0 ? totalFailed / (totalCompleted + totalFailed) : 0,
522
- avgDuration: allDurations.length > 0 ? Math.round(
523
- allDurations.reduce((a, b) => a + b, 0) / allDurations.length
524
- ) : 0,
525
- avgWaitTime: allWaitTimes.length > 0 ? Math.round(
526
- allWaitTimes.reduce((a, b) => a + b, 0) / allWaitTimes.length
527
- ) : 0,
528
- throughputPerHour: Math.round(
529
- (totalCompleted + totalFailed) / 24
530
- )
531
- }
532
- },
533
- slowestJobs,
534
- mostFailingTypes,
535
- computedAt: now
536
- };
537
- })(),
538
- 45e3,
539
- // 45 second timeout (before proxy timeout)
540
- "Metrics computation timed out after 45 seconds"
541
- );
542
- });
543
- }
544
- /**
545
- * Get activity stats for the last 7 days (cached)
546
- * Returns 4-hour buckets for the activity timeline
547
- */
548
- async getActivityStats() {
549
- return this.cached("activity", this.CACHE_TTL.activity, async () => {
550
- const now = Date.now();
551
- const bucketSize = 4 * 60 * 60 * 1e3;
552
- const bucketCount = 42;
553
- const startDate = new Date(now);
554
- startDate.setHours(0, 0, 0, 0);
555
- startDate.setDate(startDate.getDate() - 6);
556
- const startTime = startDate.getTime();
557
- const buckets = [];
558
- for (let i = 0; i < bucketCount; i++) {
559
- buckets.push({
560
- time: startTime + i * bucketSize,
561
- completed: 0,
562
- failed: 0
563
- });
564
- }
565
- const queueEntries = Array.from(this.queues.entries());
566
- const queueChecks = await Promise.all(
567
- queueEntries.map(async ([, queue]) => {
568
- const counts = await this.getCachedJobCounts(queue);
569
- return {
570
- queue,
571
- hasRelevantJobs: (counts.completed || 0) > 0 || (counts.failed || 0) > 0
572
- };
573
- })
574
- );
575
- const relevantQueues = queueChecks.filter((q) => q.hasRelevantJobs);
576
- const queueResults = await Promise.all(
577
- relevantQueues.map(async ({ queue }) => {
578
- const [completedJobs, failedJobs] = await Promise.all([
579
- this.getJobsByTimeRange(
580
- queue,
581
- "completed",
582
- startTime,
583
- now,
584
- 200
585
- // Reduced from 500 - only jobs in time range needed
586
- ),
587
- this.getJobsByTimeRange(
588
- queue,
589
- "failed",
590
- startTime,
591
- now,
592
- 200
593
- // Reduced from 500 - only jobs in time range needed
594
- )
595
- ]);
596
- return { completedJobs, failedJobs };
597
- })
598
- );
599
- for (const { completedJobs, failedJobs } of queueResults) {
600
- for (const job of completedJobs) {
601
- if (!job?.finishedOn || job.finishedOn < startTime) continue;
602
- const bucketIndex = Math.floor(
603
- (job.finishedOn - startTime) / bucketSize
604
- );
605
- if (bucketIndex >= 0 && bucketIndex < bucketCount) {
606
- buckets[bucketIndex].completed++;
607
- }
608
- }
609
- for (const job of failedJobs) {
610
- if (!job?.finishedOn || job.finishedOn < startTime) continue;
611
- const bucketIndex = Math.floor(
612
- (job.finishedOn - startTime) / bucketSize
613
- );
614
- if (bucketIndex >= 0 && bucketIndex < bucketCount) {
615
- buckets[bucketIndex].failed++;
616
- }
617
- }
618
- }
619
- const totalCompleted = buckets.reduce((sum, b) => sum + b.completed, 0);
620
- const totalFailed = buckets.reduce((sum, b) => sum + b.failed, 0);
621
- return {
622
- buckets,
623
- startTime,
624
- endTime: now,
625
- bucketSize,
626
- totalCompleted,
627
- totalFailed,
628
- computedAt: now
629
- };
630
- });
631
- }
632
- /**
633
- * Get jobs for a specific queue with pagination and sorting
634
- */
635
- async getJobs(queueName, status, limit = 50, start = 0, sort) {
636
- const queue = this.queues.get(queueName);
637
- if (!queue) {
638
- return { data: [], total: 0, hasMore: false };
639
- }
640
- const types = status ? [status] : ["waiting", "active", "completed", "failed", "delayed"];
641
- const counts = await this.getCachedJobCounts(queue);
642
- const jobsWithState = [];
643
- let total = 0;
644
- for (const type of types) {
645
- const typeJobs = await queue.getJobs(type, start, start + limit);
646
- jobsWithState.push(
647
- ...typeJobs.map((job) => ({ job, state: type }))
648
- );
649
- const typeCount = counts[type] || 0;
650
- total += typeCount;
651
- }
652
- const jobInfos = await Promise.all(
653
- jobsWithState.map(({ job, state }) => this.jobToInfo(job, "full", state))
654
- );
655
- const sortField = sort?.field ?? "timestamp";
656
- const sortDir = sort?.direction === "asc" ? 1 : -1;
657
- jobInfos.sort((a, b) => {
658
- const aVal = this.getSortValue(a, sortField);
659
- const bVal = this.getSortValue(b, sortField);
660
- if (aVal < bVal) return -1 * sortDir;
661
- if (aVal > bVal) return 1 * sortDir;
662
- return 0;
663
- });
664
- const data = jobInfos.slice(0, limit);
665
- return {
666
- data,
667
- total,
668
- hasMore: start + limit < total,
669
- cursor: start + limit < total ? String(start + limit) : void 0
670
- };
671
- }
672
- /**
673
- * Get a single job by ID
674
- */
675
- async getJob(queueName, jobId) {
676
- const queue = this.queues.get(queueName);
677
- if (!queue) return null;
678
- const job = await queue.getJob(jobId);
679
- if (!job) return null;
680
- return this.jobToInfo(job, "full");
681
- }
682
- /**
683
- * Get BullMQ log lines for a job.
684
- */
685
- async getJobLogs(queueName, jobId, start = 0, end = 999, asc = true) {
686
- const queue = this.queues.get(queueName);
687
- if (!queue) return null;
688
- const job = await queue.getJob(jobId);
689
- if (!job) return null;
690
- const result = await queue.getJobLogs(jobId, start, end, asc);
691
- return {
692
- logs: result.logs,
693
- count: result.count,
694
- start,
695
- end
696
- };
697
- }
698
- /**
699
- * Retry a failed job
700
- */
701
- async retryJob(queueName, jobId) {
702
- const queue = this.queues.get(queueName);
703
- if (!queue) return false;
704
- const job = await queue.getJob(jobId);
705
- if (!job) return false;
706
- await job.retry();
707
- this.invalidateJobCache(queueName, jobId);
708
- return true;
709
- }
710
- /**
711
- * Remove a job
712
- */
713
- async removeJob(queueName, jobId) {
714
- const queue = this.queues.get(queueName);
715
- if (!queue) return false;
716
- const job = await queue.getJob(jobId);
717
- if (!job) return false;
718
- await job.remove();
719
- this.invalidateJobCache(queueName, jobId);
720
- return true;
721
- }
722
- /**
723
- * Promote a delayed job to waiting
724
- */
725
- async promoteJob(queueName, jobId) {
726
- const queue = this.queues.get(queueName);
727
- if (!queue) return false;
728
- const job = await queue.getJob(jobId);
729
- if (!job) return false;
730
- await job.promote();
731
- this.invalidateJobCache(queueName, jobId);
732
- return true;
733
- }
734
- /**
735
- * Parse search query for field:value filters
736
- * Returns { filters: { field: value }, text: remainingText }
737
- */
738
- parseSearchQuery(query) {
739
- const filters = {};
740
- const textParts = [];
741
- const len = query.length;
742
- let i = 0;
743
- while (i < len) {
744
- while (i < len && query[i] === " ") i++;
745
- if (i >= len) break;
746
- let j = i;
747
- while (j < len && /\w/.test(query[j])) j++;
748
- if (j > i && j < len && query[j] === ":") {
749
- const field = query.slice(i, j);
750
- j++;
751
- let value;
752
- if (j < len && query[j] === '"') {
753
- j++;
754
- const closeQuote = query.indexOf('"', j);
755
- if (closeQuote !== -1) {
756
- value = query.slice(j, closeQuote);
757
- j = closeQuote + 1;
758
- } else {
759
- value = query.slice(j);
760
- j = len;
761
- }
762
- } else {
763
- const valueStart = j;
764
- while (j < len && query[j] !== " ") j++;
765
- value = query.slice(valueStart, j);
766
- }
767
- if (value) {
768
- filters[field] = value;
769
- } else {
770
- textParts.push(`${field}:`);
771
- }
772
- i = j;
773
- } else {
774
- const start = i;
775
- while (i < len && query[i] !== " ") i++;
776
- textParts.push(query.slice(start, i));
777
- }
778
- }
779
- return {
780
- filters,
781
- text: textParts.filter(Boolean).join(" ")
782
- };
783
- }
784
- /**
785
- * Check if a raw job matches all provided filters (before conversion)
786
- * This is more efficient than converting to JobInfo first
787
- */
788
- jobMatchesAllFilters(job, filters) {
789
- if (filters.timeRange) {
790
- const jobTime = job.processedOn || job.finishedOn || job.timestamp || 0;
791
- if (jobTime < filters.timeRange.start || jobTime > filters.timeRange.end) {
792
- return false;
793
- }
794
- }
795
- if (filters.tags && Object.keys(filters.tags).length > 0) {
796
- if (!job.data || typeof job.data !== "object") {
797
- return false;
798
- }
799
- const dataObj = job.data;
800
- for (const [field, value] of Object.entries(filters.tags)) {
801
- const jobValue = dataObj[field];
802
- if (jobValue === void 0 || jobValue === null) {
803
- return false;
804
- }
805
- const strJobValue = String(jobValue).toLowerCase();
806
- const strFilterValue = value.toLowerCase();
807
- if (!strJobValue.includes(strFilterValue)) {
808
- return false;
809
- }
810
- }
811
- }
812
- if (filters.text) {
813
- const lowerText = filters.text.toLowerCase();
814
- const matchesId = job.id?.toLowerCase().includes(lowerText);
815
- const matchesName = job.name?.toLowerCase().includes(lowerText);
816
- if (!matchesId && !matchesName) {
817
- const stringifiedData = JSON.stringify(job.data).toLowerCase();
818
- if (!stringifiedData.includes(lowerText)) {
819
- return false;
820
- }
821
- }
822
- }
823
- return true;
824
- }
825
- /**
826
- * Check if a job matches the given tag filters
827
- */
828
- jobMatchesFilters(job, filters) {
829
- if (!job.data || typeof job.data !== "object") {
830
- return Object.keys(filters).length === 0;
831
- }
832
- const dataObj = job.data;
833
- for (const [field, value] of Object.entries(filters)) {
834
- const jobValue = dataObj[field];
835
- if (jobValue === void 0 || jobValue === null) {
836
- return false;
837
- }
838
- const strJobValue = String(jobValue).toLowerCase();
839
- const strFilterValue = value.toLowerCase();
840
- if (!strJobValue.includes(strFilterValue)) {
841
- return false;
842
- }
843
- }
844
- return true;
845
- }
846
- /**
847
- * Search jobs across all queues
848
- * Supports field:value syntax (e.g., "teamId:abc-123 invoice")
849
- * Optimized with parallel processing, early exits, and count checks
850
- */
851
- async search(query, limit = 20) {
852
- const { filters, text } = this.parseSearchQuery(query);
853
- const lowerText = text.toLowerCase();
854
- const hasFilters = Object.keys(filters).length > 0;
855
- const hasText = lowerText.length > 0;
856
- if (!hasFilters && !hasText) {
857
- return [];
858
- }
859
- const queueEntries = Array.from(this.queues.entries());
860
- const queueChecks = await Promise.all(
861
- queueEntries.map(async ([queueName, queue]) => {
862
- const counts = await this.getCachedJobCounts(queue);
863
- const hasJobs = (counts.waiting || 0) > 0 || (counts.active || 0) > 0 || (counts.completed || 0) > 0 || (counts.failed || 0) > 0 || (counts.delayed || 0) > 0;
864
- return { queueName, queue, hasJobs };
865
- })
866
- );
867
- const relevantQueues = queueChecks.filter((q) => q.hasJobs);
868
- if (relevantQueues.length === 0) {
869
- return [];
870
- }
871
- const types = ["waiting", "active", "completed", "failed", "delayed"];
872
- const fetchLimit = Math.min(limit * 2, 50);
873
- const stringifiedDataCache = /* @__PURE__ */ new WeakMap();
874
- const queueResults = await Promise.allSettled(
875
- relevantQueues.map(async ({ queueName, queue }) => {
876
- const typeResults = await Promise.all(
877
- types.map(async (type) => {
878
- try {
879
- const jobs = await queue.getJobs(type, 0, fetchLimit);
880
- const matches = [];
881
- for (const job of jobs) {
882
- if (hasFilters && !this.jobMatchesFilters(job, filters)) {
883
- continue;
884
- }
885
- if (hasText) {
886
- const matchesId = job.id?.toLowerCase().includes(lowerText);
887
- const matchesName = job.name?.toLowerCase().includes(lowerText);
888
- let matchesData = false;
889
- if (!matchesId && !matchesName) {
890
- let stringifiedData = stringifiedDataCache.get(job);
891
- if (!stringifiedData) {
892
- stringifiedData = JSON.stringify(job.data).toLowerCase();
893
- stringifiedDataCache.set(job, stringifiedData);
894
- }
895
- matchesData = stringifiedData.includes(lowerText);
896
- }
897
- if (!matchesId && !matchesName && !matchesData) {
898
- continue;
899
- }
900
- }
901
- matches.push({
902
- queue: queueName,
903
- job: await this.jobToInfo(
904
- job,
905
- "full",
906
- type
907
- )
908
- });
909
- }
910
- return matches;
911
- } catch {
912
- return [];
913
- }
914
- })
915
- );
916
- return typeResults.flat();
917
- })
918
- );
919
- const allMatches = [];
920
- for (const result of queueResults) {
921
- if (result.status === "fulfilled") {
922
- allMatches.push(...result.value);
923
- }
924
- }
925
- return allMatches.slice(0, limit);
926
- }
927
- /**
928
- * Clean jobs from a queue
929
- */
930
- async cleanJobs(queueName, status, grace = 0) {
931
- const queue = this.queues.get(queueName);
932
- if (!queue) return 0;
933
- const removed = await queue.clean(grace, 1e3, status);
934
- return removed.length;
935
- }
936
- /**
937
- * FAST PATH: Get latest runs without filters
938
- * Optimized for the common case of viewing newest jobs (timestamp desc, no filters)
939
- * - Single getJobs call per queue (not per status type)
940
- * - No count checks needed
941
- * - Minimal Redis round-trips
942
- */
943
- async getLatestRuns(limit, start) {
944
- const queueEntries = Array.from(this.queues.entries());
945
- const numQueues = queueEntries.length;
946
- if (numQueues === 0) {
947
- return { data: [], total: -1, hasMore: false, cursor: void 0 };
948
- }
949
- const perQueueFetch = Math.max(5, Math.ceil((limit + 10) / numQueues) + 2);
950
- const allTypes = [
951
- "waiting",
952
- "active",
953
- "completed",
954
- "failed",
955
- "delayed"
956
- ];
957
- const results = await Promise.all(
958
- queueEntries.map(async ([queueName, queue]) => {
959
- const jobs = await queue.getJobs(allTypes, 0, perQueueFetch);
960
- return jobs.map((job) => ({ job, queueName }));
961
- })
962
- );
963
- const allJobs = results.flat();
964
- allJobs.sort((a, b) => {
965
- const timeDiff = (b.job.timestamp || 0) - (a.job.timestamp || 0);
966
- if (timeDiff !== 0) return timeDiff;
967
- return a.queueName.localeCompare(b.queueName);
968
- });
969
- const jobsToConvert = allJobs.slice(start, start + limit);
970
- const runInfos = await Promise.all(
971
- jobsToConvert.map(async ({ job, queueName }) => {
972
- let state = "waiting";
973
- if (job.finishedOn) {
974
- state = job.failedReason ? "failed" : "completed";
975
- } else if (job.processedOn) {
976
- state = "active";
977
- } else if (job.delay && job.delay > 0) {
978
- state = "delayed";
979
- }
980
- const info = await this.jobToInfo(job, "list", state);
981
- return { ...info, queueName };
982
- })
983
- );
984
- const hasMore = allJobs.length > start + limit;
985
- return {
986
- data: runInfos,
987
- total: -1,
988
- // Don't calculate total for fast path - not needed for UI
989
- hasMore,
990
- cursor: hasMore ? String(start + runInfos.length) : void 0
991
- };
992
- }
993
- /**
994
- * Get all runs (jobs) across all queues with sorting and filtering
995
- * Uses fast path for common case (no filters, timestamp desc)
996
- */
997
- async getAllRuns(limit = 50, start = 0, sort, filters) {
998
- const sortField = sort?.field ?? "timestamp";
999
- const sortDir = sort?.direction === "asc" ? 1 : -1;
1000
- const hasFilters = !!(filters?.status || filters?.tags || filters?.text || filters?.timeRange);
1001
- const isTimestampSort = sortField === "timestamp";
1002
- if (!hasFilters && isTimestampSort && sortDir === -1) {
1003
- return this.getLatestRuns(limit, start);
1004
- }
1005
- const queueEntries = Array.from(this.queues.entries());
1006
- const types = filters?.status ? [filters.status] : ["waiting", "active", "completed", "failed", "delayed"];
1007
- const hasTimeRange = !!filters?.timeRange;
1008
- const numQueues = Math.max(queueEntries.length, 1);
1009
- if (queueEntries.length === 0) {
1010
- return {
1011
- data: [],
1012
- total: 0,
1013
- hasMore: false,
1014
- cursor: void 0
1015
- };
1016
- }
1017
- const baseFetchPerQueue = Math.max(
1018
- Math.ceil(limit * 2 / numQueues) + 3,
1019
- 5
1020
- );
1021
- let allJobs = [];
1022
- const fetchFromQueue = async (queueName, queue, fetchCount) => {
1023
- if (hasTimeRange && filters?.timeRange) {
1024
- const timeRangeJobs = [];
1025
- if (types.includes("completed")) {
1026
- const completedJobs = await this.getJobsByTimeRange(
1027
- queue,
1028
- "completed",
1029
- filters.timeRange.start,
1030
- filters.timeRange.end,
1031
- fetchCount
1032
- );
1033
- timeRangeJobs.push(
1034
- ...completedJobs.map((job) => ({
1035
- job,
1036
- state: "completed"
1037
- }))
1038
- );
1039
- }
1040
- if (types.includes("failed")) {
1041
- const failedJobs = await this.getJobsByTimeRange(
1042
- queue,
1043
- "failed",
1044
- filters.timeRange.start,
1045
- filters.timeRange.end,
1046
- fetchCount
1047
- );
1048
- timeRangeJobs.push(
1049
- ...failedJobs.map((job) => ({
1050
- job,
1051
- state: "failed"
1052
- }))
1053
- );
1054
- }
1055
- const otherTypes = types.filter(
1056
- (t) => t !== "completed" && t !== "failed"
1057
- );
1058
- if (otherTypes.length > 0) {
1059
- const otherJobArrays = await Promise.all(
1060
- otherTypes.map(async (type) => {
1061
- const jobs = await queue.getJobs(type, 0, fetchCount);
1062
- return jobs.map((job) => ({ job, state: type }));
1063
- })
1064
- );
1065
- timeRangeJobs.push(...otherJobArrays.flat());
1066
- }
1067
- return timeRangeJobs.map(({ job, state }) => ({
1068
- job,
1069
- queueName,
1070
- state
1071
- }));
1072
- }
1073
- if (filters?.status) {
1074
- const jobs = await queue.getJobs(filters.status, 0, fetchCount);
1075
- return jobs.map((job) => ({
1076
- job,
1077
- queueName,
1078
- state: filters.status
1079
- }));
1080
- }
1081
- const jobArrays = await Promise.all(
1082
- types.map(async (type) => {
1083
- const jobs = await queue.getJobs(type, 0, fetchCount);
1084
- return jobs.map((job) => ({ job, state: type }));
1085
- })
1086
- );
1087
- return jobArrays.flat().map(({ job, state }) => ({ job, queueName, state }));
1088
- };
1089
- const results = await Promise.all(
1090
- queueEntries.map(
1091
- ([queueName, queue]) => fetchFromQueue(queueName, queue, baseFetchPerQueue)
1092
- )
1093
- );
1094
- allJobs = results.flat();
1095
- if (filters) {
1096
- allJobs = allJobs.filter(
1097
- ({ job }) => this.jobMatchesAllFilters(job, filters)
1098
- );
1099
- }
1100
- if (isTimestampSort) {
1101
- allJobs.sort((a, b) => {
1102
- const aTime = a.job.timestamp || 0;
1103
- const bTime = b.job.timestamp || 0;
1104
- const timeDiff = sortDir === -1 ? bTime - aTime : aTime - bTime;
1105
- if (timeDiff !== 0) return timeDiff;
1106
- const queueDiff = a.queueName.localeCompare(b.queueName);
1107
- if (queueDiff !== 0) return queueDiff;
1108
- return (a.job.id || "").localeCompare(b.job.id || "");
1109
- });
1110
- }
1111
- const jobsToConvert = allJobs.slice(start, start + limit);
1112
- const runInfos = await Promise.all(
1113
- jobsToConvert.map(async ({ job, queueName, state }) => {
1114
- const info = await this.jobToInfo(job, "list", state);
1115
- return { ...info, queueName };
1116
- })
1117
- );
1118
- if (!isTimestampSort) {
1119
- runInfos.sort((a, b) => {
1120
- const aVal = this.getSortValueForList(a, sortField);
1121
- const bVal = this.getSortValueForList(b, sortField);
1122
- if (aVal < bVal) return -1 * sortDir;
1123
- if (aVal > bVal) return 1 * sortDir;
1124
- return 0;
1125
- });
1126
- }
1127
- const hasMore = allJobs.length > start + limit;
1128
- return {
1129
- data: runInfos,
1130
- total: -1,
1131
- // Don't calculate total - not needed for UI
1132
- hasMore,
1133
- cursor: hasMore ? String(start + runInfos.length) : void 0
1134
- };
1135
- }
1136
- /**
1137
- * Get all schedulers (repeatable and delayed jobs) with sorting
1138
- */
1139
- async getSchedulers(repeatableSort, delayedSort) {
1140
- const repeatable = [];
1141
- const delayed = [];
1142
- const queueEntries = Array.from(this.queues.entries());
1143
- const results = await Promise.all(
1144
- queueEntries.map(async ([queueName, queue]) => {
1145
- const [repeatableJobs, delayedJobs] = await Promise.all([
1146
- queue.getRepeatableJobs(),
1147
- queue.getJobs("delayed", 0, 50)
1148
- ]);
1149
- return { queueName, repeatableJobs, delayedJobs };
1150
- })
1151
- );
1152
- for (const { queueName, repeatableJobs, delayedJobs } of results) {
1153
- for (const job of repeatableJobs) {
1154
- repeatable.push({
1155
- key: job.key,
1156
- name: job.name || "unnamed",
1157
- queueName,
1158
- pattern: job.pattern ?? void 0,
1159
- every: job.every ? Number(job.every) : void 0,
1160
- next: job.next ?? void 0,
1161
- endDate: job.endDate ?? void 0,
1162
- tz: job.tz ?? void 0
1163
- });
1164
- }
1165
- for (const job of delayedJobs) {
1166
- const delay = job.opts.delay || 0;
1167
- delayed.push({
1168
- id: job.id || "",
1169
- name: job.name,
1170
- queueName,
1171
- delay,
1172
- processAt: job.timestamp + delay,
1173
- data: job.data
1174
- });
1175
- }
1176
- }
1177
- const repeatableField = repeatableSort?.field ?? "name";
1178
- const repeatableDir = repeatableSort?.direction === "desc" ? -1 : 1;
1179
- repeatable.sort((a, b) => {
1180
- const aVal = this.getSchedulerSortValue(a, repeatableField);
1181
- const bVal = this.getSchedulerSortValue(b, repeatableField);
1182
- if (aVal < bVal) return -1 * repeatableDir;
1183
- if (aVal > bVal) return 1 * repeatableDir;
1184
- return 0;
1185
- });
1186
- const delayedField = delayedSort?.field ?? "processAt";
1187
- const delayedDir = delayedSort?.direction === "desc" ? -1 : 1;
1188
- delayed.sort((a, b) => {
1189
- const aVal = this.getDelayedSortValue(a, delayedField);
1190
- const bVal = this.getDelayedSortValue(b, delayedField);
1191
- if (aVal < bVal) return -1 * delayedDir;
1192
- if (aVal > bVal) return 1 * delayedDir;
1193
- return 0;
1194
- });
1195
- return { repeatable, delayed };
1196
- }
1197
- /**
1198
- * Enqueue a new job (for testing)
1199
- */
1200
- async enqueueJob(request) {
1201
- const queue = this.queues.get(request.queueName);
1202
- if (!queue) {
1203
- throw new Error(`Queue "${request.queueName}" not found`);
1204
- }
1205
- const job = await queue.add(request.jobName, request.data, {
1206
- delay: request.opts?.delay,
1207
- priority: request.opts?.priority,
1208
- attempts: request.opts?.attempts
1209
- });
1210
- return { id: job.id || "" };
1211
- }
1212
- /**
1213
- * Extract tag values from job data based on configured tag fields
1214
- */
1215
- extractTags(data) {
1216
- if (!this.tagFields.length || !data || typeof data !== "object") {
1217
- return void 0;
1218
- }
1219
- const tags = {};
1220
- const dataObj = data;
1221
- for (const field of this.tagFields) {
1222
- const value = dataObj[field];
1223
- if (value !== void 0 && (typeof value === "string" || typeof value === "number" || typeof value === "boolean" || value === null)) {
1224
- tags[field] = value;
1225
- }
1226
- }
1227
- return Object.keys(tags).length > 0 ? tags : void 0;
1228
- }
1229
- /**
1230
- * Get unique values for a specific tag field across all jobs
1231
- */
1232
- async getTagValues(field, limit = 50) {
1233
- const valueMap = /* @__PURE__ */ new Map();
1234
- const types = ["waiting", "active", "completed", "failed", "delayed"];
1235
- const queueEntries = Array.from(this.queues.entries());
1236
- const queueResults = await Promise.all(
1237
- queueEntries.map(async ([, queue]) => {
1238
- const jobArrays = await Promise.all(
1239
- types.map((type) => queue.getJobs(type, 0, 100))
1240
- );
1241
- return jobArrays.flat();
1242
- })
1243
- );
1244
- for (const jobs of queueResults) {
1245
- for (const job of jobs) {
1246
- if (job.data && typeof job.data === "object") {
1247
- const dataObj = job.data;
1248
- const value = dataObj[field];
1249
- if (value !== void 0 && value !== null) {
1250
- const strValue = String(value);
1251
- valueMap.set(strValue, (valueMap.get(strValue) || 0) + 1);
1252
- }
1253
- }
1254
- }
1255
- }
1256
- const sorted = Array.from(valueMap.entries()).sort((a, b) => b[1] - a[1]).slice(0, limit).map(([value, count]) => ({ value, count }));
1257
- return sorted;
1258
- }
1259
- /**
1260
- * Get sortable value from JobInfo/RunInfo
1261
- */
1262
- getSortValue(item, field) {
1263
- switch (field) {
1264
- case "timestamp":
1265
- return item.timestamp ?? 0;
1266
- case "name":
1267
- return item.name.toLowerCase();
1268
- case "status":
1269
- return item.status;
1270
- case "duration":
1271
- return item.duration ?? 0;
1272
- case "queueName":
1273
- return "queueName" in item ? item.queueName.toLowerCase() : "";
1274
- case "processedOn":
1275
- return item.processedOn ?? 0;
1276
- default:
1277
- return item.timestamp ?? 0;
1278
- }
1279
- }
1280
- /**
1281
- * Get sortable value from RunInfoList (lightweight version)
1282
- */
1283
- getSortValueForList(item, field) {
1284
- switch (field) {
1285
- case "timestamp":
1286
- return item.timestamp ?? 0;
1287
- case "name":
1288
- return item.name.toLowerCase();
1289
- case "status":
1290
- return item.status;
1291
- case "duration":
1292
- return item.duration ?? 0;
1293
- case "queueName":
1294
- return item.queueName.toLowerCase();
1295
- case "processedOn":
1296
- return item.processedOn ?? 0;
1297
- default:
1298
- return item.timestamp ?? 0;
1299
- }
1300
- }
1301
- /**
1302
- * Get sortable value from SchedulerInfo
1303
- */
1304
- getSchedulerSortValue(item, field) {
1305
- switch (field) {
1306
- case "name":
1307
- return item.name.toLowerCase();
1308
- case "queueName":
1309
- return item.queueName.toLowerCase();
1310
- case "pattern":
1311
- return item.pattern?.toLowerCase() ?? "";
1312
- case "next":
1313
- return item.next ?? 0;
1314
- case "tz":
1315
- return item.tz?.toLowerCase() ?? "";
1316
- default:
1317
- return item.name.toLowerCase();
1318
- }
1319
- }
1320
- /**
1321
- * Get sortable value from DelayedJobInfo
1322
- */
1323
- getDelayedSortValue(item, field) {
1324
- switch (field) {
1325
- case "name":
1326
- return item.name.toLowerCase();
1327
- case "queueName":
1328
- return item.queueName.toLowerCase();
1329
- case "processAt":
1330
- return item.processAt;
1331
- case "delay":
1332
- return item.delay;
1333
- default:
1334
- return item.processAt;
1335
- }
1336
- }
1337
- /**
1338
- * Convert a BullMQ Job to JobInfo or RunInfoList
1339
- * @param job - The BullMQ job to convert
1340
- * @param fields - "list" for lightweight list view, "full" for complete job details
1341
- * @param knownState - Optional: skip getState() call if state is already known from fetch
1342
- */
1343
- async jobToInfo(job, _fields = "full", knownState) {
1344
- let state = knownState;
1345
- if (!state) {
1346
- const cacheKey = `${job.queueName}:${job.id}`;
1347
- state = this.jobStateCache.get(cacheKey);
1348
- if (!state) {
1349
- state = await job.getState();
1350
- this.jobStateCache.set(cacheKey, state);
1351
- }
1352
- }
1353
- const duration = job.finishedOn && job.processedOn ? job.finishedOn - job.processedOn : void 0;
1354
- let progress = 0;
1355
- if (typeof job.progress === "number") {
1356
- progress = job.progress;
1357
- } else if (typeof job.progress === "object" && job.progress !== null) {
1358
- progress = job.progress;
1359
- }
1360
- const tags = this.extractTags(job.data);
1361
- let parent;
1362
- if (job.parent?.id) {
1363
- parent = {
1364
- id: job.parent.id,
1365
- queueName: job.parent.queueKey?.split(":")[1] || job.parent.queueKey || ""
1366
- };
1367
- } else if (job.parentKey) {
1368
- const parts = job.parentKey.split(":");
1369
- if (parts.length >= 3) {
1370
- parent = {
1371
- id: parts[parts.length - 1] || "",
1372
- queueName: parts[parts.length - 2] || ""
1373
- };
1374
- }
1375
- }
1376
- return {
1377
- id: job.id || "",
1378
- name: job.name,
1379
- data: job.data,
1380
- opts: {
1381
- attempts: job.opts.attempts,
1382
- delay: job.opts.delay,
1383
- priority: job.opts.priority
1384
- },
1385
- progress,
1386
- attemptsMade: job.attemptsMade,
1387
- processedOn: job.processedOn,
1388
- finishedOn: job.finishedOn,
1389
- timestamp: job.timestamp,
1390
- failedReason: job.failedReason,
1391
- stacktrace: job.stacktrace ?? void 0,
1392
- returnvalue: job.returnvalue,
1393
- status: state,
1394
- duration,
1395
- tags,
1396
- parent
1397
- };
1398
- }
1399
- // ─────────────────────────────────────────────────────────────────────────────
1400
- // Bulk Operations
1401
- // ─────────────────────────────────────────────────────────────────────────────
1402
- /**
1403
- * Retry multiple jobs across queues
1404
- * Processed in parallel for better performance
1405
- */
1406
- async bulkRetry(jobs) {
1407
- const results = await Promise.allSettled(
1408
- jobs.map(async ({ queueName, jobId }) => {
1409
- const queue = this.queues.get(queueName);
1410
- if (!queue) {
1411
- throw new Error("Queue not found");
1412
- }
1413
- const job = await queue.getJob(jobId);
1414
- if (!job) {
1415
- throw new Error("Job not found");
1416
- }
1417
- await job.retry();
1418
- this.invalidateJobCache(queueName, jobId);
1419
- return { success: true };
1420
- })
1421
- );
1422
- let success = 0;
1423
- let failed = 0;
1424
- for (const result of results) {
1425
- if (result.status === "fulfilled") {
1426
- success++;
1427
- } else {
1428
- failed++;
1429
- }
1430
- }
1431
- return { success, failed };
1432
- }
1433
- /**
1434
- * Delete multiple jobs across queues
1435
- * Processed in parallel for better performance
1436
- */
1437
- async bulkDelete(jobs) {
1438
- const results = await Promise.allSettled(
1439
- jobs.map(async ({ queueName, jobId }) => {
1440
- const queue = this.queues.get(queueName);
1441
- if (!queue) {
1442
- throw new Error("Queue not found");
1443
- }
1444
- const job = await queue.getJob(jobId);
1445
- if (!job) {
1446
- throw new Error("Job not found");
1447
- }
1448
- await job.remove();
1449
- this.invalidateJobCache(queueName, jobId);
1450
- return { success: true };
1451
- })
1452
- );
1453
- let success = 0;
1454
- let failed = 0;
1455
- for (const result of results) {
1456
- if (result.status === "fulfilled") {
1457
- success++;
1458
- } else {
1459
- failed++;
1460
- }
1461
- }
1462
- return { success, failed };
1463
- }
1464
- /**
1465
- * Promote multiple delayed jobs across queues (move to waiting)
1466
- * Processed in parallel for better performance
1467
- */
1468
- async bulkPromote(jobs) {
1469
- const results = await Promise.allSettled(
1470
- jobs.map(async ({ queueName, jobId }) => {
1471
- const queue = this.queues.get(queueName);
1472
- if (!queue) {
1473
- throw new Error("Queue not found");
1474
- }
1475
- const job = await queue.getJob(jobId);
1476
- if (!job) {
1477
- throw new Error("Job not found");
1478
- }
1479
- await job.promote();
1480
- this.invalidateJobCache(queueName, jobId);
1481
- return { success: true };
1482
- })
1483
- );
1484
- let success = 0;
1485
- let failed = 0;
1486
- for (const result of results) {
1487
- if (result.status === "fulfilled") {
1488
- success++;
1489
- } else {
1490
- failed++;
1491
- }
1492
- }
1493
- return { success, failed };
1494
- }
1495
- // ─────────────────────────────────────────────────────────────────────────────
1496
- // Flow Operations
1497
- // ─────────────────────────────────────────────────────────────────────────────
1498
- /**
1499
- * Get all flows (jobs that have children or are part of a flow) - cached
1500
- * Optimized to focus on waiting-children type first and early exit
1501
- */
1502
- async getFlows(limit = 50) {
1503
- if (!this.flowProducer) {
1504
- return [];
1505
- }
1506
- return this.cached(`flows:${limit}`, this.CACHE_TTL.flows, async () => {
1507
- const queueEntries = Array.from(this.queues.entries());
1508
- const queueChecks = await Promise.all(
1509
- queueEntries.map(async ([queueName, queue]) => {
1510
- const counts = await this.getCachedJobCounts(queue);
1511
- const hasRelevantJobs = (counts.waiting || 0) > 0 || (counts["waiting-children"] || 0) > 0 || (counts.active || 0) > 0;
1512
- return { queueName, queue, hasRelevantJobs };
1513
- })
1514
- );
1515
- const relevantQueues = queueChecks.filter((q) => q.hasRelevantJobs);
1516
- if (relevantQueues.length === 0) {
1517
- return [];
1518
- }
1519
- const queueResults = await Promise.all(
1520
- relevantQueues.map(async ({ queueName, queue }) => {
1521
- try {
1522
- const waitingChildrenJobs = await queue.getJobs(
1523
- ["waiting-children"],
1524
- 0,
1525
- 50
1526
- );
1527
- if (waitingChildrenJobs.length >= limit) {
1528
- return { queueName, jobs: waitingChildrenJobs };
1529
- }
1530
- const otherTypes = [
1531
- "waiting",
1532
- "active",
1533
- "completed",
1534
- "failed",
1535
- "delayed"
1536
- ];
1537
- const otherJobArrays = await Promise.all(
1538
- otherTypes.map(async (type) => {
1539
- try {
1540
- return await queue.getJobs(type, 0, 30);
1541
- } catch {
1542
- return [];
1543
- }
1544
- })
1545
- );
1546
- const allJobs = [...waitingChildrenJobs, ...otherJobArrays.flat()];
1547
- return { queueName, jobs: allJobs };
1548
- } catch {
1549
- return { queueName, jobs: [] };
1550
- }
1551
- })
1552
- );
1553
- const seenJobIds = /* @__PURE__ */ new Set();
1554
- const potentialRoots = [];
1555
- for (const { queueName, jobs } of queueResults) {
1556
- if (potentialRoots.length >= limit * 2) {
1557
- break;
1558
- }
1559
- for (const job of jobs) {
1560
- if (!job?.id) continue;
1561
- const jobKey = `${queueName}:${job.id}`;
1562
- if (seenJobIds.has(jobKey)) continue;
1563
- seenJobIds.add(jobKey);
1564
- const hasParent = !!job.parent || !!job.parentKey;
1565
- if (!hasParent) {
1566
- potentialRoots.push({ queueName, job });
1567
- if (potentialRoots.length >= limit * 2) {
1568
- break;
1569
- }
1570
- }
1571
- }
1572
- }
1573
- const batchSize = 20;
1574
- const flows = [];
1575
- for (let i = 0; i < potentialRoots.length && flows.length < limit; i += batchSize) {
1576
- const batch = potentialRoots.slice(i, i + batchSize);
1577
- const batchResults = await Promise.all(
1578
- batch.map(async ({ queueName, job }) => {
1579
- try {
1580
- const flowTree = await this.flowProducer.getFlow({
1581
- id: job.id,
1582
- queueName
1583
- });
1584
- if (flowTree?.children && flowTree.children.length > 0) {
1585
- const stats = this.countFlowStats(flowTree);
1586
- const state = await job.getState();
1587
- return {
1588
- id: job.id,
1589
- name: job.name,
1590
- queueName,
1591
- status: state,
1592
- totalJobs: stats.total,
1593
- completedJobs: stats.completed,
1594
- failedJobs: stats.failed,
1595
- timestamp: job.timestamp,
1596
- duration: job.finishedOn && job.processedOn ? job.finishedOn - job.processedOn : void 0
1597
- };
1598
- }
1599
- } catch {
1600
- }
1601
- return null;
1602
- })
1603
- );
1604
- for (const result of batchResults) {
1605
- if (result && flows.length < limit) {
1606
- flows.push(result);
1607
- }
1608
- }
1609
- }
1610
- return flows.sort((a, b) => b.timestamp - a.timestamp);
1611
- });
1612
- }
1613
- /**
1614
- * Get a single flow tree by root job ID
1615
- */
1616
- async getFlow(queueName, jobId) {
1617
- if (!this.flowProducer) {
1618
- return null;
1619
- }
1620
- try {
1621
- const flowTree = await this.flowProducer.getFlow({
1622
- id: jobId,
1623
- queueName
1624
- });
1625
- if (!flowTree) {
1626
- return null;
1627
- }
1628
- return this.convertFlowTree(flowTree);
1629
- } catch {
1630
- return null;
1631
- }
1632
- }
1633
- /**
1634
- * Create a new flow
1635
- */
1636
- async createFlow(request) {
1637
- if (!this.flowProducer) {
1638
- throw new Error("FlowProducer not initialized");
1639
- }
1640
- const flowJob = this.buildFlowJob(request);
1641
- const result = await this.flowProducer.add(flowJob);
1642
- return { id: result.job.id || "" };
1643
- }
1644
- /**
1645
- * Build a FlowJob from CreateFlowRequest or CreateFlowChildRequest
1646
- */
1647
- buildFlowJob(request) {
1648
- const result = {
1649
- name: request.name,
1650
- queueName: request.queueName,
1651
- data: request.data || {}
1652
- };
1653
- if (request.children && request.children.length > 0) {
1654
- result.children = request.children.map(
1655
- (child) => this.buildFlowJob(child)
1656
- );
1657
- }
1658
- return result;
1659
- }
1660
- /**
1661
- * Convert BullMQ flow tree to our FlowNode structure
1662
- */
1663
- async convertFlowTree(tree) {
1664
- const job = tree.job;
1665
- const state = await job.getState();
1666
- const duration = job.finishedOn && job.processedOn ? job.finishedOn - job.processedOn : void 0;
1667
- const jobInfo = {
1668
- id: job.id || "",
1669
- name: job.name,
1670
- data: job.data,
1671
- opts: {
1672
- attempts: job.opts?.attempts,
1673
- delay: job.opts?.delay,
1674
- priority: job.opts?.priority
1675
- },
1676
- progress: typeof job.progress === "number" ? job.progress : typeof job.progress === "object" ? job.progress : 0,
1677
- attemptsMade: job.attemptsMade || 0,
1678
- processedOn: job.processedOn,
1679
- finishedOn: job.finishedOn,
1680
- timestamp: job.timestamp,
1681
- failedReason: job.failedReason,
1682
- stacktrace: job.stacktrace ?? void 0,
1683
- returnvalue: job.returnvalue,
1684
- status: state,
1685
- duration,
1686
- tags: this.extractTags(job.data)
1687
- };
1688
- const children = [];
1689
- if (tree.children && tree.children.length > 0) {
1690
- for (const child of tree.children) {
1691
- children.push(await this.convertFlowTree(child));
1692
- }
1693
- }
1694
- return {
1695
- job: jobInfo,
1696
- queueName: job.queueName || tree.queueName || "",
1697
- children: children.length > 0 ? children : void 0
1698
- };
1699
- }
1700
- /**
1701
- * Count statistics for a flow tree
1702
- */
1703
- countFlowStats(tree) {
1704
- let total = 1;
1705
- let completed = 0;
1706
- let failed = 0;
1707
- const job = tree.job;
1708
- if (job.finishedOn && !job.failedReason) {
1709
- completed = 1;
1710
- } else if (job.failedReason) {
1711
- failed = 1;
1712
- }
1713
- if (tree.children) {
1714
- for (const child of tree.children) {
1715
- const childStats = this.countFlowStats(child);
1716
- total += childStats.total;
1717
- completed += childStats.completed;
1718
- failed += childStats.failed;
1719
- }
1720
- }
1721
- return { total, completed, failed };
1722
- }
1723
- };
1724
-
1725
- // src/core/workbench.ts
1726
- var WorkbenchCore = class {
1727
- options;
1728
- queueManager;
1729
- constructor(options) {
1730
- const opts = Array.isArray(options) ? { queues: options } : options;
1731
- const queueGroups = [
1732
- ...opts.queueGroups || [],
1733
- ...opts.groups || []
1734
- ];
1735
- const queues = [
1736
- ...opts.queues || [],
1737
- ...queueGroups.flatMap((group) => group.queues)
1738
- ];
1739
- this.options = {
1740
- title: "Workbench",
1741
- readonly: false,
1742
- ...opts,
1743
- queues
1744
- };
1745
- if (queues.length === 0) {
1746
- throw new Error(
1747
- "Workbench requires at least one queue. Pass queues directly, use queueGroups, or provide a redis connection for auto-discovery."
1748
- );
1749
- }
1750
- this.queueManager = new QueueManager(queues, this.options.tags || [], [
1751
- ...queueGroups
1752
- ]);
1753
- }
1754
- /**
1755
- * Get the queue manager instance
1756
- */
1757
- getQueueManager() {
1758
- return this.queueManager;
1759
- }
1760
- /**
1761
- * Check if authentication is required
1762
- */
1763
- requiresAuth() {
1764
- return !!(this.options.auth?.username && this.options.auth?.password);
1765
- }
1766
- /**
1767
- * Validate authentication credentials
1768
- */
1769
- validateAuth(username, password) {
1770
- if (!this.requiresAuth()) return true;
1771
- return username === this.options.auth?.username && password === this.options.auth?.password;
1772
- }
1773
- /**
1774
- * Get dashboard configuration for the UI
1775
- */
1776
- getConfig() {
1777
- return {
1778
- title: this.options.title,
1779
- logo: this.options.logo,
1780
- readonly: this.options.readonly,
1781
- queues: this.queueManager.getQueueNames(),
1782
- queueGroups: this.queueManager.getQueueGroups(),
1783
- tags: this.queueManager.getTagFields()
1784
- };
1785
- }
1786
- };
1787
-
1788
- // src/server/hono-app.ts
1789
- import { Hono as Hono2 } from "hono";
1790
- import { basicAuth } from "hono/basic-auth";
1791
- import { cors } from "hono/cors";
1792
-
1793
- // src/api/router.ts
1794
- import { Hono } from "hono";
1795
-
1796
- // src/api/handlers.ts
1797
- function parseSort(sort) {
1798
- if (!sort) return void 0;
1799
- const [field, dir] = sort.split(":");
1800
- if (!field) return void 0;
1801
- return {
1802
- field,
1803
- direction: dir === "asc" ? "asc" : "desc"
1804
- };
1805
- }
1806
- var readonlyError = {
1807
- status: 403,
1808
- body: { error: "Dashboard is in readonly mode" }
1809
- };
1810
- function buildRouteTable(core) {
1811
- const qm = core.queueManager;
1812
- const isReadonly = () => !!core.options.readonly;
1813
- return [
1814
- {
1815
- method: "post",
1816
- path: "/refresh",
1817
- handler: async () => {
1818
- qm.clearCache();
1819
- return { status: 200, body: { success: true } };
1820
- }
1821
- },
1822
- {
1823
- method: "get",
1824
- path: "/overview",
1825
- handler: async () => ({
1826
- status: 200,
1827
- body: await qm.getOverview()
1828
- })
1829
- },
1830
- {
1831
- method: "get",
1832
- path: "/counts",
1833
- handler: async () => ({
1834
- status: 200,
1835
- body: await qm.getQuickCounts()
1836
- })
1837
- },
1838
- {
1839
- method: "get",
1840
- path: "/runs",
1841
- handler: async ({ query }) => {
1842
- const limit = Number(query.limit) || 50;
1843
- const cursor = query.cursor;
1844
- const start = cursor ? Number(cursor) : 0;
1845
- const sort = parseSort(query.sort);
1846
- const status = query.status;
1847
- const q = query.q;
1848
- const from = query.from;
1849
- const to = query.to;
1850
- const tagsParam = query.tags;
1851
- let tags;
1852
- if (tagsParam) {
1853
- try {
1854
- tags = JSON.parse(tagsParam);
1855
- } catch {
1856
- const tagPairs = tagsParam.split(",");
1857
- tags = {};
1858
- for (const pair of tagPairs) {
1859
- const [key, value] = pair.split(":");
1860
- if (key && value) {
1861
- tags[key.trim()] = value.trim();
1862
- }
1863
- }
1864
- }
1865
- }
1866
- let timeRange;
1867
- if (from && to) {
1868
- timeRange = {
1869
- start: Number(from),
1870
- end: Number(to)
1871
- };
1872
- }
1873
- let text;
1874
- if (q) {
1875
- if (!q.includes(":")) {
1876
- text = q;
1877
- } else {
1878
- const parts = q.split(" ");
1879
- const textParts = parts.filter((p) => !p.includes(":"));
1880
- if (textParts.length > 0) {
1881
- text = textParts.join(" ");
1882
- }
1883
- }
1884
- }
1885
- const filters = status || tags || text || timeRange ? {
1886
- status,
1887
- tags,
1888
- text,
1889
- timeRange
1890
- } : void 0;
1891
- return {
1892
- status: 200,
1893
- body: await qm.getAllRuns(limit, start, sort, filters)
1894
- };
1895
- }
1896
- },
1897
- {
1898
- method: "get",
1899
- path: "/schedulers",
1900
- handler: async ({ query }) => {
1901
- const repeatableSort = parseSort(query.repeatableSort);
1902
- const delayedSort = parseSort(query.delayedSort);
1903
- return {
1904
- status: 200,
1905
- body: await qm.getSchedulers(repeatableSort, delayedSort)
1906
- };
1907
- }
1908
- },
1909
- {
1910
- method: "post",
1911
- path: "/test",
1912
- handler: async ({ body }) => {
1913
- if (isReadonly()) return readonlyError;
1914
- const req = body;
1915
- if (!req?.queueName || !req.jobName) {
1916
- return {
1917
- status: 400,
1918
- body: { error: "queueName and jobName are required" }
1919
- };
1920
- }
1921
- try {
1922
- const result = await qm.enqueueJob(req);
1923
- return { status: 200, body: result };
1924
- } catch (e) {
1925
- return { status: 400, body: { error: e.message } };
1926
- }
1927
- }
1928
- },
1929
- {
1930
- method: "get",
1931
- path: "/queue-names",
1932
- handler: async () => ({
1933
- status: 200,
1934
- body: qm.getQueueNames()
1935
- })
1936
- },
1937
- {
1938
- method: "get",
1939
- path: "/queues",
1940
- handler: async () => ({
1941
- status: 200,
1942
- body: await qm.getQueues()
1943
- })
1944
- },
1945
- {
1946
- method: "get",
1947
- path: "/metrics",
1948
- handler: async () => ({
1949
- status: 200,
1950
- body: await qm.getMetrics()
1951
- })
1952
- },
1953
- {
1954
- method: "get",
1955
- path: "/activity",
1956
- handler: async () => ({
1957
- status: 200,
1958
- body: await qm.getActivityStats()
1959
- })
1960
- },
1961
- {
1962
- method: "get",
1963
- path: "/queues/:name/jobs",
1964
- handler: async ({ params, query }) => {
1965
- const name = params.name;
1966
- const status = query.status;
1967
- const limit = Number(query.limit) || 50;
1968
- const cursor = query.cursor;
1969
- const start = cursor ? Number(cursor) : 0;
1970
- const sort = parseSort(query.sort);
1971
- return {
1972
- status: 200,
1973
- body: await qm.getJobs(name, status, limit, start, sort)
1974
- };
1975
- }
1976
- },
1977
- {
1978
- method: "get",
1979
- path: "/jobs/:queue/:id/logs",
1980
- handler: async ({ params, query }) => {
1981
- const start = query.start ? Number(query.start) : 0;
1982
- const end = query.end ? Number(query.end) : 999;
1983
- const asc = query.asc !== "false";
1984
- const logs = await qm.getJobLogs(
1985
- params.queue,
1986
- params.id,
1987
- start,
1988
- end,
1989
- asc
1990
- );
1991
- if (!logs) {
1992
- return { status: 404, body: { error: "Job not found" } };
1993
- }
1994
- return { status: 200, body: logs };
1995
- }
1996
- },
1997
- {
1998
- method: "get",
1999
- path: "/jobs/:queue/:id",
2000
- handler: async ({ params }) => {
2001
- const job = await qm.getJob(params.queue, params.id);
2002
- if (!job) {
2003
- return { status: 404, body: { error: "Job not found" } };
2004
- }
2005
- return { status: 200, body: job };
2006
- }
2007
- },
2008
- {
2009
- method: "post",
2010
- path: "/jobs/:queue/:id/retry",
2011
- handler: async ({ params }) => {
2012
- if (isReadonly()) return readonlyError;
2013
- const success = await qm.retryJob(params.queue, params.id);
2014
- if (!success) {
2015
- return { status: 400, body: { error: "Failed to retry job" } };
2016
- }
2017
- return { status: 200, body: { success: true } };
2018
- }
2019
- },
2020
- {
2021
- method: "post",
2022
- path: "/jobs/:queue/:id/remove",
2023
- handler: async ({ params }) => {
2024
- if (isReadonly()) return readonlyError;
2025
- const success = await qm.removeJob(params.queue, params.id);
2026
- if (!success) {
2027
- return { status: 400, body: { error: "Failed to remove job" } };
2028
- }
2029
- return { status: 200, body: { success: true } };
2030
- }
2031
- },
2032
- {
2033
- method: "post",
2034
- path: "/jobs/:queue/:id/promote",
2035
- handler: async ({ params }) => {
2036
- if (isReadonly()) return readonlyError;
2037
- const success = await qm.promoteJob(params.queue, params.id);
2038
- if (!success) {
2039
- return { status: 400, body: { error: "Failed to promote job" } };
2040
- }
2041
- return { status: 200, body: { success: true } };
2042
- }
2043
- },
2044
- {
2045
- method: "get",
2046
- path: "/search",
2047
- handler: async ({ query }) => {
2048
- const q = query.q || "";
2049
- const limit = Number(query.limit) || 20;
2050
- if (!q) return { status: 200, body: { results: [] } };
2051
- const results = await qm.search(q, limit);
2052
- return { status: 200, body: { results } };
2053
- }
2054
- },
2055
- {
2056
- method: "get",
2057
- path: "/tags/:field/values",
2058
- handler: async ({ params, query }) => {
2059
- const field = params.field;
2060
- const limit = Number(query.limit) || 50;
2061
- const tagFields = qm.getTagFields();
2062
- if (tagFields.length > 0 && !tagFields.includes(field)) {
2063
- return {
2064
- status: 400,
2065
- body: {
2066
- error: `Field "${field}" is not a configured tag field`
2067
- }
2068
- };
2069
- }
2070
- const values = await qm.getTagValues(field, limit);
2071
- return { status: 200, body: { field, values } };
2072
- }
2073
- },
2074
- {
2075
- method: "post",
2076
- path: "/queues/:name/clean",
2077
- handler: async ({ params, body }) => {
2078
- if (isReadonly()) return readonlyError;
2079
- const req = body;
2080
- if (!req) {
2081
- return { status: 400, body: { error: "Body required" } };
2082
- }
2083
- const count = await qm.cleanJobs(
2084
- params.name,
2085
- req.status,
2086
- req.grace || 0
2087
- );
2088
- return { status: 200, body: { removed: count } };
2089
- }
2090
- },
2091
- {
2092
- method: "post",
2093
- path: "/bulk/retry",
2094
- handler: async ({ body }) => {
2095
- if (isReadonly()) return readonlyError;
2096
- const req = body;
2097
- if (!req?.jobs) {
2098
- return { status: 400, body: { error: "jobs is required" } };
2099
- }
2100
- return { status: 200, body: await qm.bulkRetry(req.jobs) };
2101
- }
2102
- },
2103
- {
2104
- method: "post",
2105
- path: "/bulk/delete",
2106
- handler: async ({ body }) => {
2107
- if (isReadonly()) return readonlyError;
2108
- const req = body;
2109
- if (!req?.jobs) {
2110
- return { status: 400, body: { error: "jobs is required" } };
2111
- }
2112
- return { status: 200, body: await qm.bulkDelete(req.jobs) };
2113
- }
2114
- },
2115
- {
2116
- method: "post",
2117
- path: "/bulk/promote",
2118
- handler: async ({ body }) => {
2119
- if (isReadonly()) return readonlyError;
2120
- const req = body;
2121
- if (!req?.jobs) {
2122
- return { status: 400, body: { error: "jobs is required" } };
2123
- }
2124
- return { status: 200, body: await qm.bulkPromote(req.jobs) };
2125
- }
2126
- },
2127
- {
2128
- method: "post",
2129
- path: "/queues/:name/pause",
2130
- handler: async ({ params }) => {
2131
- if (isReadonly()) return readonlyError;
2132
- try {
2133
- await qm.pauseQueue(params.name);
2134
- return { status: 200, body: { success: true, paused: true } };
2135
- } catch (error) {
2136
- return {
2137
- status: 404,
2138
- body: {
2139
- error: error instanceof Error ? error.message : "Failed to pause queue"
2140
- }
2141
- };
2142
- }
2143
- }
2144
- },
2145
- {
2146
- method: "post",
2147
- path: "/queues/:name/resume",
2148
- handler: async ({ params }) => {
2149
- if (isReadonly()) return readonlyError;
2150
- try {
2151
- await qm.resumeQueue(params.name);
2152
- return { status: 200, body: { success: true, paused: false } };
2153
- } catch (error) {
2154
- return {
2155
- status: 404,
2156
- body: {
2157
- error: error instanceof Error ? error.message : "Failed to resume queue"
2158
- }
2159
- };
2160
- }
2161
- }
2162
- },
2163
- {
2164
- method: "get",
2165
- path: "/flows",
2166
- handler: async ({ query }) => {
2167
- const limit = Number(query.limit) || 50;
2168
- const flows = await qm.getFlows(limit);
2169
- return { status: 200, body: { flows } };
2170
- }
2171
- },
2172
- {
2173
- method: "get",
2174
- path: "/flows/:queueName/:jobId",
2175
- handler: async ({ params }) => {
2176
- const flow = await qm.getFlow(params.queueName, params.jobId);
2177
- if (!flow) {
2178
- return { status: 404, body: { error: "Flow not found" } };
2179
- }
2180
- return { status: 200, body: flow };
2181
- }
2182
- },
2183
- {
2184
- method: "post",
2185
- path: "/flows",
2186
- handler: async ({ body }) => {
2187
- if (isReadonly()) return readonlyError;
2188
- const req = body;
2189
- if (!req?.name || !req.queueName || !req.children?.length) {
2190
- return {
2191
- status: 400,
2192
- body: { error: "name, queueName, and children are required" }
2193
- };
2194
- }
2195
- try {
2196
- const result = await qm.createFlow(req);
2197
- return { status: 200, body: result };
2198
- } catch (e) {
2199
- return { status: 400, body: { error: e.message } };
2200
- }
2201
- }
2202
- }
2203
- ];
2204
- }
2205
-
2206
- // src/api/router.ts
2207
- function createApiRoutes(core) {
2208
- const app = new Hono();
2209
- for (const route of buildRouteTable(core)) {
2210
- app[route.method](route.path, async (c) => {
2211
- let body;
2212
- const method = c.req.method;
2213
- if (method !== "GET" && method !== "HEAD") {
2214
- const contentType = c.req.header("content-type") ?? "";
2215
- if (contentType.includes("application/json")) {
2216
- try {
2217
- body = await c.req.json();
2218
- } catch {
2219
- body = void 0;
2220
- }
2221
- }
2222
- }
2223
- const input = {
2224
- params: c.req.param(),
2225
- query: c.req.query(),
2226
- body
2227
- };
2228
- const result = await route.handler(input);
2229
- return new Response(JSON.stringify(result.body), {
2230
- status: result.status,
2231
- headers: { "Content-Type": "application/json" }
2232
- });
2233
- });
2234
- }
2235
- return app;
2236
- }
2237
-
2238
- // src/server/base-path.ts
2239
- var CLIENT_ROUTES = [
2240
- /\/queues\/[^/]+\/jobs\/[^/]+\/?$/,
2241
- /\/queues\/[^/]+\/?$/,
2242
- /\/queues\/?$/,
2243
- /\/flows\/[^/]+\/[^/]+\/?$/,
2244
- /\/schedulers\/?$/,
2245
- /\/flows\/?$/,
2246
- /\/metrics\/?$/,
2247
- /\/test\/?$/
2248
- ];
2249
- function computeBasePath(pathname) {
2250
- let basePath = pathname;
2251
- for (const route of CLIENT_ROUTES) {
2252
- basePath = basePath.replace(route, "");
2253
- }
2254
- if (!basePath.endsWith("/")) {
2255
- basePath = `${basePath}/`;
2256
- }
2257
- return basePath;
2258
- }
2259
- function resolveBasePath(override, pathname) {
2260
- if (override) {
2261
- return override.endsWith("/") ? override : `${override}/`;
2262
- }
2263
- return computeBasePath(pathname);
2264
- }
2265
-
2266
- // src/server/static-assets.ts
2267
- import { existsSync, readFileSync } from "fs";
2268
- import { join as join2 } from "path";
2269
-
2270
- // src/ui-dist.ts
2271
- import { dirname, join } from "path";
2272
- import { fileURLToPath } from "url";
2273
- var UI_DIST_PATH = join(dirname(fileURLToPath(import.meta.url)), "ui");
2274
-
2275
- // src/server/static-assets.ts
2276
- function serveStaticAsset(filename) {
2277
- const filePath = join2(UI_DIST_PATH, "assets", filename);
2278
- if (!existsSync(filePath)) {
2279
- return { status: 404, body: null, contentType: "text/plain" };
2280
- }
2281
- const body = readFileSync(filePath);
2282
- const contentType = filename.endsWith(".js") ? "application/javascript" : filename.endsWith(".css") ? "text/css" : filename.endsWith(".svg") ? "image/svg+xml" : filename.endsWith(".png") ? "image/png" : filename.endsWith(".ico") ? "image/x-icon" : filename.endsWith(".woff2") ? "font/woff2" : "application/octet-stream";
2283
- return { status: 200, body, contentType };
2284
- }
2285
- function renderIndexHtml(basePath, title) {
2286
- const devServerUrl = process.env.WORKBENCH_UI_DEV_SERVER;
2287
- if (devServerUrl) {
2288
- return {
2289
- body: devServerHtml(title, basePath, devServerUrl),
2290
- contentType: "text/html; charset=utf-8"
2291
- };
2292
- }
2293
- const indexPath = join2(UI_DIST_PATH, "index.html");
2294
- if (existsSync(indexPath)) {
2295
- let html = readFileSync(indexPath, "utf-8");
2296
- html = html.replace("<head>", `<head>
2297
- <base href="${basePath}">`);
2298
- return { body: html, contentType: "text/html; charset=utf-8" };
2299
- }
2300
- return {
2301
- body: fallbackHtml(title, basePath),
2302
- contentType: "text/html; charset=utf-8"
2303
- };
2304
- }
2305
- function devServerHtml(title, basePath, devServerUrl) {
2306
- const origin = devServerUrl.replace(/\/$/, "");
2307
- const devBasePath = basePath.endsWith("/") ? basePath : `${basePath}/`;
2308
- return `<!DOCTYPE html>
2309
- <html lang="en">
2310
- <head>
2311
- <base href="${basePath}">
2312
- <meta charset="UTF-8" />
2313
- <link rel="icon" href="assets/favicon.ico" />
2314
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
2315
- <title>${title}</title>
2316
- <script type="module">
2317
- import RefreshRuntime from "${origin}${devBasePath}@react-refresh";
2318
- RefreshRuntime.injectIntoGlobalHook(window);
2319
- window.$RefreshReg$ = () => {};
2320
- window.$RefreshSig$ = () => (type) => type;
2321
- window.__vite_plugin_react_preamble_installed__ = true;
2322
- </script>
2323
- <script type="module" src="${origin}${devBasePath}@vite/client"></script>
2324
- </head>
2325
- <body>
2326
- <div id="root"></div>
2327
- <script type="module" src="${origin}${devBasePath}main.tsx"></script>
2328
- </body>
2329
- </html>`;
2330
- }
2331
- function fallbackHtml(title, basePath) {
2332
- return `<!DOCTYPE html>
2333
- <html lang="en">
2334
- <head>
2335
- <base href="${basePath}">
2336
- <meta charset="UTF-8" />
2337
- <link rel="icon" href="assets/favicon.ico" />
2338
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
2339
- <title>${title}</title>
2340
- <style>
2341
- body {
2342
- font-family: system-ui, sans-serif;
2343
- display: flex;
2344
- align-items: center;
2345
- justify-content: center;
2346
- min-height: 100vh;
2347
- margin: 0;
2348
- background: #0a0a0a;
2349
- color: #fafafa;
2350
- }
2351
- .message {
2352
- text-align: center;
2353
- padding: 2rem;
2354
- }
2355
- code {
2356
- background: #1a1a1a;
2357
- padding: 0.5rem 1rem;
2358
- border-radius: 0.5rem;
2359
- display: block;
2360
- margin-top: 1rem;
2361
- }
2362
- </style>
2363
- </head>
2364
- <body>
2365
- <div class="message">
2366
- <h1>${title}</h1>
2367
- <p>UI assets not found. Build @maydotinc/q-studio/core first:</p>
2368
- <code>pnpm --filter @maydotinc/q-studio-core build</code>
2369
- </div>
2370
- </body>
2371
- </html>`;
2372
- }
2373
-
2374
- // src/server/hono-app.ts
2375
- function buildWorkbenchApp(core) {
2376
- const app = new Hono2();
2377
- app.use("/api/*", cors());
2378
- if (core.requiresAuth()) {
2379
- app.use(
2380
- "*",
2381
- basicAuth({
2382
- username: core.options.auth.username,
2383
- password: core.options.auth.password
2384
- })
2385
- );
2386
- }
2387
- app.route("/api", createApiRoutes(core));
2388
- app.get("/config", (c) => c.json(core.getConfig()));
2389
- app.get("/assets/:file", (c) => {
2390
- const fileName = c.req.param("file");
2391
- const asset = serveStaticAsset(fileName);
2392
- if (asset.status === 404 || !asset.body) {
2393
- return c.text("Not found", 404);
2394
- }
2395
- return new Response(new Uint8Array(asset.body), {
2396
- status: 200,
2397
- headers: { "Content-Type": asset.contentType }
2398
- });
2399
- });
2400
- app.get("*", (c) => {
2401
- const url = new URL(c.req.url);
2402
- const basePath = resolveBasePath(core.options.basePath, url.pathname);
2403
- const html = renderIndexHtml(basePath, core.options.title || "Workbench");
2404
- return c.html(html.body);
2405
- });
2406
- return app;
2407
- }
2408
-
2409
- // src/api/fetch-handler.ts
2410
- function createFetchHandler(options) {
2411
- const core = new WorkbenchCore(options);
2412
- const app = buildWorkbenchApp(core);
2413
- const basePath = normalizeBasePath(core.options.basePath);
2414
- const fetchHandler = async (req) => {
2415
- if (basePath) {
2416
- const url = new URL(req.url);
2417
- if (url.pathname === basePath || url.pathname.startsWith(`${basePath}/`)) {
2418
- const rewritten = url.pathname.slice(basePath.length) || "/";
2419
- url.pathname = rewritten;
2420
- const init = {
2421
- method: req.method,
2422
- headers: req.headers,
2423
- body: req.method === "GET" || req.method === "HEAD" ? void 0 : req.body,
2424
- // @ts-expect-error duplex is required for streaming bodies in Node 18+
2425
- duplex: "half",
2426
- redirect: req.redirect
2427
- };
2428
- return app.fetch(new Request(url.toString(), init));
2429
- }
2430
- }
2431
- return app.fetch(req);
2432
- };
2433
- return {
2434
- fetch: fetchHandler,
2435
- core
2436
- };
2437
- }
2438
- function normalizeBasePath(value) {
2439
- if (!value) return null;
2440
- const trimmed = value.endsWith("/") ? value.slice(0, -1) : value;
2441
- if (trimmed === "" || trimmed === "/") return null;
2442
- return trimmed.startsWith("/") ? trimmed : `/${trimmed}`;
2443
- }
2444
-
2445
- // src/server/basic-auth.ts
2446
- function checkBasicAuth(authHeader, username, password) {
2447
- if (!authHeader) return false;
2448
- const [scheme, encoded] = authHeader.split(" ");
2449
- if (!scheme || scheme.toLowerCase() !== "basic" || !encoded) return false;
2450
- let decoded;
2451
- try {
2452
- decoded = Buffer.from(encoded, "base64").toString("utf-8");
2453
- } catch {
2454
- return false;
2455
- }
2456
- const idx = decoded.indexOf(":");
2457
- if (idx === -1) return false;
2458
- const user = decoded.slice(0, idx);
2459
- const pass = decoded.slice(idx + 1);
2460
- return safeEqual(user, username) && safeEqual(pass, password);
2461
- }
2462
- function safeEqual(a, b) {
2463
- if (a.length !== b.length) return false;
2464
- let diff = 0;
2465
- for (let i = 0; i < a.length; i++) {
2466
- diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
2467
- }
2468
- return diff === 0;
2469
- }
2470
- var BASIC_AUTH_CHALLENGE = {
2471
- status: 401,
2472
- headers: { "WWW-Authenticate": 'Basic realm="Workbench"' },
2473
- body: "Unauthorized"
2474
- };
2475
- export {
2476
- BASIC_AUTH_CHALLENGE,
2477
- QueueManager,
2478
- UI_DIST_PATH,
2479
- WorkbenchCore,
2480
- buildRouteTable,
2481
- buildWorkbenchApp,
2482
- checkBasicAuth,
2483
- computeBasePath,
2484
- createApiRoutes,
2485
- createFetchHandler,
2486
- renderIndexHtml,
2487
- resolveBasePath,
2488
- serveStaticAsset
2489
- };
2490
- //# sourceMappingURL=index.js.map