-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚡️ boilerplate to add cloudflare queues
- Loading branch information
Andrew Bierman
committed
Jul 16, 2024
1 parent
bc20b1f
commit e32c992
Showing
3 changed files
with
70 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { MessageBatch } from '@cloudflare/workers-types'; | ||
import { Bindings } from '..'; | ||
|
||
async function handleLogQueue(batch: MessageBatch<Error>, env: Bindings) { | ||
for (const message of batch.messages) { | ||
console.log('Processing log message:', message.body); | ||
// Add your log processing logic here | ||
} | ||
} | ||
|
||
const handleQueueDefault = async ( | ||
batch: MessageBatch<Error>, | ||
env: Bindings, | ||
) => { | ||
console.error(`No handler found for queue: ${batch.queue}`); | ||
// Optionally, implement a default behavior or error handling | ||
}; | ||
|
||
// Add more handler functions as needed | ||
|
||
// Create a Map for handlers | ||
const queueHandlersMap = new Map< | ||
string, | ||
(batch: MessageBatch<Error>, env: Bindings) => Promise<void> | ||
>([ | ||
['log-queue', handleLogQueue], | ||
// Add more handlers here | ||
]); | ||
|
||
export async function queue( | ||
batch: MessageBatch<Error>, | ||
env: Bindings, | ||
): Promise<void> { | ||
try { | ||
const handler = queueHandlersMap.get(batch.queue) || handleQueueDefault; | ||
await handler(batch, env); | ||
} catch (error) { | ||
console.error(`Error processing queue: ${batch.queue}`, error); | ||
// Optionally, add more error handling logic here | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters