Module: Ruote::StorageBase

Included in:
CompositeStorage, FsStorage, HashStorage
Defined in:
lib/ruote/storage/base.rb

Overview

Base methods for storage implementations.

Instance Method Summary (collapse)

Instance Method Details

- (Object) clear

Used when doing integration tests, removes all msgs, schedules, errors, expressions and workitems.

NOTE that it doesn't remove engine variables (danger)



246
247
248
249
250
251
# File 'lib/ruote/storage/base.rb', line 246

def clear

  %w[ msgs schedules errors expressions workitems ].each do |type|
    purge_type!(type)
  end
end

- (Object) context



35
36
37
38
# File 'lib/ruote/storage/base.rb', line 35

def context

  @context ||= Ruote::Context.new(self)
end

- (Object) context=(c)



40
41
42
43
# File 'lib/ruote/storage/base.rb', line 40

def context=(c)

  @context = c
end

- (Object) copy_to(target, opts = {})

Copies the content of this storage into a target storage.

Of course, the target storage may be a different implementation.



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/ruote/storage/base.rb', line 218

def copy_to(target, opts={})

  counter = 0

  %w[
    configurations errors expressions msgs schedules variables workitems
  ].each do |type|

    ids(type).each do |id|

      item = get(type, id)

      item.delete('_rev')
      target.put(item)

      counter += 1
      puts("  #{type}/#{item['_id']}") if opts[:verbose]
    end
  end

  counter
end

- (Object) delete_schedule(schedule_id)



185
186
187
188
189
190
191
# File 'lib/ruote/storage/base.rb', line 185

def delete_schedule(schedule_id)

  return if schedule_id.nil?

  s = get('schedules', schedule_id)
  delete(s) if s
end

- (Boolean) empty?(type)

Returns:

  • (Boolean)


96
97
98
99
# File 'lib/ruote/storage/base.rb', line 96

def empty?(type)

  (get_many(type, nil, :count => true) == 0)
end

- (Object) expression_wfids(opts)

Given all the expressions stored here, returns a sorted list of unique wfids (this is used in Engine#processes(opts).

Understands the :skip, :limit and :descending options.

This is a base implementation, different storage implementations may come up with different implementations (think CouchDB, which could provide a view for it).



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ruote/storage/base.rb', line 123

def expression_wfids(opts)

  wfids = ids('expressions').collect { |fei| fei.split('!').last }.uniq.sort

  wfids = wfids.reverse if opts[:descending]

  skip = opts[:skip] || 0
  limit = opts[:limit] || wfids.length

  wfids[skip, limit]
end

- (Object) find_root_expression(wfid)

– expressions ++



105
106
107
108
109
110
111
112
# File 'lib/ruote/storage/base.rb', line 105

def find_root_expression(wfid)

  get_many('expressions', wfid).sort_by { |fexp|
    fexp['fei']['expid']
  }.select { |e|
    e['parent_id'].nil?
  }.first
end

- (Object) get_configuration(key)

– configurations ++



57
58
59
60
# File 'lib/ruote/storage/base.rb', line 57

def get_configuration(key)

  get('configurations', key)
end

- (Object) get_engine_variable(k)

– engine variables ++



197
198
199
200
# File 'lib/ruote/storage/base.rb', line 197

def get_engine_variable(k)

  get_engine_variables['variables'][k]
end

- (Object) get_msgs

def get_local_msgs

p @local_msgs
if @local_msgs
  r = @local_msgs
  @local_msgs = nil
  r
else
  []
end

end



87
88
89
90
91
92
93
94
# File 'lib/ruote/storage/base.rb', line 87

def get_msgs

  get_many(
    'msgs', nil, :limit => 300
  ).sort { |a, b|
    a['put_at'] <=> b['put_at']
  }
end

- (Object) get_schedules(delta, now)

– ats and crons ++



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/ruote/storage/base.rb', line 149

def get_schedules(delta, now)

  # TODO : bring that 'optimization' back in,
  #        maybe every minute, if min != last_min ...

  #if delta < 1.0
  #  at = now.strftime('%Y%m%d%H%M%S')
  #  get_many('schedules', /-#{at}$/)
  #elsif delta < 60.0
  #  at = now.strftime('%Y%m%d%H%M')
  #  scheds = get_many('schedules', /-#{at}\d\d$/)
  #  filter_schedules(scheds, now)
  #else # load all the schedules

  scheds = get_many('schedules')
  filter_schedules(scheds, now)

  #end
end

- (Object) get_trackers

– trackers ++



139
140
141
142
143
# File 'lib/ruote/storage/base.rb', line 139

def get_trackers

  get('variables', 'trackers') ||
    { '_id' => 'trackers', 'type' => 'variables', 'trackers' => {} }
end

- (Object) put_engine_variable(k, v)



202
203
204
205
206
207
208
# File 'lib/ruote/storage/base.rb', line 202

def put_engine_variable(k, v)

  vars = get_engine_variables
  vars['variables'][k] = v

  put_engine_variable(k, v) unless put(vars).nil?
end

- (Object) put_msg(action, options)

– messages ++



66
67
68
69
70
71
72
73
74
# File 'lib/ruote/storage/base.rb', line 66

def put_msg(action, options)

  msg = prepare_msg_doc(action, options)

  put(msg)

  #put(msg, :update_rev => true)
  #(@local_msgs ||= []) << Ruote.fulldup(msg)
end

- (Object) put_schedule(flavour, owner_fei, s, msg)

Places schedule in storage. Returns the id of the 'schedule' document. If the schedule got triggered immediately, nil is returned.



172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/ruote/storage/base.rb', line 172

def put_schedule(flavour, owner_fei, s, msg)

  doc = prepare_schedule_doc(flavour, owner_fei, s, msg)

  return nil unless doc

  r = put(doc)

  raise "put_schedule failed" if r != nil

  doc['_id']
end

- (Object) reserve(doc)

Attempts to delete a document, returns true if the deletion succeeded. This is used with msgs to reserve work on them.



48
49
50
51
# File 'lib/ruote/storage/base.rb', line 48

def reserve(doc)

  delete(doc).nil?
end