Class: Cloudflare::QueueContext

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

Overview

‘self` inside a `consume_queue do |batch| … end` block. Exposes env / ctx helpers alongside the batch so the block can reach the same D1 / KV / R2 bindings an HTTP route would. This keeps the consumer side consistent with Phase 9’s ScheduledContext.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(batch, js_env, js_ctx) ⇒ QueueContext

Returns a new instance of QueueContext.



417
418
419
420
421
422
# File 'lib/homura/runtime/queue.rb', line 417

def initialize(batch, js_env, js_ctx)
  @batch = batch
  @js_env = js_env
  @js_ctx = js_ctx
  @env = build_env(js_env)
end

Instance Attribute Details

#batchObject (readonly)

Returns the value of attribute batch.



415
416
417
# File 'lib/homura/runtime/queue.rb', line 415

def batch
  @batch
end

#envObject (readonly)

Returns the value of attribute env.



415
416
417
# File 'lib/homura/runtime/queue.rb', line 415

def env
  @env
end

#js_ctxObject (readonly)

Returns the value of attribute js_ctx.



415
416
417
# File 'lib/homura/runtime/queue.rb', line 415

def js_ctx
  @js_ctx
end

#js_envObject (readonly)

Returns the value of attribute js_env.



415
416
417
# File 'lib/homura/runtime/queue.rb', line 415

def js_env
  @js_env
end

Instance Method Details

#aiObject



448
449
450
# File 'lib/homura/runtime/queue.rb', line 448

def ai
  Cloudflare::Bindings.ai(env)
end

#bucketObject



444
445
446
# File 'lib/homura/runtime/queue.rb', line 444

def bucket
  env["cloudflare.BUCKET"]
end

#cf_ctxObject



436
437
438
# File 'lib/homura/runtime/queue.rb', line 436

def cf_ctx
  env["cloudflare.ctx"]
end

#cf_envObject



432
433
434
# File 'lib/homura/runtime/queue.rb', line 432

def cf_env
  env["cloudflare.env"]
end

#d1Object



424
425
426
# File 'lib/homura/runtime/queue.rb', line 424

def d1
  env["cloudflare.DB"]
end

#dbObject



428
429
430
# File 'lib/homura/runtime/queue.rb', line 428

def db
  d1
end

#durable_object(name, id_or_name = nil) ⇒ Object



460
461
462
# File 'lib/homura/runtime/queue.rb', line 460

def durable_object(name, id_or_name = nil)
  Cloudflare::Bindings.durable_object(env, name, id_or_name)
end

#jobs_queueObject



456
457
458
# File 'lib/homura/runtime/queue.rb', line 456

def jobs_queue
  env["cloudflare.QUEUE_JOBS"]
end

#kvObject



440
441
442
# File 'lib/homura/runtime/queue.rb', line 440

def kv
  env["cloudflare.KV"]
end

#send_emailObject



452
453
454
# File 'lib/homura/runtime/queue.rb', line 452

def send_email
  env["cloudflare.SEND_EMAIL"]
end

#wait_until(promise) ⇒ Object

Hand a long-running promise to ctx.waitUntil. Mirrors the same helper in Sinatra::Scheduled’s ScheduledContext.



466
467
468
469
470
471
# File 'lib/homura/runtime/queue.rb', line 466

def wait_until(promise)
  return promise if @js_ctx.nil?
  js_ctx = @js_ctx
  `#{js_ctx}.waitUntil(#{promise})`
  promise
end