Class: Cloudflare::QueueMessage

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudflare_workers/queue.rb

Overview

Represents one message inside a queue batch. Wraps the JS message object so user code can call ‘.body`, `.id`, `.ack`, `.retry`.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(js) ⇒ QueueMessage

Returns a new instance of QueueMessage.



159
160
161
# File 'lib/cloudflare_workers/queue.rb', line 159

def initialize(js)
  @js = js
end

Instance Attribute Details

#jsObject (readonly)

Returns the value of attribute js.



157
158
159
# File 'lib/cloudflare_workers/queue.rb', line 157

def js
  @js
end

Instance Method Details

#ackObject

Queues API: mark this message as successfully handled.



202
203
204
205
206
# File 'lib/cloudflare_workers/queue.rb', line 202

def ack
  js = @js
  `(#{js} && typeof #{js}.ack === 'function' ? #{js}.ack() : null)`
  nil
end

#bodyObject

Parsed body. Workers Queues gives us the structured clone of whatever the producer sent, so this is usually already a Ruby Hash / Array after crossing the JS<->Ruby boundary. Raw strings pass through untouched — we deliberately do NOT attempt to ‘JSON.parse` an opaque String here because Workers Queues already structure-clones producer payloads, so a String body means the producer really meant a String. Callers that did encode a JSON string themselves can `JSON.parse(msg.body)` at the call site.

(Copilot review PR #9 flagged the earlier “JSON-looking strings are parsed” comment — fixed by removing that claim.)



187
188
189
190
191
192
# File 'lib/cloudflare_workers/queue.rb', line 187

def body
  return @body if defined?(@body)
  js = @js
  raw = `(#{js} && typeof #{js}.body !== 'undefined' ? #{js}.body : null)`
  @body = js_to_ruby(raw)
end

#idObject



163
164
165
166
# File 'lib/cloudflare_workers/queue.rb', line 163

def id
  js = @js
  `(#{js} && #{js}.id ? String(#{js}.id) : '')`
end

#raw_bodyObject

Raw body as it came from JS — useful when the consumer wants to see the unparsed value (e.g. a JSON string stored as-is).



196
197
198
199
# File 'lib/cloudflare_workers/queue.rb', line 196

def raw_body
  js = @js
  `(#{js} && typeof #{js}.body !== 'undefined' ? #{js}.body : null)`
end

#retry(delay_seconds: nil) ⇒ Object

Queues API: request a retry. ‘delay_seconds:` caps the retry backoff at a specific value.



210
211
212
213
214
215
216
217
218
219
# File 'lib/cloudflare_workers/queue.rb', line 210

def retry(delay_seconds: nil)
  js = @js
  if delay_seconds
    ds = delay_seconds.to_i
    `(#{js} && typeof #{js}.retry === 'function' ? #{js}.retry({ delaySeconds: #{ds} }) : null)`
  else
    `(#{js} && typeof #{js}.retry === 'function' ? #{js}.retry() : null)`
  end
  nil
end

#timestampObject



168
169
170
171
172
173
# File 'lib/cloudflare_workers/queue.rb', line 168

def timestamp
  js = @js
  ms = `(#{js} && #{js}.timestamp && typeof #{js}.timestamp.getTime === 'function' ? #{js}.timestamp.getTime() : null)`
  return nil if `#{ms} == null`
  Time.at(ms.to_f / 1000.0)
end