Class: Patriot::JobStore::InMemoryStore
Overview
a JobStore implementation on memory
Constant Summary
Util::Config::ADMIN_USER_KEY, Util::Config::DEFAULT_CONFIG, Util::Config::DEFAULT_PLUGIN_DIR, Util::Config::INFO_SERVER_PORT_KEY, Util::Config::PASSWORD_KEY, Util::Config::PLUGIN_DIR_KEY, Util::Config::PLUGIN_INIT_SCRIPT, Util::Config::PLUGIN_KEY, Util::Config::PLUGIN_LIB_DIR, Util::Config::USERNAME_KEY, Util::Config::WORKER_HOST_KEY, Util::Config::WORKER_USER_KEY
Instance Method Summary
collapse
-
#acceptable?(job) ⇒ Boolean
-
#delete_job(job_id) ⇒ Object
-
#find_jobs_by_state(state, opts = {}) ⇒ Object
-
#get_consumers(products, opts = {:include_attrs => [Patriot::Command::STATE_ATTR]}) ⇒ Object
-
#get_execution_history(job_id, opts = {}) ⇒ Object
-
#get_graph(job_id, opts = {}) ⇒ Array
get nodes and edges information to render graph.
-
#get_job(job_id) ⇒ Object
-
#get_job_size(opts = {}) ⇒ Object
-
#get_job_tickets(host, nodes, options = {}) ⇒ Object
-
#get_producers(products, opts = {:include_attrs => [Patriot::Command::STATE_ATTR]}) ⇒ Object
-
#initialize(store_id, config) ⇒ InMemoryStore
constructor
A new instance of InMemoryStore.
-
#offer_to_execute(job_ticket) ⇒ Object
-
#register(update_id, jobs) ⇒ Object
-
#report_completion_status(job_ticket) ⇒ Object
-
#set_state(update_id, job_ids, new_state) ⇒ Object
execute_with_retry
#create_logger
#load_config, #load_plugins
Methods inherited from Base
#get, #process_subsequent
Constructor Details
#initialize(store_id, config) ⇒ InMemoryStore
Returns a new instance of InMemoryStore.
Instance Method Details
#acceptable?(job) ⇒ Boolean
60
61
62
63
|
# File 'lib/patriot/job_store/in_memory_store.rb', line 60
def acceptable?(job)
raise "invalid class #{job.class}" unless job.is_a?(Patriot::JobStore::Job)
return true
end
|
#delete_job(job_id) ⇒ Object
302
303
304
305
306
307
308
309
310
|
# File 'lib/patriot/job_store/in_memory_store.rb', line 302
def delete_job(job_id)
job_id = job_id.to_sym
@mutex.synchronize do
@job_states.each{|s,js| js.delete(job_id)}
@jobs.delete(job_id)
@producers.delete(job_id)
@consumers.delete(job_id)
end
end
|
#find_jobs_by_state(state, opts = {}) ⇒ Object
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
# File 'lib/patriot/job_store/in_memory_store.rb', line 190
def find_jobs_by_state(state, opts = {})
all_records = @job_states[state] - [Patriot::JobStore::INITIATOR_JOB_ID]
size = all_records.size
opts = {:limit => size, :offset => 0}.merge(opts)
filter = opts.has_key?(:filter_exp) ? Regexp.new(opts[:filter_exp].gsub(/(?<!\\)%/,'.*').gsub(/(?<!\\)_/,'.')) : nil
result = []
opts[:offset].upto(size).each do |i|
break if i >= size
break if result.size >= opts[:limit]
job_id = all_records[size - 1 - i].to_s
next if !filter.nil? && !filter.match(job_id)
result << job_id
end
return result
end
|
#get_consumers(products, opts = {:include_attrs => [Patriot::Command::STATE_ATTR]}) ⇒ Object
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
# File 'lib/patriot/job_store/in_memory_store.rb', line 173
def get_consumers(products, opts = {:include_attrs => [Patriot::Command::STATE_ATTR]})
opts = {:include_attrs => []}.merge(opts)
products = [products] unless products.is_a?(Array)
consumers = []
products.each{|product|
@consumers.map{|pid, prods|
if prods.include?(product)
job = @jobs[pid].filter_attributes(opts[:include_attrs])
job[:job_id] = pid.to_s
consumers.push(job)
end
}
}
return consumers.uniq
end
|
#get_execution_history(job_id, opts = {}) ⇒ Object
207
208
209
210
|
# File 'lib/patriot/job_store/in_memory_store.rb', line 207
def get_execution_history(job_id, opts = {})
opts = {:limit => 1, :order => :DESC}
return @job_history[job_id.to_sym] || []
end
|
#get_graph(job_id, opts = {}) ⇒ Array
get nodes and edges information to render graph
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
# File 'lib/patriot/job_store/in_memory_store.rb', line 216
def get_graph(job_id, opts = {})
job = get(job_id)
history = get_execution_history(job_id, {})[0]
hashed_job = {
:job_id => job.job_id,
:history => history,
:depth => 0
}.merge(job.attributes)
nodes = {job_id => hashed_job}
edges = []
_set_dependency(
:producers,
opts[:producer_depth],
nodes,
edges,
hashed_job
)
_set_dependency(
:consumers,
opts[:consumer_depth],
nodes,
edges,
hashed_job
)
return {:nodes => nodes, :edges => edges}
end
|
#get_job(job_id) ⇒ Object
149
150
151
152
153
|
# File 'lib/patriot/job_store/in_memory_store.rb', line 149
def get_job(job_id)
return nil if job_id.nil?
raise "string is expected but job_id is a #{job_id.class}" unless job_id.is_a?(String)
return @jobs[job_id.to_sym]
end
|
#get_job_size(opts = {}) ⇒ Object
290
291
292
293
294
295
296
297
298
299
|
# File 'lib/patriot/job_store/in_memory_store.rb', line 290
def get_job_size(opts = {})
opts = {:ignore_states => []}.merge(opts)
sizes = {}
@job_states.each do |s,js|
next if opts[:ignore_states].include?(s)
sizes[s] = js.size
sizes[s] = sizes[s] -1 if s == Patriot::JobStore::JobState::SUCCEEDED
end
return sizes
end
|
#get_job_tickets(host, nodes, options = {}) ⇒ Object
#get_producers(products, opts = {:include_attrs => [Patriot::Command::STATE_ATTR]}) ⇒ Object
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/patriot/job_store/in_memory_store.rb', line 156
def get_producers(products, opts = {:include_attrs => [Patriot::Command::STATE_ATTR]})
opts = {:include_attrs => []}.merge(opts)
products = [products] unless products.is_a?(Array)
producers = []
products.each{|product|
@producers.map{|pid, prods|
if prods.include?(product)
job = @jobs[pid].filter_attributes(opts[:include_attrs])
job[:job_id] = pid.to_s
producers.push(job)
end
}
}
return producers.uniq
end
|
#offer_to_execute(job_ticket) ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/patriot/job_store/in_memory_store.rb', line 86
def offer_to_execute(job_ticket)
job_id = job_ticket.job_id.to_sym
update_id = job_ticket.update_id
@mutex.synchronize do
unless _check_and_set_state(job_id, update_id, Patriot::JobStore::JobState::WAIT, Patriot::JobStore::JobState::RUNNING)
@logger.debug("execution of job: #{job_id} is skipped")
return
end
job = @jobs[job_id]
raise "no entry found for #{job_ticket}" if job.nil?
begin
execution_id = Time.now.to_i
@job_history[job_id] = [{:id => execution_id,
:job_id => job_id.to_s,
:host => job_ticket.exec_host,
:node => job_ticket.exec_node,
:thread => job_ticket.exec_thread,
:begin_at => Time.now
}]
return {:execution_id => execution_id, :command => job.to_command(@config)}
rescue Exception => e
_check_and_set_state(job_id, update_id, Patriot::JobStore::JobState::RUNNING, Patriot::JobStore::JobState::FAILED)
raise e
end
end
end
|
#register(update_id, jobs) ⇒ Object
30
31
32
33
34
35
|
# File 'lib/patriot/job_store/in_memory_store.rb', line 30
def register(update_id, jobs)
jobs.each{|job| raise "#{job.job_id} is not acceptable" unless acceptable?(job) }
@mutex.synchronize do
jobs.each {|job| _upsert(update_id, job) }
end
end
|
#report_completion_status(job_ticket) ⇒ Object
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/patriot/job_store/in_memory_store.rb', line 115
def report_completion_status(job_ticket)
job_id = job_ticket.job_id.to_sym
update_id = job_ticket.update_id
exit_code = job_ticket.exit_code
raise "exit code is not set " if exit_code.nil?
state = Patriot::JobStore::EXIT_CODE_TO_STATE[exit_code]
raise "invalid exit code #{exit_code} " if state.nil?
@mutex.synchronize do
last_history = @job_history[job_id]
raise "illegal state job_history is not set for #{job_id}" if last_history.nil? || last_history.empty?
last_history = last_history[0]
@job_history[job_id] = [last_history.merge({:exit_code => exit_code, :end_at => Time.now, :description => job_ticket.description})]
return _check_and_set_state(job_id, update_id, Patriot::JobStore::JobState::RUNNING, state)
end
end
|
#set_state(update_id, job_ids, new_state) ⇒ Object
134
135
136
137
138
139
140
141
142
143
144
145
146
|
# File 'lib/patriot/job_store/in_memory_store.rb', line 134
def set_state(update_id, job_ids, new_state)
@mutex.synchronize do
job_ids = job_ids.map do |jid|
@jobs[jid.to_sym][Patriot::Command::STATE_ATTR] = new_state
jid.to_sym
end
@job_states.each do |s,jobs|
next if s == new_state
@job_states[s] -= job_ids
end
@job_states[new_state] += job_ids
end
end
|