Class: Ruote::ParticipantList

Inherits:
Object
  • Object
show all
Defined in:
lib/ruote/svc/participant_list.rb

Overview

Tracking participants to [business] processes.

The methods here are mostly called via the engine (registering / unregistering participants) and via the dispatch_pool (when handing workitems to participants).

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ ParticipantList

Returns a new instance of ParticipantList.



43
44
45
46
# File 'lib/ruote/svc/participant_list.rb', line 43

def initialize(context)

  @context = context
end

Instance Method Details

#clearObject

Clears this participant list.

Used by Engine#register(&block)



294
295
296
297
# File 'lib/ruote/svc/participant_list.rb', line 294

def clear

  self.list=([])
end

#initialize_participant(klass, options) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/ruote/svc/participant_list.rb', line 220

def initialize_participant(klass, options)

  participant = if klass.instance_method(:initialize).arity == 0
    klass.new
  else
    klass.new(options)
  end

  participant.context = @context if participant.respond_to?(:context=)

  participant
end

#instantiate(pinfo, opts = {}) ⇒ Object

Returns an instance of a participant



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/ruote/svc/participant_list.rb', line 197

def instantiate(pinfo, opts={})

  pa_class_name, options = pinfo

  if rp = options['require_path']
    require(rp)
  end
  if lp = options['load_path']
    load(lp)
  end

  pa_class = Ruote.constantize(pa_class_name)
  pa_m = pa_class.instance_methods

  irt = opts[:if_respond_to?]

  if irt && ! (pa_m.include?(irt.to_s) || pa_m.include?(irt.to_sym))
    return nil
  end

  initialize_participant(pa_class, options)
end

#listObject

Used by Engine#participant_list

Returns a representation of this participant list as an array of ParticipantEntry instances.



259
260
261
262
# File 'lib/ruote/svc/participant_list.rb', line 259

def list

  get_list['list'].collect { |e| ParticipantEntry.new(e) }
end

#list=(pl) ⇒ Object

Used by Engine#participant_list=

Takes as input an array of ParticipantEntry instances and updates this participant list with it.

See ParticipantList#list



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/ruote/svc/participant_list.rb', line 271

def list=(pl)

  list = get_list

  list['list'] = pl.collect { |e|
    ParticipantEntry.read(e)
  }.collect { |e|
    e[0] = e[0].source if e[0].is_a?(Regexp)
    e
  }

  if r = @context.storage.put(list)
    #
    # put failed, have to redo it
    #
    self.list=(pl)
  end
end

#lookup(participant_name, workitem, opts = {}) ⇒ Object

Returns a participant instance, or nil if there is no participant for the given participant name.

Mostly a combination of #lookup_info and #instantiate.



159
160
161
162
163
164
165
166
# File 'lib/ruote/svc/participant_list.rb', line 159

def lookup(participant_name, workitem, opts={})

  pinfo = participant_name.is_a?(String) ?
    lookup_info(participant_name, workitem) : participant_name

  pinfo ?
    instantiate(pinfo, opts) : nil
end

#lookup_info(pname, workitem) ⇒ Object

Given a participant name, returns

Returns nil if there is no participant registered that covers the given participant name.



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/ruote/svc/participant_list.rb', line 173

def lookup_info(pname, workitem)

  get_list['list'].each do |regex, pinfo|

    next unless pname.match(regex)

    return pinfo if workitem.nil?

    pa = instantiate(pinfo, :if_respond_to? => :accept?)

    return pinfo if pa.nil?

    return pinfo if pa.accept?(
      Ruote::Workitem.new(workitem.merge('participant_name' => pname))
    )
  end

  # nothing found...

  nil
end

#namesObject

Return a list of names (regex) for the registered participants



235
236
237
238
# File 'lib/ruote/svc/participant_list.rb', line 235

def names

  get_list['list'].collect { |re, pa| re }
end

#register(name, participant, options, block) ⇒ Object

Registers a participant. Called by Engine#register_participant.

Raises:

  • (ArgumentError)


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
# File 'lib/ruote/svc/participant_list.rb', line 50

def register(name, participant, options, block)

  raise(
    ArgumentError.new(
      "can only accept strings (classnames) or classes as participant arg")
  ) unless [ String, Class, NilClass ].include?(participant.class)

  klass = (participant || Ruote::BlockParticipant).to_s

  options = options.inject({}) { |h, (k, v)|

    h[k.to_s] = case v
      when Symbol then v.to_s
      when Proc then v.to_raw_source
      else v
    end

    h
  }

  if block
    options['on_workitem'] = block.to_raw_source
    @context.treechecker.block_check(options['on_workitem'])
  end

  key = (name.is_a?(Regexp) ? name : Regexp.new("^#{name}$")).source
  entry = [ key, [ klass, options ] ]

  list = get_list

  position = options['position'] || options['pos'] || 'last'

  if position == 'before'

    position = list['list'].index { |e| e.first == key } || -1

  elsif position == 'after'

    position = (list['list'].rindex { |e| e.first == key } || -2) + 1

  elsif position == 'over'

    position = list['list'].index { |e| e.first == key } || -1
    list['list'].delete_at(position) unless position == -1

  elsif options.delete('override') != false

    list['list'].delete_if { |e| e.first == key }
      # enforces only one instance of a participant per key/regex
  end

  case position
    when 'last' then list['list'] << entry
    when 'first' then list['list'].unshift(entry)
    when Fixnum then list['list'].insert(position, entry)
    else raise "cannot insert participant at position '#{position}'"
  end

  if r = @context.storage.put(list)
    #
    # if put returns something it means the put failed, have to redo the
    # work...
    #
    return register(name, participant, options, block)
  end

  if entry.last.first == 'Ruote::StorageParticipant'
    Ruote::StorageParticipant.new(@context)
  else
    nil
  end
end

#shutdownObject

Calls #shutdown on any participant that sports this method.



242
243
244
245
246
247
248
249
250
251
252
# File 'lib/ruote/svc/participant_list.rb', line 242

def shutdown

  get_list['list'].each do |re, (kl, op)|

    kl = (Ruote.constantize(kl) rescue nil)

    if (kl.instance_method(:shutdown) rescue false)
      initialize_participant(kl, op).shutdown
    end
  end
end

#unregister(name_or_participant) ⇒ Object

Removes a participant, given via its name or directly from this participant list.

Called usually by Engine#unregister_participant.



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
# File 'lib/ruote/svc/participant_list.rb', line 128

def unregister(name_or_participant)

  code = nil
  entry = nil
  list = get_list

  name_or_participant = name_or_participant.to_s

  entry = list['list'].find { |re, pa| name_or_participant.match(re) }

  return nil unless entry

  code = entry.last if entry.last.is_a?(String)

  list['list'].delete(entry)

  if r = @context.storage.put(list)
    #
    # put failed, have to redo it
    #
    return unregister(name_or_participant)
  end

  entry.first
end