queen-mq 0.1.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.
Files changed (90) hide show
  1. package/API.md +1116 -0
  2. package/CACHE.md +519 -0
  3. package/DASHBOARD-V3.md +478 -0
  4. package/DASHBOARD.md +382 -0
  5. package/MOD_QUEUE.md +453 -0
  6. package/PARTITION_LOCKING_DESIGN.md +989 -0
  7. package/PLAN.md +707 -0
  8. package/QUERY_ANALSYS.md +72 -0
  9. package/QUEUE_BUS.md +334 -0
  10. package/README.md +1495 -0
  11. package/V2-PLAN.md +236 -0
  12. package/assets/dashboard.png +0 -0
  13. package/dashboard/.vscode/extensions.json +3 -0
  14. package/dashboard/README.md +5 -0
  15. package/dashboard/index.html +14 -0
  16. package/dashboard/package-lock.json +1458 -0
  17. package/dashboard/package.json +25 -0
  18. package/dashboard/public/vite.svg +1 -0
  19. package/dashboard/src/App.vue +29 -0
  20. package/dashboard/src/assets/styles/main.css +908 -0
  21. package/dashboard/src/assets/vue.svg +1 -0
  22. package/dashboard/src/components/cards/MetricCard.vue +298 -0
  23. package/dashboard/src/components/charts/QueueDepthChart.vue +276 -0
  24. package/dashboard/src/components/charts/QueueLagChart.vue +436 -0
  25. package/dashboard/src/components/charts/ThroughputChart.vue +302 -0
  26. package/dashboard/src/components/common/ActivityFeed.vue +251 -0
  27. package/dashboard/src/components/layout/AppHeader.vue +208 -0
  28. package/dashboard/src/components/layout/AppLayout.vue +88 -0
  29. package/dashboard/src/components/layout/AppSidebar.vue +261 -0
  30. package/dashboard/src/main.js +44 -0
  31. package/dashboard/src/router.js +54 -0
  32. package/dashboard/src/services/api.js +187 -0
  33. package/dashboard/src/services/websocket.js +167 -0
  34. package/dashboard/src/utils/constants.js +56 -0
  35. package/dashboard/src/utils/helpers.js +118 -0
  36. package/dashboard/src/views/Analytics.vue +912 -0
  37. package/dashboard/src/views/Dashboard.vue +906 -0
  38. package/dashboard/src/views/Messages.vue +437 -0
  39. package/dashboard/src/views/QueueDetail.vue +501 -0
  40. package/dashboard/src/views/Queues.vue +333 -0
  41. package/dashboard/vite.config.js +30 -0
  42. package/debug-namespace.js +110 -0
  43. package/docs/long-polling.md +159 -0
  44. package/docs/multi-server-cache-solutions.md +185 -0
  45. package/docs/performance-tuning.md +222 -0
  46. package/examples/bus-mode.js +239 -0
  47. package/examples/continuous-consumer-optimized.js +215 -0
  48. package/examples/continuous-consumer.js +159 -0
  49. package/examples/continuous-producer.js +343 -0
  50. package/examples/mixed-mode.js +277 -0
  51. package/examples/multi-server-test.js +305 -0
  52. package/examples/single.js +64 -0
  53. package/examples/smartchat-dealyed.js +42 -0
  54. package/examples/smartchat.js +52 -0
  55. package/examples/test-cache-invalidation.js +119 -0
  56. package/examples/test-cache-multi-server.js +245 -0
  57. package/examples/test-minimal-client.js +112 -0
  58. package/examples/test-queue-creation-policy.js +137 -0
  59. package/init-db.js +20 -0
  60. package/package.json +36 -0
  61. package/src/client/client.js +291 -0
  62. package/src/client/index.js +6 -0
  63. package/src/client/queenClient.js +513 -0
  64. package/src/client/utils/http.js +172 -0
  65. package/src/client/utils/loadBalancer.js +152 -0
  66. package/src/client/utils/retry.js +35 -0
  67. package/src/config.js +215 -0
  68. package/src/database/connection.js +103 -0
  69. package/src/database/poolManager.js +192 -0
  70. package/src/database/schema-v2.sql +214 -0
  71. package/src/managers/eventManager.js +59 -0
  72. package/src/managers/queueManagerOptimized.js +1512 -0
  73. package/src/managers/resourceCache.js +96 -0
  74. package/src/managers/systemEventManager.js +127 -0
  75. package/src/routes/ack.js +26 -0
  76. package/src/routes/analytics.js +812 -0
  77. package/src/routes/configure.js +46 -0
  78. package/src/routes/messages.js +298 -0
  79. package/src/routes/pop.js +85 -0
  80. package/src/routes/push.js +28 -0
  81. package/src/routes/resources.js +296 -0
  82. package/src/server.js +1286 -0
  83. package/src/services/encryptionService.js +82 -0
  84. package/src/services/evictionService.js +131 -0
  85. package/src/services/retentionService.js +129 -0
  86. package/src/services/startupSync.js +35 -0
  87. package/src/test/test.js +4521 -0
  88. package/src/utils/logger.js +44 -0
  89. package/src/utils/uuid.js +5 -0
  90. package/src/websocket/wsServer.js +221 -0
