-
Notifications
You must be signed in to change notification settings - Fork 602
MQ-1200 Add response body to queue send() and sendBatch() #6354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,46 @@ class WorkerQueue: public jsg::Object { | |
| JSG_STRUCT_TS_OVERRIDE(QueueMetrics); | ||
| }; | ||
|
|
||
| struct SendMetrics { | ||
| double backlogCount; | ||
| double backlogBytes; | ||
| double oldestMessageTimestamp; | ||
| JSG_STRUCT(backlogCount, backlogBytes, oldestMessageTimestamp); | ||
| JSG_STRUCT_TS_OVERRIDE(QueueSendMetrics); | ||
| }; | ||
|
|
||
| struct SendMetadata { | ||
| SendMetrics metrics; | ||
| JSG_STRUCT(metrics); | ||
| JSG_STRUCT_TS_OVERRIDE(QueueSendMetadata); | ||
| }; | ||
|
|
||
| struct SendResponse { | ||
| SendMetadata metadata; | ||
| JSG_STRUCT(metadata); | ||
| JSG_STRUCT_TS_OVERRIDE(QueueSendResponse); | ||
| }; | ||
|
|
||
| struct SendBatchMetrics { | ||
| double backlogCount; | ||
| double backlogBytes; | ||
| double oldestMessageTimestamp; | ||
| JSG_STRUCT(backlogCount, backlogBytes, oldestMessageTimestamp); | ||
| JSG_STRUCT_TS_OVERRIDE(QueueSendBatchMetrics); | ||
| }; | ||
|
|
||
| struct SendBatchMetadata { | ||
| SendBatchMetrics metrics; | ||
| JSG_STRUCT(metrics); | ||
| JSG_STRUCT_TS_OVERRIDE(QueueSendBatchMetadata); | ||
| }; | ||
|
|
||
| struct SendBatchResponse { | ||
| SendBatchMetadata metadata; | ||
| JSG_STRUCT(metadata); | ||
| JSG_STRUCT_TS_OVERRIDE(QueueSendBatchResponse); | ||
| }; | ||
|
|
||
| struct SendOptions { | ||
| // TODO(soon): Support metadata. | ||
|
|
||
|
|
@@ -77,26 +117,39 @@ class WorkerQueue: public jsg::Object { | |
|
|
||
| kj::Promise<void> send(jsg::Lock& js, jsg::JsValue body, jsg::Optional<SendOptions> options); | ||
|
|
||
| jsg::Promise<SendResponse> sendWithResponse(jsg::Lock& js, | ||
| jsg::JsValue body, | ||
| jsg::Optional<SendOptions> options, | ||
| const jsg::TypeHandler<SendResponse>& responseHandler); | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I forget, did we decide to put
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leaving this here for posterity: |
||
| kj::Promise<void> sendBatch(jsg::Lock& js, | ||
| jsg::Sequence<MessageSendRequest> batch, | ||
| jsg::Optional<SendBatchOptions> options); | ||
|
|
||
| jsg::Promise<SendBatchResponse> sendBatchWithResponse(jsg::Lock& js, | ||
| jsg::Sequence<MessageSendRequest> batch, | ||
| jsg::Optional<SendBatchOptions> options, | ||
| const jsg::TypeHandler<SendBatchResponse>& responseHandler); | ||
|
|
||
| jsg::Promise<Metrics> metrics(jsg::Lock& js, const jsg::TypeHandler<Metrics>& metricsHandler); | ||
|
|
||
| JSG_RESOURCE_TYPE(WorkerQueue, CompatibilityFlags::Reader flags) { | ||
| JSG_METHOD(send); | ||
| JSG_METHOD(sendBatch); | ||
| if (flags.getWorkerdExperimental()) { | ||
| JSG_METHOD_NAMED(send, sendWithResponse); | ||
| JSG_METHOD_NAMED(sendBatch, sendBatchWithResponse); | ||
| JSG_METHOD(metrics); | ||
| } else { | ||
| JSG_METHOD(send); | ||
| JSG_METHOD(sendBatch); | ||
| } | ||
|
|
||
| JSG_TS_ROOT(); | ||
| if (flags.getWorkerdExperimental()) { | ||
| JSG_TS_OVERRIDE(Queue<Body = unknown> { | ||
| send(message: Body, options?: QueueSendOptions): Promise<void>; | ||
| send(message: Body, options?: QueueSendOptions): Promise<QueueSendResponse>; | ||
| sendBatch(messages | ||
| : Iterable<MessageSendRequest<Body>>, options ?: QueueSendBatchOptions) | ||
| : Promise<void>; | ||
| : Promise<QueueSendBatchResponse>; | ||
| metrics(): Promise<QueueMetrics>; | ||
| }); | ||
| } else { | ||
|
|
@@ -446,7 +499,10 @@ class QueueCustomEvent final: public WorkerInterface::CustomEvent, public kj::Re | |
| }; | ||
|
|
||
| #define EW_QUEUE_ISOLATE_TYPES \ | ||
| api::WorkerQueue, api::WorkerQueue::SendOptions, api::WorkerQueue::SendBatchOptions, \ | ||
| api::WorkerQueue, api::WorkerQueue::SendMetrics, api::WorkerQueue::SendMetadata, \ | ||
| api::WorkerQueue::SendResponse, api::WorkerQueue::SendBatchMetrics, \ | ||
| api::WorkerQueue::SendBatchMetadata, api::WorkerQueue::SendBatchResponse, \ | ||
| api::WorkerQueue::SendOptions, api::WorkerQueue::SendBatchOptions, \ | ||
| api::WorkerQueue::MessageSendRequest, api::WorkerQueue::Metrics, api::MessageBatchMetrics, \ | ||
| api::MessageBatchMetadata, api::IncomingQueueMessage, api::QueueRetryBatch, \ | ||
| api::QueueRetryMessage, api::QueueResponse, api::QueueRetryOptions, api::QueueMessage, \ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // Copyright (c) 2023 Cloudflare, Inc. | ||
| // Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
| // https://opensource.org/licenses/Apache-2.0 | ||
|
|
||
| import assert from 'node:assert'; | ||
|
|
||
| const SEND_RESPONSE = { | ||
| metadata: { | ||
| metrics: { | ||
| backlogCount: 100, | ||
| backlogBytes: 2048, | ||
| oldestMessageTimestamp: 1000000, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const SEND_BATCH_RESPONSE = { | ||
| metadata: { | ||
| metrics: { | ||
| backlogCount: 200, | ||
| backlogBytes: 4096, | ||
| oldestMessageTimestamp: 2000000, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| export default { | ||
| async fetch(request) { | ||
| const { pathname } = new URL(request.url); | ||
|
|
||
| if (pathname === '/message') { | ||
| const text = await request.text(); | ||
| assert.strictEqual(request.method, 'POST'); | ||
| assert.strictEqual(text, 'abc'); | ||
| return Response.json(SEND_RESPONSE); | ||
| } | ||
|
|
||
| if (pathname === '/batch') { | ||
| assert.strictEqual(request.method, 'POST'); | ||
| const body = await request.json(); | ||
| assert.strictEqual(body.messages.length, 1); | ||
| return Response.json(SEND_BATCH_RESPONSE); | ||
| } | ||
|
|
||
| return new Response('Not Found', { status: 404 }); | ||
| }, | ||
|
|
||
| async test(ctrl, env) { | ||
| const responseBodyEnabled = env.RESPONSE_BODY_FLAG; | ||
|
|
||
| const sendResult = await env.QUEUE.send('abc', { contentType: 'text' }); | ||
| const sendBatchResult = await env.QUEUE.sendBatch([ | ||
| { body: 'def', contentType: 'text' }, | ||
| ]); | ||
|
|
||
| if (responseBodyEnabled) { | ||
| assert.strictEqual(sendResult.metadata.metrics.backlogCount, 100); | ||
| assert.strictEqual(sendResult.metadata.metrics.backlogBytes, 2048); | ||
| assert.strictEqual( | ||
| sendResult.metadata.metrics.oldestMessageTimestamp, | ||
| 1000000 | ||
| ); | ||
|
|
||
| assert.strictEqual(sendBatchResult.metadata.metrics.backlogCount, 200); | ||
| assert.strictEqual(sendBatchResult.metadata.metrics.backlogBytes, 4096); | ||
| assert.strictEqual( | ||
| sendBatchResult.metadata.metrics.oldestMessageTimestamp, | ||
| 2000000 | ||
| ); | ||
| } else { | ||
| assert.strictEqual(sendResult, undefined); | ||
| assert.strictEqual(sendBatchResult, undefined); | ||
| } | ||
| }, | ||
| }; |
Uh oh!
There was an error while loading. Please reload this page.