@cloudwerk/queue 0.0.1 → 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.
- package/README.md +131 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# @cloudwerk/queue
|
|
2
|
+
|
|
3
|
+
Type-safe queue consumers for Cloudwerk applications with schema validation, dead letter queues, and batch processing.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @cloudwerk/queue
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
### 1. Define a queue consumer
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
// app/queues/email.ts
|
|
17
|
+
import { defineQueue } from '@cloudwerk/queue'
|
|
18
|
+
|
|
19
|
+
interface EmailMessage {
|
|
20
|
+
to: string
|
|
21
|
+
subject: string
|
|
22
|
+
body: string
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export default defineQueue<EmailMessage>({
|
|
26
|
+
async process(message) {
|
|
27
|
+
await sendEmail(message.body)
|
|
28
|
+
message.ack()
|
|
29
|
+
},
|
|
30
|
+
})
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### 2. Send messages
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
import { queues } from '@cloudwerk/core/bindings'
|
|
37
|
+
|
|
38
|
+
await queues.email.send({
|
|
39
|
+
to: 'user@example.com',
|
|
40
|
+
subject: 'Welcome!',
|
|
41
|
+
body: 'Thanks for signing up.',
|
|
42
|
+
})
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Features
|
|
46
|
+
|
|
47
|
+
### Schema Validation
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
import { z } from 'zod'
|
|
51
|
+
|
|
52
|
+
const EmailSchema = z.object({
|
|
53
|
+
to: z.string().email(),
|
|
54
|
+
subject: z.string().min(1),
|
|
55
|
+
body: z.string(),
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
export default defineQueue<z.infer<typeof EmailSchema>>({
|
|
59
|
+
schema: EmailSchema,
|
|
60
|
+
async process(message) {
|
|
61
|
+
// message.body is validated and typed
|
|
62
|
+
message.ack()
|
|
63
|
+
},
|
|
64
|
+
})
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Configuration
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
export default defineQueue<OrderMessage>({
|
|
71
|
+
config: {
|
|
72
|
+
batchSize: 25,
|
|
73
|
+
maxRetries: 5,
|
|
74
|
+
retryDelay: '2m',
|
|
75
|
+
deadLetterQueue: 'orders-dlq',
|
|
76
|
+
},
|
|
77
|
+
async process(message) { /* ... */ },
|
|
78
|
+
})
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Batch Processing
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
export default defineQueue<AnalyticsEvent>({
|
|
85
|
+
config: { batchSize: 100 },
|
|
86
|
+
async processBatch(messages) {
|
|
87
|
+
await batchInsert(messages.map(m => m.body))
|
|
88
|
+
messages.forEach(m => m.ack())
|
|
89
|
+
},
|
|
90
|
+
})
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Message Handling
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
async process(message) {
|
|
97
|
+
try {
|
|
98
|
+
await processWork(message.body)
|
|
99
|
+
message.ack() // Success - remove from queue
|
|
100
|
+
} catch (error) {
|
|
101
|
+
if (isRetryable(error)) {
|
|
102
|
+
message.retry() // Try again
|
|
103
|
+
} else {
|
|
104
|
+
message.deadLetter() // Send to DLQ
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Sending Messages
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
import { queues } from '@cloudwerk/core/bindings'
|
|
114
|
+
|
|
115
|
+
// Single message
|
|
116
|
+
await queues.email.send({ to: '...', subject: '...', body: '...' })
|
|
117
|
+
|
|
118
|
+
// With delay
|
|
119
|
+
await queues.email.send(message, { delaySeconds: 60 })
|
|
120
|
+
|
|
121
|
+
// Batch
|
|
122
|
+
await queues.notifications.sendBatch([...messages])
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## Documentation
|
|
126
|
+
|
|
127
|
+
For complete documentation, visit the [Cloudwerk Queues Guide](https://cloudwerk.dev/guides/queues/).
|
|
128
|
+
|
|
129
|
+
## License
|
|
130
|
+
|
|
131
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudwerk/queue",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "Queue producers and consumers for Cloudwerk",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"dist"
|
|
19
19
|
],
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@cloudwerk/core": "0.
|
|
21
|
+
"@cloudwerk/core": "0.13.0"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"typescript": "^5.4.0",
|