Class: Cloudflare::QueueMessage
- Inherits:
-
Object
- Object
- Cloudflare::QueueMessage
- 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
-
#js ⇒ Object
readonly
Returns the value of attribute js.
Instance Method Summary collapse
-
#ack ⇒ Object
Queues API: mark this message as successfully handled.
-
#body ⇒ Object
Parsed body.
- #id ⇒ Object
-
#initialize(js) ⇒ QueueMessage
constructor
A new instance of QueueMessage.
-
#raw_body ⇒ Object
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).
-
#retry(delay_seconds: nil) ⇒ Object
Queues API: request a retry.
- #timestamp ⇒ Object
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
#js ⇒ Object (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
#ack ⇒ Object
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 |
#body ⇒ Object
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 |
#id ⇒ Object
163 164 165 166 |
# File 'lib/cloudflare_workers/queue.rb', line 163 def id js = @js `(#{js} && #{js}.id ? String(#{js}.id) : '')` end |
#raw_body ⇒ Object
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 |
#timestamp ⇒ Object
168 169 170 171 172 173 |
# File 'lib/cloudflare_workers/queue.rb', line 168 def 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 |