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).

Defined Under Namespace

Classes: BlockParticipantContext

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ ParticipantList

Vanilla service #initialize.



46
47
48
49
# File 'lib/ruote/svc/participant_list.rb', line 46

def initialize(context)

  @context = context
end

Instance Method Details

#clearObject

Clears this participant list.

Used by Engine#register(&block)



310
311
312
313
# File 'lib/ruote/svc/participant_list.rb', line 310

def clear

  self.list=([])
end

#initialize_participant(klass, options) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/ruote/svc/participant_list.rb', line 236

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.



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/ruote/svc/participant_list.rb', line 211

def instantiate(pinfo, opts={})

  return nil unless pinfo

  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.



275
276
277
278
# File 'lib/ruote/svc/participant_list.rb', line 275

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



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/ruote/svc/participant_list.rb', line 287

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.



171
172
173
174
175
176
177
# File 'lib/ruote/svc/participant_list.rb', line 171

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

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

  instantiate(pinfo, opts)
end

#lookup_info(pname, workitem) ⇒ Object

Given a participant name, returns participant details.

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



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/ruote/svc/participant_list.rb', line 184

def lookup_info(pname, workitem)

  return nil unless pname

  wi = workitem ?
    Ruote::Workitem.new(workitem.merge('participant_name' => pname)) :
    nil

  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 Ruote.participant_send(pa, :accept?, 'workitem' => wi)
  end

  # nothing found...

  nil
end

#namesObject

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



251
252
253
254
# File 'lib/ruote/svc/participant_list.rb', line 251

def names

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

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

Registers a participant. Called by Engine#register_participant.



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

def register(name, participant, options, block)

  entry = to_entry(name, participant, options, block)

  key = entry.first
  options = entry.last.last

  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.



258
259
260
261
262
263
264
265
266
267
268
# File 'lib/ruote/svc/participant_list.rb', line 258

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

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

Used by #register and by Ruote::ParticipantRegistrationProxy

Raises:

  • (ArgumentError)


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

def to_entry(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.remap { |(k, v), h|
    h[k.to_s] = case v
      when Symbol then v.to_s
      when Proc then v.to_raw_source
      else v
    end
  }

  extract_blocks(block).each do |meth, code|
    @context.treechecker.block_check(code)
    options[meth] = code
  end

  [
    (name.is_a?(Regexp) ? name : Regexp.new("^#{name}$")).source,
    [ klass, options ]
  ]
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.



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

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