/
worker.rb
406 lines (283 loc) · 9.36 KB
/
worker.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#--
# Copyright (c) 2005-2010, John Mettraux, jmettraux@gmail.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# Made in Japan.
#++
require 'ruote/fei'
module Ruote
#
# Workers fetch 'msgs' and 'schedules' from the storage and process them.
#
# Read more at http://ruote.rubyforge.org/configuration.html
#
class Worker
EXP_ACTIONS = %w[ reply cancel fail receive dispatched ]
# 'apply' is comprised in 'launch'
# 'receive' is a ParticipantExpression alias for 'reply'
PROC_ACTIONS = %w[ cancel_process kill_process ]
attr_reader :storage
attr_reader :context
attr_reader :run_thread
attr_reader :running
def initialize (storage)
@storage = storage
@subscribers = []
@context = Ruote::Context.new(@storage, self)
@last_time = Time.at(0.0).utc # 1970...
@running = true
@run_thread = nil
@msgs = []
@sleep_time = 0.001
end
def run
while(@running) do
step
end
end
def run_in_thread
#Thread.abort_on_exception = true
# TODO : remove me at some point
@running = true
@run_thread = Thread.new { run }
end
def subscribe (actions, subscriber)
@subscribers << [ actions, subscriber ]
end
def shutdown
@running = false
return unless @run_thread
begin
@run_thread.join
rescue Exception => e
end
end
# This method is public, since it's used by the DispatchPool when
# reporting an error that occurred in the dispatch/consume thread of
# a participant.
#
def handle_exception (msg, fexp, ex)
wfid = msg['wfid'] || (msg['fei']['wfid'] rescue nil)
fei = msg['fei'] || (fexp.h.fei rescue nil)
# debug only
if ARGV.include?('-d')
puts "\n== worker intercepted error =="
puts
p ex
ex.backtrace[0, 20].each { |l| puts l }
puts "..."
puts
puts "-- msg --"
msg.keys.sort.each { |k|
puts " #{k.inspect} =>\n#{msg[k].inspect}"
}
puts "-- . --"
puts
end
# on_error ?
if not(fexp) && fei
fexp = Ruote::Exp::FlowExpression.fetch(@context, fei)
end
return if fexp && fexp.handle_on_error(msg, ex)
# emit 'msg'
@storage.put_msg(
'error_intercepted',
'message' => ex.inspect,
'wfid' => wfid,
'msg' => msg)
# fill error in the error journal
@storage.put(
'type' => 'errors',
'_id' => "err_#{Ruote.to_storage_id(fei)}",
'message' => ex.inspect,
'trace' => ex.backtrace.join("\n"),
'fei' => fei,
'msg' => msg
) if fei
end
protected
def step
now = Time.now.utc
delta = now - @last_time
if delta >= 0.8
#
# at most once per second, deal with 'ats' and 'crons'
@last_time = now
@storage.get_schedules(delta, now).each do |sche|
trigger(sche)
end
end
# msgs
@msgs = @storage.get_msgs if @msgs.empty?
processed = 0
collisions = 0
while msg = @msgs.shift
r = process(msg)
if r != false
processed += 1
else
collisions += 1
end
if collisions > 2
@msgs = @msgs[(@msgs.size / 2)..-1] || []
end
#@msgs.concat(@storage.get_local_msgs)
#print r == false ? '*' : '.'
break if Time.now.utc - @last_time >= 0.8
end
#puts processed.to_s
if processed == 0
@sleep_time += 0.001
@sleep_time = 0.499 if @sleep_time > 0.499
sleep(@sleep_time)
else
@sleep_time = 0.001
end
end
def trigger (schedule)
msg = Ruote.fulldup(schedule['msg'])
return false unless @storage.reserve(schedule)
@storage.put_msg(msg.delete('action'), msg)
true
end
def process (msg)
return false if cannot_handle(msg)
return false unless @storage.reserve(msg)
begin
action = msg['action']
if msg['tree']
#
# warning here, it could be a reply, with a 'tree' key...
launch(msg)
elsif EXP_ACTIONS.include?(action)
Ruote::Exp::FlowExpression.do_action(@context, msg)
elsif action.match(/^dispatch/)
@context.dispatch_pool.handle(msg)
elsif PROC_ACTIONS.include?(action)
self.send(action, msg)
#else
# msg got deleted, might still be interesting for a subscriber
end
notify(msg)
rescue Exception => ex
handle_exception(msg, nil, ex)
end
true
end
def notify (msg)
@subscribers.each do |actions, subscriber|
if actions == :all || actions.include?(msg['action'])
subscriber.notify(msg)
end
end
end
# Should always return false. Except when the message is a 'dispatch'
# and it's for a participant only available to an 'engine_worker'
# (block participants, stateful participants)
#
def cannot_handle (msg)
return false if msg['action'] != 'dispatch'
@context.engine.nil? && msg['for_engine_worker?']
end
# Works for both the 'launch' and the 'apply' msgs.
#
def launch (msg)
tree = msg['tree']
variables = msg['variables']
exp_class = @context.expmap.expression_class(tree.first)
# msg['wfid'] only : it's a launch
# msg['fei'] : it's a sub launch (a supplant ?)
exp_hash = {
'fei' => msg['fei'] || {
'engine_id' => @context.engine_id,
'wfid' => msg['wfid'],
'sub_wfid' => msg['sub_wfid'],
'expid' => '0' },
'parent_id' => msg['parent_id'],
'original_tree' => tree,
'variables' => variables,
'applied_workitem' => msg['workitem'],
'forgotten' => msg['forgotten']
}
if not exp_class
exp_class, tree = lookup_subprocess_or_participant(exp_hash)
elsif msg['action'] == 'launch' && exp_class == Ruote::Exp::DefineExpression
def_name, tree = Ruote::Exp::DefineExpression.reorganize(tree)
variables[def_name] = [ '0', tree ] if def_name
exp_class = Ruote::Exp::SequenceExpression
end
if exp_class == Ruote::Exp::SubprocessExpression && tree[1]['engine']
#
# the subprocess has to be transformed into an EngineParticipant...
exp_class = Ruote::Exp::ParticipantExpression
atts = tree[1]
if ref = atts.find { |k, v| v.nil? }
ref = ref.first
atts.delete(ref)
end
atts['pdef'] = atts['ref'] || ref
atts['ref'] = atts.delete('engine')
end
raise_unknown_expression_error(exp_hash) unless exp_class
exp = exp_class.new(@context, exp_hash.merge!('original_tree' => tree))
exp.initial_persist
exp.do_apply
end
def raise_unknown_expression_error (exp_hash)
exp_hash['state'] = 'failed'
#exp_hash['has_error'] = true
Ruote::Exp::RawExpression.new(@context, exp_hash).persist_or_raise
# undigested expression is stored
raise "unknown expression '#{exp_hash['original_tree'].first}'"
end
def lookup_subprocess_or_participant (exp_hash)
tree = exp_hash['original_tree']
key, value = Ruote::Exp::FlowExpression.new(
@context, exp_hash.merge('name' => 'temporary')
).iterative_var_lookup(tree[0])
sub = value
part = @context.plist.lookup_info(key)
sub = key if (not sub) && (not part) && Ruote.is_uri?(key)
# for when a variable points to the URI of a[n external] subprocess
if sub or part
tree[1]['ref'] = key
tree[1]['original_ref'] = tree[0] if key != tree[0]
if sub
[ Ruote::Exp::SubprocessExpression, [ 'subprocess', *tree[1..2] ] ]
else
[ Ruote::Exp::ParticipantExpression, [ 'participant', *tree[1..2] ] ]
end
else
[ nil, tree ]
end
end
def cancel_process (msg)
root = @storage.find_root_expression(msg['wfid'])
return unless root
flavour = (msg['action'] == 'kill_process') ? 'kill' : nil
@storage.put_msg(
'cancel',
'fei' => root['fei'],
'wfid' => msg['wfid'], # indicates this was triggered by cancel_process
'flavour' => flavour)
end
alias :kill_process :cancel_process
end
end