class SayWhen::Storage::MemoryStrategy::Job
Public Class Methods
acquire_lock()
click to toggle source
# File lib/say_when/storage/memory_strategy.rb, line 41 def acquire_lock @acquire_lock ||= Mutex.new end
acquire_next(no_later_than)
click to toggle source
# File lib/say_when/storage/memory_strategy.rb, line 63 def acquire_next(no_later_than) acquire_lock.synchronize do next_job = jobs.detect(nil) do |j| (j.status == SayWhen::Storage::BaseJob::STATE_WAITING) && (j.next_fire_at.to_i <= no_later_than.to_i) end if next_job next_job.status = SayWhen::Storage::BaseJob::STATE_ACQUIRED next_job.updated_at = Time.now end next_job end end
create(job)
click to toggle source
# File lib/say_when/storage/memory_strategy.rb, line 77 def create(job) if existing_job = find_named_job(job[:group], job[:name]) self.jobs.delete(existing_job) end new(job).save end
find_named_job(group, name)
click to toggle source
# File lib/say_when/storage/memory_strategy.rb, line 85 def find_named_job(group, name) group && name && jobs.detect { |j| j.group == group && j.name == name } end
has_properties(*args)
click to toggle source
# File lib/say_when/storage/memory_strategy.rb, line 89 def has_properties(*args) args.each do |a| unless props.member?(a.to_s) props << a.to_s class_eval { attr_accessor(a.to_sym) } end end end
jobs()
click to toggle source
# File lib/say_when/storage/memory_strategy.rb, line 45 def jobs @jobs ||= SortedSet.new end
new(options = {})
click to toggle source
# File lib/say_when/storage/memory_strategy.rb, line 103 def initialize(options = {}) options.each do |k,v| if self.class.props.member?(k.to_s) send("#{k}=", v) end end self.updated_at = Time.now self.status = STATE_WAITING unless self.status self.next_fire_at = trigger.next_fire_at end
props()
click to toggle source
# File lib/say_when/storage/memory_strategy.rb, line 49 def props @props ||= [] end
reset_acquired(older_than_seconds)
click to toggle source
# File lib/say_when/storage/memory_strategy.rb, line 53 def reset_acquired(older_than_seconds) return unless older_than_seconds.to_i > 0 older_than = (Time.now - older_than_seconds.to_i) acquire_lock.synchronize do jobs.select do |j| j.status == SayWhen::Storage::BaseJob::STATE_ACQUIRED && j.updated_at < older_than end.each{ |j| j.status = SayWhen::Storage::BaseJob::STATE_WAITING } end end
Public Instance Methods
<=>(job)
click to toggle source
# File lib/say_when/storage/memory_strategy.rb, line 124 def <=>(job) self.next_fire_at.to_i <=> job.next_fire_at.to_i end
fired(fired_at=Time.now)
click to toggle source
Calls superclass method
SayWhen::Storage::BaseJob#fired
# File lib/say_when/storage/memory_strategy.rb, line 128 def fired(fired_at=Time.now) super self.updated_at = Time.now end
release()
click to toggle source
Calls superclass method
SayWhen::Storage::BaseJob#release
# File lib/say_when/storage/memory_strategy.rb, line 133 def release super self.updated_at = Time.now end
save()
click to toggle source
# File lib/say_when/storage/memory_strategy.rb, line 119 def save self.class.jobs << self self end
to_hash()
click to toggle source
# File lib/say_when/storage/memory_strategy.rb, line 115 def to_hash [:job_class, :job_method, :data].inject({}) { |h,k| h[k] = send(k); h } end