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

- (ParticipantList) initialize(context)

A new instance of ParticipantList



41
42
43
44
# File 'lib/ruote/svc/participant_list.rb', line 41

def initialize(context)

  @context = context
end

Instance Method Details

- (Object) initialize_participant(klass, options)



198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/ruote/svc/participant_list.rb', line 198

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

- (Object) instantiate(pinfo, opts = {})

Returns an instance of a participant



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

def instantiate(pinfo, opts={})

  #pinfo = @instantiated_participants[pinfo] if pinfo.is_a?(String)
  #if pinfo.respond_to?(:consume)
  #  return (pinfo.respond_to?(irt) ? pinfo : nil) if irt
  #  return pinfo
  #end
  #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

- (Object) list

Used by Engine#participant_list

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



237
238
239
240
# File 'lib/ruote/svc/participant_list.rb', line 237

def list

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

- (Object) list=(pl)

Used by Engine#participant_list=

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

See ParticipantList#list



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/ruote/svc/participant_list.rb', line 249

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
    #
    list=(pl)
  end
end

- (Object) lookup(participant_name, workitem, opts = {})

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

Mostly a combination of #lookup_info and #instantiate.



134
135
136
137
138
139
140
141
# File 'lib/ruote/svc/participant_list.rb', line 134

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

- (Object) lookup_info(pname, workitem)

Given a participant name, returns

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



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 148

def lookup_info(pname, workitem)

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

    next unless pname.match(regex)

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

    return pinfo unless pa

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

  nil
end

- (Object) names

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



213
214
215
216
# File 'lib/ruote/svc/participant_list.rb', line 213

def names

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

- (Object) register(name, participant, options, block)

Registers a participant. Called by Engine#register_participant.

Raises:

  • (ArgumentError)


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

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] = v.is_a?(Symbol) ? v.to_s : v
    h
  }

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

  key = (name.is_a?(Regexp) ? name : Regexp.new("^#{name}$")).source

  entry = [ key, [ klass, options ] ]

  list = get_list

  list['list'].delete_if { |e| e.first == key }

  position = options['position'] || 'last'
  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

- (Object) shutdown

Calls #shutdown on any participant that sports this method.



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

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

- (Object) unregister(name_or_participant)

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

Called usually by Engine#unregister_participant.



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

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