Class: SayWhen::Storage::MemoryStrategy::Job

Inherits:
Object
  • Object
show all
Includes:
BaseJob
Defined in:
lib/say_when/storage/memory_strategy.rb

Constant Summary

Constants included from BaseJob

BaseJob::STATE_ACQUIRED, BaseJob::STATE_COMPLETE, BaseJob::STATE_ERROR, BaseJob::STATE_EXECUTING, BaseJob::STATE_WAITING

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BaseJob

#execute, #execute_job, #get_task, #load_trigger, #lock, #trigger

Constructor Details

#initialize(options = {}) ⇒ Job

Returns a new instance of Job.



103
104
105
106
107
108
109
110
111
112
113
# 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

Class Method Details

.acquire_lockObject



41
42
43
# File 'lib/say_when/storage/memory_strategy.rb', line 41

def acquire_lock
  @acquire_lock ||= Mutex.new
end

.acquire_next(no_later_than) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# 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) ⇒ Object



77
78
79
80
81
82
83
# 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) ⇒ Object



85
86
87
# 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) ⇒ Object



89
90
91
92
93
94
95
96
# 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

.jobsObject



45
46
47
# File 'lib/say_when/storage/memory_strategy.rb', line 45

def jobs
  @jobs ||= SortedSet.new
end

.propsObject



49
50
51
# File 'lib/say_when/storage/memory_strategy.rb', line 49

def props
  @props ||= []
end

.reset_acquired(older_than_seconds) ⇒ Object



53
54
55
56
57
58
59
60
61
# 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

Instance Method Details

#<=>(job) ⇒ Object



124
125
126
# 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) ⇒ Object



128
129
130
131
# File 'lib/say_when/storage/memory_strategy.rb', line 128

def fired(fired_at=Time.now)
  super
  self.updated_at = Time.now
end

#releaseObject



133
134
135
136
# File 'lib/say_when/storage/memory_strategy.rb', line 133

def release
  super
  self.updated_at = Time.now
end

#saveObject



119
120
121
122
# File 'lib/say_when/storage/memory_strategy.rb', line 119

def save
  self.class.jobs << self
  self
end

#to_hashObject



115
116
117
# 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