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.



282
283
284
285
286
# File 'lib/sinatra/scheduled.rb', line 282

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.



280
281
282
# File 'lib/sinatra/scheduled.rb', line 280

def env
  @env
end

#eventObject (readonly)

Returns the value of attribute event.



280
281
282
# File 'lib/sinatra/scheduled.rb', line 280

def event
  @event
end

Instance Method Details

#aiObject



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

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

#bucketObject



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

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

#cf_ctxObject



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

def cf_ctx; env['cloudflare.ctx']; end

#cf_envObject



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

def cf_env; env['cloudflare.env']; end

#d1Object



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

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

#dbObject



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

def db;     d1;                    end

#durable_object(name, id_or_name = nil) ⇒ Object



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

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

#jobs_queueObject



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

def jobs_queue; env['cloudflare.QUEUE_JOBS']; end

#kvObject



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

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

#loggerObject

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



313
314
315
# File 'lib/sinatra/scheduled.rb', line 313

def logger
  @logger ||= LoggerShim.new
end

#send_emailObject



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

def send_email; env['cloudflare.SEND_EMAIL']; 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.



304
305
306
307
308
309
# File 'lib/sinatra/scheduled.rb', line 304

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