package/API.md ADDED
@@ -0,0 +1,1116 @@
1
+ # Queen V2 API Documentation
2
+
3
+ ## Overview
4
+
5
+ Queen V2 is a high-performance message queue system with a two-tier architecture:
6
+ - **Queues**: Top-level organizational units
7
+ - **Partitions**: Subdivisions within queues where FIFO ordering is maintained
8
+
9
+ Base URL: `http://localhost:6632/api/v1`
10
+
11
+ ## Core Concepts
12
+
13
+ ### Message Flow
14
+ 1. Messages are **pushed** to a queue (optionally specifying a partition)
15
+ 2. Messages are **popped** from either a specific partition or any partition in a queue
16
+ 3. Messages must be **acknowledged** after processing (completed or failed)
17
+
18
+ ### Partitions
19
+ - Every queue automatically has a "Default" partition
20
+ - Additional partitions can be created by pushing messages to them
21
+ - FIFO ordering is maintained within each partition
22
+ - Messages without a specified partition go to "Default"
23
+
24
+ ### Optional Grouping
25
+ - Queues can have optional `namespace` and `task` fields for logical grouping
26
+ - These don't affect the hierarchy but allow filtering operations
27
+
28
+ ---
29
+
30
+ ## API Endpoints
31
+
32
+ ### 1. Push Messages
33
+ **Endpoint:** `POST /api/v1/push`
34
+
35
+ Adds one or more messages to queues.
36
+
37
+ **Request Body:**
38
+ ```json
39
+ {
40
+ "items": [
41
+ {
42
+ "queue": "email-queue", // Required: queue name
43
+ "partition": "urgent", // Optional: defaults to "Default"
44
+ "payload": { // Required: message data (JSON)
45
+ "to": "user@example.com",
46
+ "subject": "Hello"
47
+ },
48
+ "transactionId": "uuid-here" // Optional: idempotency key
49
+ }
50
+ ]
51
+ }
52
+ ```
53
+
54
+ **Response:**
55
+ ```json
56
+ {
57
+ "messages": [
58
+ {
59
+ "id": "018e63b7-6165-453f-88ae-56effa177605",
60
+ "transactionId": "4dfb0478-655b-4c91-bcd9-b7acacf0400f",
61
+ "status": "queued" // or "duplicate" if transactionId exists
62
+ }
63
+ ]
64
+ }
65
+ ```
66
+
67
+ **Notes:**
68
+ - Supports batch operations (up to 1000 messages)
69
+ - Automatic duplicate detection via transactionId
70
+ - Creates queue/partition if they don't exist
71
+
72
+ ---
73
+
74
+ ### 2. Pop Messages
75
+
76
+ #### 2.1 Pop from Specific Partition
77
+ **Endpoint:** `GET /api/v1/pop/queue/:queue/partition/:partition`
78
+
79
+ Retrieves messages from a specific partition.
80
+
81
+ **Query Parameters:**
82
+ - `batch` (integer): Number of messages to retrieve (default: 1)
83
+ - `wait` (boolean): Enable long polling (default: false)
84
+ - `timeout` (integer): Long polling timeout in ms (default: 30000, max: 60000)
85
+
86
+ **Example:**
87
+ ```
88
+ GET /api/v1/pop/queue/emails/partition/urgent?batch=5&wait=true
89
+ ```
90
+
91
+ **Response:**
92
+ ```json
93
+ {
94
+ "messages": [
95
+ {
96
+ "id": "message-uuid",
97
+ "transactionId": "transaction-uuid",
98
+ "queue": "emails",
99
+ "partition": "urgent",
100
+ "data": { "to": "user@example.com", "subject": "Alert" },
101
+ "retryCount": 0,
102
+ "priority": 0,
103
+ "createdAt": "2025-10-08T05:12:33.893Z",
104
+ "lockedAt": null,
105
+ "options": { "leaseTime": 300, "retryLimit": 3 }
106
+ }
107
+ ]
108
+ }
109
+ ```
110
+
111
+ #### 2.2 Pop from Any Partition in Queue
112
+ **Endpoint:** `GET /api/v1/pop/queue/:queue`
113
+
114
+ Retrieves messages from any available partition in the queue (oldest first).
115
+
116
+ **Query Parameters:** Same as above
117
+
118
+ **Example:**
119
+ ```
120
+ GET /api/v1/pop/queue/emails?batch=10
121
+ ```
122
+
123
+ #### 2.3 Pop with Filters
124
+ **Endpoint:** `GET /api/v1/pop`
125
+
126
+ Retrieves messages from queues matching namespace or task filters.
127
+
128
+ **Query Parameters:**
129
+ - `namespace` (string): Filter by namespace
130
+ - `task` (string): Filter by task
131
+ - `batch`, `wait`, `timeout`: Same as above
132
+
133
+ **Example:**
134
+ ```
135
+ GET /api/v1/pop?namespace=production&batch=5
136
+ GET /api/v1/pop?task=billing&wait=true
137
+ ```
138
+
139
+ **Response Format:** Same as above
140
+
141
+ **Status Codes:**
142
+ - `200`: Messages retrieved successfully
143
+ - `204`: No messages available (empty response)
144
+
145
+ ---
146
+
147
+ ### 3. Acknowledge Messages
148
+
149
+ #### 3.1 Single Acknowledgment
150
+ **Endpoint:** `POST /api/v1/ack`
151
+
152
+ Acknowledges a single message as completed or failed.
153
+
154
+ **Request Body:**
155
+ ```json
156
+ {
157
+ "transactionId": "4dfb0478-655b-4c91-bcd9-b7acacf0400f",
158
+ "status": "completed", // or "failed"
159
+ "error": "Error message if failed" // Optional
160
+ }
161
+ ```
162
+
163
+ **Response:**
164
+ ```json
165
+ {
166
+ "transactionId": "4dfb0478-655b-4c91-bcd9-b7acacf0400f",
167
+ "status": "completed",
168
+ "acknowledgedAt": "2025-10-08T07:12:47.353Z"
169
+ }
170
+ ```
171
+
172
+ #### 3.2 Batch Acknowledgment
173
+ **Endpoint:** `POST /api/v1/ack/batch`
174
+
175
+ Acknowledges multiple messages at once.
176
+
177
+ **Request Body:**
178
+ ```json
179
+ {
180
+ "acknowledgments": [
181
+ {
182
+ "transactionId": "uuid-1",
183
+ "status": "completed"
184
+ },
185
+ {
186
+ "transactionId": "uuid-2",
187
+ "status": "failed",
188
+ "error": "Processing error"
189
+ }
190
+ ]
191
+ }
192
+ ```
193
+
194
+ **Response:**
195
+ ```json
196
+ {
197
+ "processed": 2,
198
+ "results": [
199
+ { "transactionId": "uuid-1", "status": "completed" },
200
+ { "transactionId": "uuid-2", "status": "retry_scheduled", "retryCount": 1 }
201
+ ]
202
+ }
203
+ ```
204
+
205
+ **Notes:**
206
+ - Failed messages are automatically retried based on partition's `retryLimit`
207
+ - After max retries, messages can be moved to dead letter queue
208
+
209
+ ---
210
+
211
+ ### 4. Configure Queue
212
+ **Endpoint:** `POST /api/v1/configure`
213
+
214
+ Configures options for a queue. All configuration is now at the queue level - partitions are simple FIFO containers.
215
+
216
+ **Request Body:**
217
+ ```json
218
+ {
219
+ "queue": "notifications",
220
+ "partition": "critical", // DEPRECATED: Ignored but accepted for backward compatibility
221
+ "options": {
222
+ "leaseTime": 600, // Seconds before message lease expires
223
+ "retryLimit": 5, // Max retry attempts
224
+ "priority": 10, // Queue priority (higher = processed first)
225
+ "maxSize": 10000, // Max messages in queue
226
+ "ttl": 3600, // Time to live in seconds
227
+ "dlqAfterMaxRetries": true, // Move to DLQ after max retries
228
+ "delayedProcessing": 0, // Delay before messages are available (seconds)
229
+ "windowBuffer": 0, // Buffer window for message processing (seconds)
230
+ "retentionSeconds": 0, // Auto-delete pending messages after X seconds
231
+ "completedRetentionSeconds": 0, // Auto-delete completed messages after X seconds
232
+ "retentionEnabled": false, // Enable retention policies
233
+ "encryptionEnabled": false, // Enable message encryption
234
+ "maxWaitTimeSeconds": 0 // Max wait time for long polling
235
+ }
236
+ }
237
+ ```
238
+
239
+ **Response:**
240
+ ```json
241
+ {
242
+ "queue": "notifications",
243
+ "configured": true,
244
+ "options": { /* all options with defaults filled */ }
245
+ }
246
+ ```
247
+
248
+ **Note:** The `partition` parameter in the request is deprecated and ignored. All configuration now applies to the entire queue.
249
+
250
+ ---
251
+
252
+ ### 5. Analytics & Monitoring
253
+
254
+ #### 5.1 Queue Statistics
255
+ **Endpoint:** `GET /api/v1/analytics/queue/:queue`
256
+
257
+ Gets detailed statistics for a specific queue.
258
+
259
+ **Response:**
260
+ ```json
261
+ {
262
+ "queue": "emails",
263
+ "namespace": null,
264
+ "task": null,
265
+ "totals": {
266
+ "pending": 10,
267
+ "processing": 5,
268
+ "completed": 100,
269
+ "failed": 2,
270
+ "deadLetter": 1,
271
+ "total": 118
272
+ },
273
+ "partitions": [
274
+ {
275
+ "name": "Default",
276
+ "stats": {
277
+ "pending": 8,
278
+ "processing": 3,
279
+ "completed": 80,
280
+ "failed": 1,
281
+ "deadLetter": 0,
282
+ "total": 92
283
+ }
284
+ },
285
+ {
286
+ "name": "urgent",
287
+ "stats": {
288
+ "pending": 2,
289
+ "processing": 2,
290
+ "completed": 20,
291
+ "failed": 1,
292
+ "deadLetter": 1,
293
+ "total": 26
294
+ }
295
+ }
296
+ ]
297
+ }
298
+ ```
299
+
300
+ #### 5.2 All Queues Overview
301
+ **Endpoint:** `GET /api/v1/analytics/queues`
302
+
303
+ Gets statistics for all queues in the system.
304
+
305
+ **Query Parameters:**
306
+ - `namespace` (string): Filter by namespace
307
+ - `task` (string): Filter by task
308
+
309
+ **Response:**
310
+ ```json
311
+ {
312
+ "queues": [
313
+ {
314
+ "queue": "emails",
315
+ "namespace": "production",
316
+ "task": "notifications",
317
+ "partitions": [...],
318
+ "totals": {...}
319
+ }
320
+ ]
321
+ }
322
+ ```
323
+
324
+ #### 5.3 Queue Depths
325
+ **Endpoint:** `GET /api/v1/analytics/queue-depths`
326
+
327
+ Gets pending message counts for all queues.
328
+
329
+ **Response:**
330
+ ```json
331
+ {
332
+ "depths": [
333
+ {
334
+ "queue": "emails",
335
+ "depth": 10, // Total pending
336
+ "processing": 5, // Total processing
337
+ "partitions": [
338
+ { "name": "Default", "depth": 8, "processing": 3 },
339
+ { "name": "urgent", "depth": 2, "processing": 2 }
340
+ ]
341
+ }
342
+ ]
343
+ }
344
+ ```
345
+
346
+ #### 5.4 Throughput Metrics
347
+ **Endpoint:** `GET /api/v1/analytics/throughput`
348
+
349
+ Gets throughput metrics over the last hour (minute-by-minute).
350
+
351
+ **Response:**
352
+ ```json
353
+ {
354
+ "throughput": [
355
+ {
356
+ "timestamp": "2025-10-08T07:00:00.000Z",
357
+ "incoming": {
358
+ "messagesPerMinute": 120,
359
+ "messagesPerSecond": 2
360
+ },
361
+ "completed": {
362
+ "messagesPerMinute": 115,
363
+ "messagesPerSecond": 1
364
+ },
365
+ "processing": {
366
+ "messagesPerMinute": 100,
367
+ "messagesPerSecond": 1
368
+ },
369
+ "failed": {
370
+ "messagesPerMinute": 5,
371
+ "messagesPerSecond": 0
372
+ },
373
+ "lag": {
374
+ "avgSeconds": 2.5,
375
+ "avgMilliseconds": 2500,
376
+ "sampleCount": 115
377
+ }
378
+ }
379
+ // ... 59 more entries
380
+ ]
381
+ }
382
+ ```
383
+
384
+ #### 5.5 Queue Lag Analysis
385
+ **Endpoint:** `GET /api/v1/analytics/queue-lag`
386
+
387
+ Gets queue lag metrics based on processing times and current backlog.
388
+
389
+ **Query Parameters:**
390
+ - `queue` (string): Filter by queue name
391
+ - `namespace` (string): Filter by namespace
392
+ - `task` (string): Filter by task
393
+
394
+ **Response:**
395
+ ```json
396
+ {
397
+ "queues": [
398
+ {
399
+ "queue": "emails",
400
+ "namespace": "production",
401
+ "task": "notifications",
402
+ "partitions": [
403
+ {
404
+ "name": "Default",
405
+ "stats": {
406
+ "pendingCount": 150,
407
+ "processingCount": 25,
408
+ "totalBacklog": 175,
409
+ "completedMessages": 1250,
410
+ "avgProcessingTimeSeconds": 2.5,
411
+ "medianProcessingTimeSeconds": 2.1,
412
+ "p95ProcessingTimeSeconds": 4.8,
413
+ "estimatedLagSeconds": 437.5,
414
+ "medianLagSeconds": 367.5,
415
+ "p95LagSeconds": 840.0,
416
+ "estimatedLag": "7m 17s",
417
+ "medianLag": "6m 7s",
418
+ "p95Lag": "14m 0s",
419
+ "avgProcessingTime": "2.5s",
420
+ "medianProcessingTime": "2.1s",
421
+ "p95ProcessingTime": "4.8s"
422
+ }
423
+ }
424
+ ],
425
+ "totals": {
426
+ "pendingCount": 150,
427
+ "processingCount": 25,
428
+ "totalBacklog": 175,
429
+ "completedMessages": 1250,
430
+ "avgProcessingTimeSeconds": 2.5,
431
+ "medianProcessingTimeSeconds": 2.1,
432
+ "p95ProcessingTimeSeconds": 4.8,
433
+ "estimatedLagSeconds": 437.5,
434
+ "medianLagSeconds": 367.5,
435
+ "p95LagSeconds": 840.0,
436
+ "estimatedLag": "7m 17s",
437
+ "medianLag": "6m 7s",
438
+ "p95Lag": "14m 0s",
439
+ "avgProcessingTime": "2.5s",
440
+ "medianProcessingTime": "2.1s",
441
+ "p95ProcessingTime": "4.8s"
442
+ }
443
+ }
444
+ ]
445
+ }
446
+ ```
447
+
448
+ **Lag Calculation:**
449
+ - **Estimated Lag**: `(pending + processing) × average_processing_time`
450
+ - **Median Lag**: `(pending + processing) × median_processing_time`
451
+ - **95th Percentile Lag**: `(pending + processing) × p95_processing_time`
452
+
453
+ **Notes:**
454
+ - Only includes queues with at least 5 completed messages in the last 24 hours
455
+ - Processing times are calculated from `completed_at - created_at` for completed messages
456
+ - Lag represents the estimated time for all current backlog to be processed
457
+
458
+ #### 5.6 Queue-Specific Stats
459
+ **Endpoint:** `GET /api/v1/analytics/queue-stats`
460
+
461
+ Gets statistics with flexible filtering.
462
+
463
+ **Query Parameters:**
464
+ - `queue` (string): Filter by queue name
465
+ - `namespace` (string): Filter by namespace
466
+ - `task` (string): Filter by task
467
+
468
+ **Example:**
469
+ ```
470
+ GET /api/v1/analytics/queue-stats?queue=emails
471
+ GET /api/v1/analytics/queue-stats?namespace=production
472
+ ```
473
+
474
+ #### 5.6 Filtered Analytics
475
+ **Endpoint:** `GET /api/v1/analytics`
476
+
477
+ Gets analytics based on namespace or task filters.
478
+
479
+ **Query Parameters:**
480
+ - `namespace` (string): Get all queues in namespace
481
+ - `task` (string): Get all queues with task
482
+
483
+ ---
484
+
485
+ ### 6. Message Management
486
+
487
+ #### 6.1 List Messages
488
+ **Endpoint:** `GET /api/v1/messages`
489
+
490
+ Lists messages with filtering options.
491
+
492
+ **Query Parameters:**
493
+ - `queue` (string): Filter by queue name
494
+ - `partition` (string): Filter by partition name
495
+ - `namespace` (string): Filter by namespace
496
+ - `task` (string): Filter by task
497
+ - `status` (string): Filter by status (pending, processing, completed, failed, dead_letter)
498
+ - `limit` (integer): Max results (default: 100)
499
+ - `offset` (integer): Pagination offset (default: 0)
500
+
501
+ **Response:**
502
+ ```json
503
+ {
504
+ "messages": [
505
+ {
506
+ "id": "message-uuid",
507
+ "transactionId": "transaction-uuid",
508
+ "queuePath": "emails/urgent",
509
+ "queue": "emails",
510
+ "partition": "urgent",
511
+ "namespace": null,
512
+ "task": null,
513
+ "payload": {...},
514
+ "status": "pending",
515
+ "workerId": null,
516
+ "createdAt": "2025-10-08T05:12:33.889Z",
517
+ "lockedAt": null,
518
+ "completedAt": null,
519
+ "failedAt": null,
520
+ "errorMessage": null,
521
+ "retryCount": 0,
522
+ "leaseExpiresAt": null
523
+ }
524
+ ]
525
+ }
526
+ ```
527
+
528
+ #### 6.2 Get Single Message
529
+ **Endpoint:** `GET /api/v1/messages/:transactionId`
530
+
531
+ Gets details of a specific message.
532
+
533
+ **Response:** Single message object with partition options included
534
+
535
+ #### 6.3 Delete Message
536
+ **Endpoint:** `DELETE /api/v1/messages/:transactionId`
537
+
538
+ Permanently deletes a message.
539
+
540
+ **Response:**
541
+ ```json
542
+ {
543
+ "deleted": true,
544
+ "transactionId": "transaction-uuid"
545
+ }
546
+ ```
547
+
548
+ #### 6.4 Retry Failed Message
549
+ **Endpoint:** `POST /api/v1/messages/:transactionId/retry`
550
+
551
+ Resets a failed message to pending for retry.
552
+
553
+ **Response:**
554
+ ```json
555
+ {
556
+ "retried": true,
557
+ "transactionId": "transaction-uuid"
558
+ }
559
+ ```
560
+
561
+ #### 6.5 Move to Dead Letter Queue
562
+ **Endpoint:** `POST /api/v1/messages/:transactionId/dlq`
563
+
564
+ Moves a failed message to the dead letter queue.
565
+
566
+ **Response:**
567
+ ```json
568
+ {
569
+ "movedToDLQ": true,
570
+ "transactionId": "transaction-uuid"
571
+ }
572
+ ```
573
+
574
+ #### 6.6 Get Related Messages
575
+ **Endpoint:** `GET /api/v1/messages/:transactionId/related`
576
+
577
+ Gets messages from the same partition within 1 hour of the specified message.
578
+
579
+ **Response:**
580
+ ```json
581
+ {
582
+ "messages": [
583
+ {
584
+ "transactionId": "related-uuid",
585
+ "status": "completed",
586
+ "createdAt": "2025-10-08T05:10:00.000Z",
587
+ "payload": {...}
588
+ }
589
+ ]
590
+ }
591
+ ```
592
+
593
+ #### 6.7 Clear Queue
594
+ **Endpoint:** `DELETE /api/v1/queues/:queue/clear`
595
+
596
+ Deletes all messages from a queue or specific partition.
597
+
598
+ **Query Parameters:**
599
+ - `partition` (string): Clear specific partition only
600
+
601
+ **Response:**
602
+ ```json
603
+ {
604
+ "cleared": true,
605
+ "count": 25,
606
+ "queue": "emails",
607
+ "partition": "all" // or specific partition name
608
+ }
609
+ ```
610
+
611
+ ---
612
+
613
+ ### 7. System Health & Metrics
614
+
615
+ #### 7.1 Health Check
616
+ **Endpoint:** `GET /health`
617
+
618
+ Checks system health and basic statistics.
619
+
620
+ **Response:**
621
+ ```json
622
+ {
623
+ "status": "healthy",
624
+ "uptime": "3600s",
625
+ "connections": 5,
626
+ "stats": {
627
+ "requests": 1000,
628
+ "messages": 5000,
629
+ "requestsPerSecond": "0.28",
630
+ "messagesPerSecond": "1.39",
631
+ "pool": {
632
+ "total": 20,
633
+ "idle": 15,
634
+ "waiting": 0
635
+ }
636
+ }
637
+ }
638
+ ```
639
+
640
+ #### 7.2 Detailed Metrics
641
+ **Endpoint:** `GET /metrics`
642
+
643
+ Gets detailed performance metrics.
644
+
645
+ **Response:**
646
+ ```json
647
+ {
648
+ "uptime": 3600,
649
+ "requests": {
650
+ "total": 1000,
651
+ "rate": 0.28
652
+ },
653
+ "messages": {
654
+ "total": 5000,
655
+ "rate": 1.39
656
+ },
657
+ "database": {
658
+ "poolSize": 20,
659
+ "idleConnections": 15,
660
+ "waitingRequests": 0
661
+ },
662
+ "memory": {
663
+ "rss": 104857600,
664
+ "heapTotal": 73728000,
665
+ "heapUsed": 45678900,
666
+ "external": 2345678,
667
+ "arrayBuffers": 123456
668
+ },
669
+ "cpu": {
670
+ "user": 1234567,
671
+ "system": 234567
672
+ }
673
+ }
674
+ ```
675
+
676
+ ---
677
+
678
+ ## Resource Management
679
+
680
+ ### 8. Resources API
681
+
682
+ These endpoints provide information about queues, partitions, and system structure for frontend displays.
683
+
684
+ #### 8.1 List All Queues
685
+ **Endpoint:** `GET /api/v1/resources/queues`
686
+
687
+ Gets all queues with summary information.
688
+
689
+ **Query Parameters:**
690
+ - `namespace` (string): Filter by namespace
691
+ - `task` (string): Filter by task
692
+
693
+ **Response:**
694
+ ```json
695
+ {
696
+ "queues": [
697
+ {
698
+ "id": "queue-uuid",
699
+ "name": "emails",
700
+ "namespace": null,
701
+ "task": null,
702
+ "createdAt": "2025-10-08T05:13:37.528Z",
703
+ "partitions": 3,
704
+ "messages": {
705
+ "total": 100,
706
+ "pending": 20,
707
+ "processing": 5
708
+ }
709
+ }
710
+ ]
711
+ }
712
+ ```
713
+
714
+ #### 8.2 Get Queue Details
715
+ **Endpoint:** `GET /api/v1/resources/queues/:queue`
716
+
717
+ Gets detailed information about a specific queue including all partitions.
718
+
719
+ **Response:**
720
+ ```json
721
+ {
722
+ "id": "queue-uuid",
723
+ "name": "emails",
724
+ "namespace": null,
725
+ "task": null,
726
+ "createdAt": "2025-10-08T05:13:37.528Z",
727
+ "partitions": [
728
+ {
729
+ "id": "partition-uuid",
730
+ "name": "urgent",
731
+ "priority": 10,
732
+ "options": {
733
+ "leaseTime": 300,
734
+ "retryLimit": 3
735
+ },
736
+ "createdAt": "2025-10-08T05:13:37.541Z",
737
+ "stats": {
738
+ "total": 50,
739
+ "pending": 10,
740
+ "processing": 2,
741
+ "completed": 35,
742
+ "failed": 3,
743
+ "deadLetter": 0
744
+ },
745
+ "oldestMessage": "2025-10-08T05:00:00.000Z",
746
+ "newestMessage": "2025-10-08T07:30:00.000Z"
747
+ }
748
+ ],
749
+ "totals": {
750
+ "total": 100,
751
+ "pending": 20,
752
+ "processing": 5,
753
+ "completed": 70,
754
+ "failed": 5,
755
+ "deadLetter": 0
756
+ }
757
+ }
758
+ ```
759
+
760
+ #### 8.3 List All Partitions
761
+ **Endpoint:** `GET /api/v1/resources/partitions`
762
+
763
+ Gets all partitions across all queues.
764
+
765
+ **Query Parameters:**
766
+ - `queue` (string): Filter by queue name
767
+ - `minDepth` (integer): Only show partitions with at least this many pending messages
768
+
769
+ **Response:**
770
+ ```json
771
+ {
772
+ "partitions": [
773
+ {
774
+ "id": "partition-uuid",
775
+ "name": "urgent",
776
+ "queue": "emails",
777
+ "namespace": null,
778
+ "task": null,
779
+ "priority": 10,
780
+ "options": { /* partition options */ },
781
+ "createdAt": "2025-10-08T05:13:37.541Z",
782
+ "depth": 10,
783
+ "processing": 2,
784
+ "total": 50
785
+ }
786
+ ]
787
+ }
788
+ ```
789
+
790
+ #### 8.4 List Namespaces
791
+ **Endpoint:** `GET /api/v1/resources/namespaces`
792
+
793
+ Gets all namespaces with aggregated statistics.
794
+
795
+ **Response:**
796
+ ```json
797
+ {
798
+ "namespaces": [
799
+ {
800
+ "namespace": "production",
801
+ "queues": 5,
802
+ "partitions": 15,
803
+ "messages": {
804
+ "total": 1000,
805
+ "pending": 200
806
+ }
807
+ }
808
+ ]
809
+ }
810
+ ```
811
+
812
+ #### 8.5 List Tasks
813
+ **Endpoint:** `GET /api/v1/resources/tasks`
814
+
815
+ Gets all tasks with aggregated statistics.
816
+
817
+ **Response:**
818
+ ```json
819
+ {
820
+ "tasks": [
821
+ {
822
+ "task": "notifications",
823
+ "queues": 3,
824
+ "partitions": 9,
825
+ "messages": {
826
+ "total": 500,
827
+ "pending": 100
828
+ }
829
+ }
830
+ ]
831
+ }
832
+ ```
833
+
834
+ #### 8.6 System Overview
835
+ **Endpoint:** `GET /api/v1/resources/overview`
836
+
837
+ Gets a complete system overview with all key metrics.
838
+
839
+ **Response:**
840
+ ```json
841
+ {
842
+ "queues": 8,
843
+ "partitions": 15,
844
+ "namespaces": 2,
845
+ "tasks": 3,
846
+ "messages": {
847
+ "total": 1000,
848
+ "pending": 200,
849
+ "processing": 50,
850
+ "completed": 700,
851
+ "failed": 40,
852
+ "deadLetter": 10
853
+ },
854
+ "timestamp": "2025-10-08T07:29:35.682Z"
855
+ }
856
+ ```
857
+
858
+ ---
859
+
860
+ ## WebSocket Support
861
+
862
+ ### Dashboard WebSocket
863
+ **Endpoint:** `ws://localhost:6632/ws/dashboard`
864
+
865
+ Real-time updates for dashboard monitoring with V2 structure.
866
+
867
+ #### Connection
868
+ ```javascript
869
+ const ws = new WebSocket('ws://localhost:6632/ws/dashboard');
870
+
871
+ ws.onopen = () => {
872
+ // Send ping to keep alive
873
+ setInterval(() => ws.send('ping'), 30000);
874
+
875
+ // Optional: Subscribe to specific queues
876
+ ws.send(JSON.stringify({
877
+ type: 'subscribe',
878
+ queues: ['emails', 'payments']
879
+ }));
880
+ };
881
+ ```
882
+
883
+ #### Events (Server → Client)
884
+
885
+ **Connection Events:**
886
+ ```json
887
+ {
888
+ "event": "connected",
889
+ "data": {
890
+ "connectionId": "uuid",
891
+ "version": "v2"
892
+ },
893
+ "timestamp": "2025-10-08T07:30:00.000Z"
894
+ }
895
+ ```
896
+
897
+ **Message Events:**
898
+ ```json
899
+ // Message pushed
900
+ {
901
+ "event": "message.pushed",
902
+ "data": {
903
+ "queue": "emails",
904
+ "partition": "urgent",
905
+ "transactionId": "uuid"
906
+ },
907
+ "timestamp": "2025-10-08T07:30:00.000Z"
908
+ }
909
+
910
+ // Message processing
911
+ {
912
+ "event": "message.processing",
913
+ "data": {
914
+ "queue": "emails",
915
+ "partition": "urgent",
916
+ "transactionId": "uuid",
917
+ "workerId": "worker-123"
918
+ },
919
+ "timestamp": "2025-10-08T07:30:00.000Z"
920
+ }
921
+
922
+ // Message completed
923
+ {
924
+ "event": "message.completed",
925
+ "data": {
926
+ "transactionId": "uuid"
927
+ },
928
+ "timestamp": "2025-10-08T07:30:00.000Z"
929
+ }
930
+
931
+ // Message failed
932
+ {
933
+ "event": "message.failed",
934
+ "data": {
935
+ "transactionId": "uuid",
936
+ "error": "Processing error"
937
+ },
938
+ "timestamp": "2025-10-08T07:30:00.000Z"
939
+ }
940
+ ```
941
+
942
+ **Queue Events:**
943
+ ```json
944
+ // Queue created
945
+ {
946
+ "event": "queue.created",
947
+ "data": {
948
+ "queue": "new-queue",
949
+ "partition": "Default"
950
+ },
951
+ "timestamp": "2025-10-08T07:30:00.000Z"
952
+ }
953
+
954
+ // Queue depth update (every 5 seconds)
955
+ {
956
+ "event": "queue.depth",
957
+ "data": {
958
+ "queue": "emails",
959
+ "namespace": null,
960
+ "task": null,
961
+ "totalDepth": 25,
962
+ "totalProcessing": 5,
963
+ "partitions": {
964
+ "Default": {
965
+ "depth": 10,
966
+ "processing": 2,
967
+ "completed": 100,
968
+ "failed": 5
969
+ },
970
+ "urgent": {
971
+ "depth": 15,
972
+ "processing": 3,
973
+ "completed": 50,
974
+ "failed": 2
975
+ }
976
+ }
977
+ },
978
+ "timestamp": "2025-10-08T07:30:00.000Z"
979
+ }
980
+
981
+ // Partition depth update (every 5 seconds)
982
+ {
983
+ "event": "partition.depth",
984
+ "data": {
985
+ "queue": "emails",
986
+ "partition": "urgent",
987
+ "depth": 15,
988
+ "processing": 3,
989
+ "completed": 50,
990
+ "failed": 2,
991
+ "total": 70
992
+ },
993
+ "timestamp": "2025-10-08T07:30:00.000Z"
994
+ }
995
+ ```
996
+
997
+ **System Events:**
998
+ ```json
999
+ // System statistics (every 10 seconds)
1000
+ {
1001
+ "event": "system.stats",
1002
+ "data": {
1003
+ "pending": 200,
1004
+ "processing": 50,
1005
+ "recentCreated": 120, // Last minute
1006
+ "recentCompleted": 115, // Last minute
1007
+ "connections": 5 // WebSocket connections
1008
+ },
1009
+ "timestamp": "2025-10-08T07:30:00.000Z"
1010
+ }
1011
+
1012
+ // Client connected
1013
+ {
1014
+ "event": "client.connected",
1015
+ "data": {
1016
+ "clientId": "uuid"
1017
+ },
1018
+ "timestamp": "2025-10-08T07:30:00.000Z"
1019
+ }
1020
+
1021
+ // Client disconnected
1022
+ {
1023
+ "event": "client.disconnected",
1024
+ "data": {
1025
+ "clientId": "uuid"
1026
+ },
1027
+ "timestamp": "2025-10-08T07:30:00.000Z"
1028
+ }
1029
+ ```
1030
+
1031
+ #### Client → Server Messages
1032
+
1033
+ **Keep Alive:**
1034
+ ```
1035
+ ping
1036
+ ```
1037
+ Server responds with: `pong`
1038
+
1039
+ **Subscribe to Queues:**
1040
+ ```json
1041
+ {
1042
+ "type": "subscribe",
1043
+ "queues": ["emails", "payments", "notifications"]
1044
+ }
1045
+ ```
1046
+
1047
+ **Note:** Subscription is optional and currently for future filtering implementation
1048
+
1049
+ ---
1050
+
1051
+ ## Error Responses
1052
+
1053
+ All endpoints may return error responses:
1054
+
1055
+ ```json
1056
+ {
1057
+ "error": "Error message description"
1058
+ }
1059
+ ```
1060
+
1061
+ **Common Status Codes:**
1062
+ - `200`: Success
1063
+ - `201`: Created
1064
+ - `204`: No Content (empty response)
1065
+ - `400`: Bad Request (invalid parameters)
1066
+ - `404`: Not Found
1067
+ - `500`: Internal Server Error
1068
+ - `503`: Service Unavailable (database connection issues)
1069
+
1070
+ ---
1071
+
1072
+ ## Performance Considerations
1073
+
1074
+ 1. **Batch Operations**: Use batch push/pop for better throughput
1075
+ 2. **Long Polling**: Use `wait=true` to reduce polling overhead
1076
+ 3. **Partition Strategy**: Use multiple partitions for parallel processing
1077
+ 4. **Lease Time**: Set appropriate lease times based on processing duration
1078
+ 5. **Connection Pooling**: System supports up to 10,000+ messages/second with proper configuration
1079
+
1080
+ ---
1081
+
1082
+ ## Example Usage Flow
1083
+
1084
+ ```javascript
1085
+ // 1. Push a message
1086
+ POST /api/v1/push
1087
+ {
1088
+ "items": [{
1089
+ "queue": "orders",
1090
+ "partition": "high-priority",
1091
+ "payload": { "orderId": "12345", "amount": 99.99 }
1092
+ }]
1093
+ }
1094
+
1095
+ // 2. Pop the message
1096
+ GET /api/v1/pop/queue/orders/partition/high-priority
1097
+
1098
+ // 3. Process the message...
1099
+
1100
+ // 4. Acknowledge completion
1101
+ POST /api/v1/ack
1102
+ {
1103
+ "transactionId": "returned-transaction-id",
1104
+ "status": "completed"
1105
+ }
1106
+ ```
1107
+
1108
+ ---
1109
+
1110
+ ## CORS Support
1111
+
1112
+ All endpoints include CORS headers:
1113
+ - `Access-Control-Allow-Origin: *`
1114
+ - `Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS`
1115
+ - `Access-Control-Allow-Headers: Content-Type, Authorization`
1116
+ - `Access-Control-Max-Age: 86400`