-
Notifications
You must be signed in to change notification settings - Fork 80
/
flowexpression.rb
718 lines (527 loc) · 16.3 KB
/
flowexpression.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
#--
# 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/util/time'
require 'ruote/util/ometa'
require 'ruote/util/dollar'
require 'ruote/util/hashdot'
module Ruote::Exp
#
# TODO : document me
#
class FlowExpression
include Ruote::WithH
include Ruote::WithMeta
require 'ruote/exp/ro_persist'
require 'ruote/exp/ro_attributes'
require 'ruote/exp/ro_variables'
COMMON_ATT_KEYS = %w[
if unless forget timeout on_error on_cancel on_timeout ]
attr_reader :context
attr_reader :h
h_reader :variables
h_reader :created_time
h_reader :original_tree
h_reader :updated_tree
h_reader :children
h_reader :state
h_reader :on_error
h_reader :on_cancel
h_reader :on_timeout
def initialize (context, h)
@context = context
@msg = nil
self.h = h
h._id ||= Ruote.to_storage_id(h.fei)
h['type'] ||= 'expressions'
h.name ||= self.class.expression_names.first
h.children ||= []
h.applied_workitem['fei'] = h.fei
h.created_time ||= Ruote.now_to_utc_s
h.on_cancel ||= attribute(:on_cancel)
h.on_error ||= attribute(:on_error)
h.on_timeout ||= attribute(:on_timeout)
end
def h= (hash)
@h = hash
class << h
include Ruote::HashDot
end
end
def fei
Ruote::FlowExpressionId.new(h.fei)
end
def parent_id
h.parent_id ? Ruote::FlowExpressionId.new(h.parent_id) : nil
end
def parent
Ruote::Exp::FlowExpression.fetch(@context, h.parent_id)
end
# Turns this FlowExpression instance into a Hash (well, just hands back
# the base hash behind it.
#
def to_h
@h
end
# Instantiates expression back from hash.
#
def self.from_h (context, h)
exp_class = context.expmap.expression_class(h['name'])
exp_class.new(context, h)
end
# Fetches an expression from the storage and readies it for service.
#
def self.fetch (context, fei)
return nil if fei.nil?
fexp = context.storage.get('expressions', Ruote.to_storage_id(fei))
fexp ? from_h(context, fexp) : nil
end
#--
# META
#++
# Keeping track of names and aliases for the expression
#
def self.names (*exp_names)
exp_names = exp_names.collect { |n| n.to_s }
meta_def(:expression_names) { exp_names }
end
#--
# apply/reply
#++
def self.do_action (context, msg)
fei = msg['fei']
action = msg['action']
if action == 'reply' && fei['engine_id'] != context.engine_id
ep = context.plist.lookup(fei['engine_id'])
raise(
"no EngineParticipant found under name '#{fei['engine_id']}'"
) unless ep
ep.reply(fei, msg['workitem'])
return
end
fexp = nil
3.times do
fexp = fetch(context, msg['fei'])
break if fexp
sleep 0.028
end
# this retry system is only useful with ruote-couch
fexp.send("do_#{action}", msg) if fexp
end
def do_apply
if not Condition.apply?(attribute(:if), attribute(:unless))
return reply_to_parent(h.applied_workitem)
end
if attribute(:forget).to_s == 'true'
i = h.parent_id
wi = Ruote.fulldup(h.applied_workitem)
h.variables = compile_variables
h.parent_id = nil
h.forgotten = true
@context.storage.put_msg('reply', 'fei' => i, 'workitem' => wi)
end
consider_tag
consider_timeout
apply
end
def reply_to_parent (workitem, delete=true)
if h.tagname
unset_variable(h.tagname)
@context.storage.put_msg(
'left_tag', 'tag' => h.tagname, 'fei' => h.fei)
end
if h.timeout_schedule_id && h.state != 'timing_out'
@context.storage.delete_schedule(h.timeout_schedule_id)
end
if h.state == 'failing' # on_error is implicit (#fail got called)
trigger('on_error', workitem)
elsif (h.state == 'cancelling') and h.on_cancel
trigger('on_cancel', workitem)
elsif (h.state == 'timing_out') and h.on_timeout
trigger('on_timeout', workitem)
else # vanilla reply
#unpersist_or_raise if delete
#try_unpersist if delete
if delete
do_unpersist || return
end
if h.parent_id
@context.storage.put_msg(
'reply',
'fei' => h.parent_id,
'workitem' => workitem.merge!('fei' => h.fei),
'updated_tree' => h.updated_tree) # nil most of the time
else
@context.storage.put_msg(
h.forgotten ? 'ceased' : 'terminated',
'wfid' => h.fei['wfid'],
'fei' => h.fei,
'workitem' => workitem)
end
end
end
def do_reply (msg)
@msg = Ruote.fulldup(msg)
# keeping the message, for 'retry' in collision cases
workitem = msg['workitem']
fei = workitem['fei']
if ut = msg['updated_tree']
ct = tree.dup
ct.last[Ruote::FlowExpressionId.child_id(fei)] = ut
update_tree(ct)
end
h.children.delete(fei)
# accept without any check ?
if h.state != nil # failing or timing out ...
if h.children.size < 1
reply_to_parent(workitem)
else
persist_or_raise # for the updated h.children
end
else # vanilla reply
reply(workitem)
end
end
# (only makes sense for the participant expression though)
#
alias :do_receive :do_reply
# A default implementation for all the expressions.
#
def reply (workitem)
reply_to_parent(workitem)
end
# The raw handling of messages passed to expressions (the fine handling
# is done in the #cancel method).
#
def do_cancel (msg)
flavour = msg['flavour']
return if h.state == 'cancelling' && flavour != 'kill'
# cancel on cancel gets discarded
return if h.state == 'failed' && flavour == 'timeout'
# do not timeout expressions that are "in error" (failed)
@msg = Ruote.fulldup(msg)
h.state = case flavour
when 'kill' then 'dying'
when 'timeout' then 'timing_out'
else 'cancelling'
end
h.applied_workitem['fields']['__timed_out__'] = [
h.fei, Ruote.now_to_utc_s
] if h.state == 'timing_out'
if h.state == 'cancelling'
if t = msg['on_cancel']
h.on_cancel = t
elsif hra = msg['re_apply']
hra = {} if hra == true
h.on_cancel = hra['tree'] || tree
if fs = hra['fields']
h.applied_workitem['fields'] = fs
end
if mfs = hra['merge_in_fields']
h.applied_workitem['fields'].merge!(mfs)
end
end
end
cancel(flavour)
end
# This default implementation cancels all the [registered] children
# of this expression.
#
def cancel (flavour)
return reply_to_parent(h.applied_workitem) if h.children.empty?
#
# there are no children, nothing to cancel, let's just reply to
# the parent expression
do_persist || return
#
# before firing the cancel message to the children
#
# if the do_persist returns false, it means it failed, implying this
# expression is stale, let's return, thus discarding this cancel message
children.each do |cfei|
#
# let's send a cancel message to each of the children
#
# maybe some of them are gone or have not yet been applied, anyway,
# the message are sent
@context.storage.put_msg(
'cancel',
'fei' => cfei,
'parent_id' => h.fei, # indicating that this is a "cancel child"
'flavour' => flavour)
end
#if ! children.find { |i| Ruote::Exp::FlowExpression.fetch(@context, i) }
# #
# # since none of the children could be found in the storage right now,
# # it could mean that all children are already done or it could mean
# # that they are not yet applied...
# #
# # just to be sure let's send a new cancel message to this expression
# #
# # it's very important, since if there is no child to cancel the parent
# # the flow might get stuck here
# @context.storage.put_msg(
# 'cancel',
# 'fei' => h.fei,
# 'flavour' => flavour)
#end
end
def do_fail (msg)
@h['state'] = 'failing'
@h['applied_workitem']['fields']['__error__'] = msg['error']
if h.children.size < 1
reply_to_parent(@h['applied_workitem'])
else
persist_or_raise
h.children.each { |i| @context.storage.put_msg('cancel', 'fei' => i) }
end
end
#--
# misc
#++
def launch_sub (pos, subtree, opts={})
i = h.fei.dup
i['sub_wfid'] = get_next_sub_wfid
i['expid'] = pos
#p '=== launch_sub ==='
#p [ :launcher, h.fei['expid'], h.fei['sub_wfid'], h.fei['wfid'] ]
#p [ :launched, i['expid'], i['sub_wfid'], i['wfid'] ]
forget = opts[:forget]
register_child(i) unless forget
variables = (
forget ? compile_variables : {}
).merge!(opts[:variables] || {})
@context.storage.put_msg(
'launch',
'fei' => i,
'parent_id' => forget ? nil : h.fei,
'tree' => subtree,
'workitem' => opts[:workitem] || h.applied_workitem,
'variables' => variables,
'forgotten' => forget)
end
# Returns true if the given fei points to an expression in the parent
# chain of this expression.
#
def ancestor? (fei)
return false unless h.parent_id
return true if h.parent_id == fei
parent.ancestor?(fei)
end
# Looks up "on_error" attribute
#
def lookup_on_error
if h.on_error
self
elsif h.parent_id
par = parent
unless par
puts "~~"
puts "parent gone for"
p h.fei
p h.parent_id
p tree
puts "~~"
end
par ? par.lookup_on_error : nil
else
nil
end
end
# Looks up parent with on_error attribute and triggers it
#
def handle_on_error (error)
return false if h.state == 'failing'
oe_parent = lookup_on_error
return false unless oe_parent
# no parent with on_error attribute found
handler = oe_parent.on_error.to_s
return false if handler == ''
# empty on_error handler nullifies ancestor's on_error
@context.storage.put_msg(
'fail',
'fei' => oe_parent.h.fei,
'error' => [
h.fei, Ruote.now_to_utc_s, error.class.to_s, error.message ])
true # yes, error is being handled.
end
#--
# TREE
#++
# Returns the current version of the tree (returns the updated version
# if it got updated.
#
def tree
h.updated_tree || h.original_tree
end
# Updates the tree of this expression
#
# update_tree(t)
#
# will set the updated tree to t
#
# update_tree
#
# will copy (deep copy) the original tree as the updated_tree.
#
# Adding a child to a sequence expression :
#
# seq.update_tree
# seq.updated_tree[2] << [ 'participant', { 'ref' => 'bob' }, [] ]
# seq.do_persist
#
def update_tree (t=nil)
h.updated_tree = t || Ruote.fulldup(h.original_tree)
end
def name
tree[0]
end
def attributes
tree[1]
end
def tree_children
tree[2]
end
# A tiny class-bound counter used when generating subprocesses ids.
#
@@sub_wfid_counter = -1
# Generates a sub_wfid, without hitting storage.
#
# There's a better implementation for sure...
#
def get_next_sub_wfid
i = [
$$, Time.now.to_f.to_s, self.hash.to_s, @h['fei'].inspect
].join('-').hash
@@sub_wfid_counter = (@@sub_wfid_counter + 1) % 1000
i = i * 1000 + (@@sub_wfid_counter)
(i < 0 ? "1#{i * -1}" : "0#{i}").to_s
end
protected
def to_dot (opts)
i = fei()
label = "#{[ i.wfid, i.sub_wfid, i.expid].join(" ")} #{tree.first}"
label += " (#{h.state})" if h.state
a = []
a << "\"#{i.to_storage_id}\" [ label=\"#{label}\" ];"
# parent
if h.parent_id
a << "\"#{i.to_storage_id}\" -> \"#{parent_id.to_storage_id}\";"
end
# children
h.children.each do |cfei|
a << "\"#{i.to_storage_id}\" -> \"#{Ruote.to_storage_id(cfei)}\";"
end
a
end
def pre_apply_child (child_index, workitem, forget)
child_fei = h.fei.merge('expid' => "#{h.fei['expid']}_#{child_index}")
h.children << child_fei unless forget
msg = {
'fei' => child_fei,
'tree' => tree.last[child_index],
'parent_id' => forget ? nil : h.fei,
'variables' => forget ? compile_variables : nil,
'workitem' => workitem
}
msg['forgotten'] = true if forget
msg
end
def apply_child (child_index, workitem, forget=false)
msg = pre_apply_child(child_index, workitem, forget)
persist_or_raise unless forget
@context.storage.put_msg('apply', msg)
end
def register_child (fei)
h.children << fei
persist_or_raise
end
def consider_tag
if h.tagname = attribute(:tag)
set_variable(h.tagname, h.fei)
@context.storage.put_msg(
'entered_tag', 'tag' => h.tagname, 'fei' => h.fei)
end
end
# Called by do_apply. Overriden in ParticipantExpression.
#
def consider_timeout
do_schedule_timeout(attribute(:timeout))
end
# Called by consider_timeout (FlowExpression) and schedule_timeout
# (ParticipantExpression).
#
def do_schedule_timeout (timeout)
return unless timeout
#h.timeout_at = Ruote.s_to_at(timeout)
#return if not(h.timeout_at) || h.timeout_at < Time.now.utc + 1.0
h.timeout_schedule_id = @context.storage.put_schedule(
'at',
h.fei,
timeout,
'action' => 'cancel',
'fei' => h.fei,
'flavour' => 'timeout')
end
# (Called by trigger_on_cancel & co)
#
def supplant_with (tree, opts)
# at first, nuke self
r = try_unpersist
raise(
"failed to remove exp to supplant "+
"#{Ruote.to_storage_id(h.fei)} #{tree.first}"
) if r.respond_to?(:keys)
# then re-apply
if t = opts['trigger']
tree[1]['_triggered'] = t.to_s
end
@context.storage.put_msg(
'apply',
{ 'fei' => h.fei,
'parent_id' => h.parent_id,
'tree' => tree,
'workitem' => h.applied_workitem,
'variables' => h.variables
}.merge!(opts))
end
# 'on_{error|timeout|cancel}' triggering
#
def trigger (on, workitem)
hon = h[on]
t = hon.is_a?(String) ? [ hon, {}, [] ] : hon
if on == 'on_error'
if hon == 'redo'
t = tree
elsif hon == 'undo'
h.state = 'failed'
reply_to_parent(workitem)
return
end
elsif on == 'on_timeout'
t = tree if hon == 'redo'
end
supplant_with(t, 'trigger' => on)
end
end
end