Skip to content

Commit eb19ddf

Browse files
committed
chore: wip
1 parent 527b0b8 commit eb19ddf

File tree

12 files changed

+1118
-9
lines changed

12 files changed

+1118
-9
lines changed

docs/intro.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
> A TypeScript Starter Kit that will help you bootstrap your next project without minimal opinion.
66
7-
# bun-bull
7+
# bun-queue
88

99
This is an opinionated TypeScript Starter kit to help kick-start development of your next Bun package.
1010

eslint.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const config: ESLintConfig = stacks({
1212
yaml: true,
1313
ignores: [
1414
'fixtures/**',
15+
'**/examples',
1516
],
1617
})
1718

examples/advanced-features.ts

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import { BatchProcessor, Queue, QueueGroup, QueueObservable } from '../src'
2+
3+
async function main() {
4+
console.log('🚀 Advanced Features Example - Groups, Observables, and Batches')
5+
6+
// Create a few queues
7+
const emailQueue = new Queue<{ to: string, subject: string, body: string }>('email', {
8+
redis: { url: 'redis://localhost:6379' },
9+
})
10+
11+
const notificationQueue = new Queue<{ userId: string, message: string }>('notification', {
12+
redis: { url: 'redis://localhost:6379' },
13+
})
14+
15+
const imageProcessingQueue = new Queue<{ imageUrl: string, filters: string[] }>('image-processing', {
16+
redis: { url: 'redis://localhost:6379' },
17+
})
18+
19+
console.log('✅ Queues created')
20+
21+
// ========================
22+
// Example 1: Batch Processing
23+
// ========================
24+
console.log('\n📦 Example 1: Batch Processing')
25+
26+
// Create a batch processor for the email queue
27+
const batchProcessor = new BatchProcessor(emailQueue)
28+
29+
// Create a batch of email jobs
30+
const emailJobs = [
31+
{ to: 'user1@example.com', subject: 'Welcome!', body: 'Welcome to our platform!' },
32+
{ to: 'user2@example.com', subject: 'Welcome!', body: 'Welcome to our platform!' },
33+
{ to: 'user3@example.com', subject: 'Welcome!', body: 'Welcome to our platform!' },
34+
]
35+
36+
const batch = await batchProcessor.createBatch(emailJobs, {
37+
attempts: 3,
38+
backoff: {
39+
type: 'exponential',
40+
delay: 1000,
41+
},
42+
})
43+
44+
console.log(`📨 Created batch ${batch.id} with ${batch.jobs.length} email jobs`)
45+
46+
// Process the batch
47+
await batchProcessor.processBatch(batch.id, async (jobs) => {
48+
console.log(`🔄 Processing batch of ${jobs.length} emails together`)
49+
50+
// Update progress
51+
await batchProcessor.setBatchProgress(batch.id, 50)
52+
53+
// Simulate batch processing (in a real scenario, you might use a bulk email API)
54+
return jobs.map((job) => {
55+
console.log(`📧 Would send email to ${job.data.to} with subject "${job.data.subject}"`)
56+
return { sent: true, to: job.data.to }
57+
})
58+
})
59+
60+
console.log('✅ Batch processing complete')
61+
62+
// ========================
63+
// Example 2: Queue Groups
64+
// ========================
65+
console.log('\n👥 Example 2: Queue Groups')
66+
67+
// Create a queue group
68+
const queueGroup = new QueueGroup('app', emailQueue.redisClient)
69+
70+
// Add queues to the group
71+
await queueGroup.addQueue(emailQueue, { name: 'notifications', maxConcurrency: 5 })
72+
await queueGroup.addQueue(notificationQueue, { name: 'notifications', maxConcurrency: 5 })
73+
74+
// Add job to all queues in the group
75+
const groupJobs = await queueGroup.addJobToGroup('notifications', {
76+
to: 'all-users@example.com',
77+
subject: 'System Maintenance',
78+
body: 'Our system will be down for maintenance tonight.',
79+
} as any)
80+
81+
console.log(`📝 Added job to all queues in the 'notifications' group, created ${groupJobs.length} jobs`)
82+
83+
// Process jobs in the group (using imported Job type from src)
84+
await queueGroup.processGroup<any>('notifications', async (job) => {
85+
if (job.name === 'email') {
86+
console.log(`📧 Processing email job: ${job.data.subject} to ${job.data.to}`)
87+
return { sent: true }
88+
}
89+
else if (job.name === 'notification') {
90+
console.log(`🔔 Processing notification job: ${job.data.message} to ${job.data.userId}`)
91+
return { delivered: true }
92+
}
93+
return null
94+
})
95+
96+
// ========================
97+
// Example 3: Observables
98+
// ========================
99+
console.log('\n👁️ Example 3: Observables')
100+
101+
// Create an observable for all queues
102+
const observable = new QueueObservable('app', emailQueue.redisClient)
103+
104+
// Create an observable that monitors all queues
105+
const queueObservable = await observable.createObservable(
106+
[emailQueue, notificationQueue, imageProcessingQueue],
107+
{ interval: 2000, autoStart: true },
108+
)
109+
110+
console.log(`👀 Created observable ${queueObservable.id} monitoring ${queueObservable.queues.length} queues`)
111+
112+
// Wait a moment for the first stats to be collected
113+
await new Promise(resolve => setTimeout(resolve, 3000))
114+
115+
// Get stats
116+
const stats = await observable.getObservableStats(queueObservable.id)
117+
console.log('📊 Queue stats:', stats)
118+
119+
// Stop the observable
120+
await observable.stopObservable(queueObservable.id)
121+
console.log(`⏹️ Stopped observable ${queueObservable.id}`)
122+
123+
// Close everything
124+
console.log('\n🧹 Cleaning up...')
125+
await emailQueue.close()
126+
await notificationQueue.close()
127+
await imageProcessingQueue.close()
128+
await queueGroup.closeAll()
129+
await observable.closeAll()
130+
131+
console.log('✅ Done!')
132+
}
133+
134+
main().catch(console.error)

examples/advanced.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Job, Queue, RateLimiter } from '../src'
1+
import { Queue, RateLimiter } from '../src'
22

33
// Define job data types
44
interface EmailJob {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@
5757
"preview:docs": "bun --bun vitepress preview docs",
5858
"typecheck": "bun --bun tsc --noEmit",
5959
"example:basic": "bun examples/basic.ts",
60-
"example:advanced": "bun examples/advanced.ts"
60+
"example:advanced": "bun examples/advanced.ts",
61+
"example:advanced-features": "bun examples/advanced-features.ts"
6162
},
6263
"devDependencies": {
6364
"@stacksjs/docs": "^0.70.23",

0 commit comments

Comments
 (0)