const workerName = getUserWorkerKebabPathFromQueueName(batch.queue)
const userWorker = dispatchNamespace.get(workerName) as QueueDispatcher
// Passing the batch directly to the queue method will cause a serialization error.
// Serializing using JSON.stringify and then parsing will remove the methods from the batch and messages which are crucial for the user worker
// To work around this, we clone the whole batch and pass it to the user worker queue method
// This way, the user worker can act just like if receiving batch directly from the queue
const clonedMessages: Message[] = batch.messages.map((message) => ({
id: message.id,
timestamp: message.timestamp,
body: message.body,
attempts: message.attempts,
ack: () => message.ack(),
retry: (options?: QueueRetryOptions) => message.retry(options),
}))
const clonedBatch: MessageBatch = {
queue: batch.queue,
messages: clonedMessages,
retryAll: (options?: QueueRetryOptions) => batch.retryAll(options),
ackAll: () => batch.ackAll(),
}
await userWorker.queue(clonedBatch)
const workerName = getUserWorkerKebabPathFromQueueName(batch.queue)
const userWorker = dispatchNamespace.get(workerName) as QueueDispatcher
// Passing the batch directly to the queue method will cause a serialization error.
// Serializing using JSON.stringify and then parsing will remove the methods from the batch and messages which are crucial for the user worker
// To work around this, we clone the whole batch and pass it to the user worker queue method
// This way, the user worker can act just like if receiving batch directly from the queue
const clonedMessages: Message[] = batch.messages.map((message) => ({
id: message.id,
timestamp: message.timestamp,
body: message.body,
attempts: message.attempts,
ack: () => message.ack(),
retry: (options?: QueueRetryOptions) => message.retry(options),
}))
const clonedBatch: MessageBatch = {
queue: batch.queue,
messages: clonedMessages,
retryAll: (options?: QueueRetryOptions) => batch.retryAll(options),
ackAll: () => batch.ackAll(),
}
await userWorker.queue(clonedBatch)