Class: Sinatra::Scheduled::ScheduledContext
- Inherits:
-
Object
- Object
- Sinatra::Scheduled::ScheduledContext
- 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
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#event ⇒ Object
readonly
Returns the value of attribute event.
Instance Method Summary collapse
- #ai ⇒ Object
- #bucket ⇒ Object
- #cf_ctx ⇒ Object
- #cf_env ⇒ Object
- #d1 ⇒ Object
- #db ⇒ Object
- #durable_object(name, id_or_name = nil) ⇒ Object
-
#initialize(env, event, js_ctx) ⇒ ScheduledContext
constructor
A new instance of ScheduledContext.
- #jobs_queue ⇒ Object
- #kv ⇒ Object
-
#logger ⇒ Object
Minimal logger so jobs can puts without crashing in non-Workers test environments.
- #send_email ⇒ Object
-
#wait_until(promise) ⇒ Object
Forward a long-running promise to the Workers runtime so the job can return immediately while the work continues.
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
#env ⇒ Object (readonly)
Returns the value of attribute env.
280 281 282 |
# File 'lib/sinatra/scheduled.rb', line 280 def env @env end |
#event ⇒ Object (readonly)
Returns the value of attribute event.
280 281 282 |
# File 'lib/sinatra/scheduled.rb', line 280 def event @event end |
Instance Method Details
#ai ⇒ Object
294 |
# File 'lib/sinatra/scheduled.rb', line 294 def ai; Cloudflare::Bindings.ai(env); end |
#bucket ⇒ Object
293 |
# File 'lib/sinatra/scheduled.rb', line 293 def bucket; env['cloudflare.BUCKET']; end |
#cf_ctx ⇒ Object
291 |
# File 'lib/sinatra/scheduled.rb', line 291 def cf_ctx; env['cloudflare.ctx']; end |
#cf_env ⇒ Object
290 |
# File 'lib/sinatra/scheduled.rb', line 290 def cf_env; env['cloudflare.env']; end |
#d1 ⇒ Object
288 |
# File 'lib/sinatra/scheduled.rb', line 288 def d1; env['cloudflare.DB']; end |
#db ⇒ Object
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_queue ⇒ Object
296 |
# File 'lib/sinatra/scheduled.rb', line 296 def jobs_queue; env['cloudflare.QUEUE_JOBS']; end |
#kv ⇒ Object
292 |
# File 'lib/sinatra/scheduled.rb', line 292 def kv; env['cloudflare.KV']; end |
#logger ⇒ Object
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_email ⇒ Object
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 |