Class: Sinatra::Scheduled::Job

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

Overview

A registered scheduled job. Captured at class-definition time; the block is invoked once per matching cron firing.

The block is held both as a Proc (for introspection and the unit tests that just call .block.call) and as an UnboundMethod bound onto a fresh ScheduledContext at fire time. The UnboundMethod path is what makes ‘__await__` inside the block actually await on the Workers runtime — Opal’s ‘# await: true` mode promotes `define_method`’d methods to async functions, but NOT free-standing blocks called via ‘Proc#call` / `instance_exec`. Sinatra itself uses the same trick for its routes (see `Sinatra::Base.generate_method`).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cron:, name:, block:, unbound_method: nil, match_proc: nil, file: nil, line: nil) ⇒ Job

Returns a new instance of Job.



73
74
75
76
77
78
79
80
81
# File 'lib/sinatra/scheduled.rb', line 73

def initialize(cron:, name:, block:, unbound_method: nil, match_proc: nil, file: nil, line: nil)
  @cron = cron.to_s.freeze
  @name = (name || cron).to_s.freeze
  @block = block
  @unbound_method = unbound_method
  @match_proc = match_proc
  @file = file
  @line = line
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



71
72
73
# File 'lib/sinatra/scheduled.rb', line 71

def block
  @block
end

#cronObject (readonly)

Returns the value of attribute cron.



71
72
73
# File 'lib/sinatra/scheduled.rb', line 71

def cron
  @cron
end

#fileObject (readonly)

Returns the value of attribute file.



71
72
73
# File 'lib/sinatra/scheduled.rb', line 71

def file
  @file
end

#lineObject (readonly)

Returns the value of attribute line.



71
72
73
# File 'lib/sinatra/scheduled.rb', line 71

def line
  @line
end

#match_procObject (readonly)

Returns the value of attribute match_proc.



71
72
73
# File 'lib/sinatra/scheduled.rb', line 71

def match_proc
  @match_proc
end

#nameObject (readonly)

Returns the value of attribute name.



71
72
73
# File 'lib/sinatra/scheduled.rb', line 71

def name
  @name
end

#unbound_methodObject (readonly)

Returns the value of attribute unbound_method.



71
72
73
# File 'lib/sinatra/scheduled.rb', line 71

def unbound_method
  @unbound_method
end

Instance Method Details

#matches?(cron_string) ⇒ Boolean

True when this job should fire for the given cron string. The default policy is exact string match, which is what the Workers runtime guarantees. A custom :match proc lets tests use loose matching (e.g. always run, regex match) without touching wrangler.toml.

Returns:

  • (Boolean)


88
89
90
91
# File 'lib/sinatra/scheduled.rb', line 88

def matches?(cron_string)
  return @match_proc.call(cron_string) if @match_proc
  @cron == cron_string.to_s
end