Class: SayWhen::Storage::ActiveRecordStrategy::Job
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- SayWhen::Storage::ActiveRecordStrategy::Job
show all
- Includes:
- BaseJob
- Defined in:
- lib/say_when/storage/active_record_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_job, #get_task, #load_trigger, #lock, #trigger
Class Method Details
.acquire_next(no_later_than = nil) ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/say_when/storage/active_record_strategy.rb', line 67
def self.acquire_next(no_later_than = nil)
next_job = nil
no_later_than = (no_later_than || Time.now).in_time_zone('UTC')
check_connection
hide_logging do
SayWhen::Storage::ActiveRecordStrategy::Job.transaction do
next_job = where(status: STATE_WAITING)
.where('next_fire_at < ?', no_later_than)
.order(next_fire_at: 'asc')
.lock(true)
.first
next_job&.update_attribute(:status, STATE_ACQUIRED)
end
end
next_job
end
|
.check_connection ⇒ Object
95
96
97
98
99
100
101
102
103
|
# File 'lib/say_when/storage/active_record_strategy.rb', line 95
def self.check_connection
if ActiveRecord::Base.respond_to?(:connection_handler) && ActiveRecord::Base.connection_handler
ActiveRecord::Base.connection_handler.clear_active_connections!
elsif ActiveRecord::Base.respond_to?(:clear_active_connections!)
ActiveRecord::Base.clear_active_connections!
elsif ActiveRecord::Base.respond_to?(:verify_active_connections!)
ActiveRecord::Base.verify_active_connections!
end
end
|
.find_named_job(group, name) ⇒ Object
63
64
65
|
# File 'lib/say_when/storage/active_record_strategy.rb', line 63
def self.find_named_job(group, name)
group && name && where(name: name, group: group).first
end
|
.hide_logging ⇒ Object
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/say_when/storage/active_record_strategy.rb', line 105
def self.hide_logging
@_null_logger ||= Logger.new(IO::NULL)
old_logger = ::ActiveRecord::Base.logger
begin
::ActiveRecord::Base.logger = @_null_logger
yield
ensure
::ActiveRecord::Base.logger = old_logger
end
end
|
.job_create(job) ⇒ Object
55
56
57
58
59
60
61
|
# File 'lib/say_when/storage/active_record_strategy.rb', line 55
def self.job_create(job)
if existing_job = find_named_job(job[:group], job[:name])
existing_job.tap { |j| j.update(job) }
else
create(job)
end
end
|
.reset_acquired(older_than_seconds) ⇒ Object
88
89
90
91
92
93
|
# File 'lib/say_when/storage/active_record_strategy.rb', line 88
def self.reset_acquired(older_than_seconds)
return unless older_than_seconds.to_i > 0
older_than = (Time.now - older_than_seconds.to_i)
where('status = ? and updated_at < ?', STATE_ACQUIRED, older_than).update_all(status: STATE_WAITING)
end
|
Instance Method Details
#execute ⇒ Object
default impl with some error handling and result recording
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/say_when/storage/active_record_strategy.rb', line 136
def execute
result = nil
if SayWhen.options[:store_executions]
result = execute_with_stored_result
else
begin
result = execute_job(data)
SayWhen.logger.info("complete - job: #{inspect}, result: #{result}")
rescue Object => e
result = "#{e.class.name}: #{e.message}\n\t#{e.backtrace.join("\n\t")}"
SayWhen.logger.error("error - job: #{inspect}, exception: #{result}")
end
end
result
end
|
#execute_with_stored_result ⇒ Object
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
# File 'lib/say_when/storage/active_record_strategy.rb', line 152
def execute_with_stored_result
execution = JobExecution.create(job: self, status: STATE_EXECUTING, start_at: Time.now)
begin
execution.result = execute_job(data)
execution.status = 'complete'
rescue Object => e
execution.result = "#{e.class.name}: #{e.message}\n\t#{e.backtrace.join("\n\t")}"
execution.status = 'error'
end
execution.end_at = Time.now
execution.save!
execution.result
end
|
#fired(fired_at = Time.now) ⇒ Object
121
122
123
124
125
126
|
# File 'lib/say_when/storage/active_record_strategy.rb', line 121
def fired(fired_at = Time.now)
self.class.transaction do
super
save!
end
end
|
#release ⇒ Object
128
129
130
131
132
133
|
# File 'lib/say_when/storage/active_record_strategy.rb', line 128
def release
self.class.transaction do
super
save!
end
end
|
#set_defaults ⇒ Object
116
117
118
119
|
# File 'lib/say_when/storage/active_record_strategy.rb', line 116
def set_defaults
self.status = STATE_WAITING
self.next_fire_at = trigger.next_fire_at
end
|