Class: Sinatra::Scheduled::ScheduledContext

Inherits:
Object
  • Object
show all
Defined in:
lib/sinatra/scheduled.rb

Overview

ScheduledContext is the ‘self` inside a `schedule do … end` block. It deliberately mirrors only the slice of Sinatra’s request scope that makes sense outside an HTTP context: env, logger, settings, and Cloudflare binding helpers (db / kv / bucket).

Defined Under Namespace

Classes: LoggerShim

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, event, js_ctx) ⇒ ScheduledContext

Returns a new instance of ScheduledContext.



295
296
297
298
299
# File 'lib/sinatra/scheduled.rb', line 295

def initialize(env, event, js_ctx)
  @env = env
  @event = event
  @js_ctx = js_ctx
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



293
294
295
# File 'lib/sinatra/scheduled.rb', line 293

def env
  @env
end

#eventObject (readonly)

Returns the value of attribute event.



293
294
295
# File 'lib/sinatra/scheduled.rb', line 293

def event
  @event
end

Instance Method Details

#bucketObject



303
# File 'lib/sinatra/scheduled.rb', line 303

def bucket; env['cloudflare.BUCKET']; end

#dbObject



301
# File 'lib/sinatra/scheduled.rb', line 301

def db;     env['cloudflare.DB'];     end

#kvObject



302
# File 'lib/sinatra/scheduled.rb', line 302

def kv;     env['cloudflare.KV'];     end

#loggerObject

Minimal logger so jobs can puts without crashing in non-Workers test environments. Falls back to STDOUT.



317
318
319
# File 'lib/sinatra/scheduled.rb', line 317

def logger
  @logger ||= LoggerShim.new
end

#wait_until(promise) ⇒ Object

Forward a long-running promise to the Workers runtime so the job can return immediately while the work continues. Mirrors ‘ctx.waitUntil(promise)` in the JS API.



308
309
310
311
312
313
# File 'lib/sinatra/scheduled.rb', line 308

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