Class: Cloudflare::QueueMessage

Inherits:
Object
  • Object
show all
Defined in:
lib/homura/runtime/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.



177
178
179
# File 'lib/homura/runtime/queue.rb', line 177

def initialize(js)
  @js = js
end

Instance Attribute Details

#jsObject (readonly)

Returns the value of attribute js.



175
176
177
# File 'lib/homura/runtime/queue.rb', line 175

def js
  @js
end

Instance Method Details

#ackObject

Queues API: mark this message as successfully handled.



221
222
223
224
225
# File 'lib/homura/runtime/queue.rb', line 221

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.)



206
207
208
209
210
211
# File 'lib/homura/runtime/queue.rb', line 206

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



181
182
183
184
# File 'lib/homura/runtime/queue.rb', line 181

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).



215
216
217
218
# File 'lib/homura/runtime/queue.rb', line 215

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.



229
230
231
232
233
234
235
236
237
238
# File 'lib/homura/runtime/queue.rb', line 229

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



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

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