Class: Taskinator::Persistence::RedisSerializationVisitor

Inherits:
Visitor::Base
  • Object
show all
Defined in:
lib/taskinator/persistence.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conn, instance, base_visitor = self) ⇒ RedisSerializationVisitor

Returns a new instance of RedisSerializationVisitor.



240
241
242
243
244
245
246
247
# File 'lib/taskinator/persistence.rb', line 240

def initialize(conn, instance, base_visitor=self)
  @conn         = conn
  @instance     = instance
  @key          = instance.key
  @root         = base_visitor.instance
  @base_visitor = base_visitor
  @task_count   = 0
end

Instance Attribute Details

#instanceObject (readonly)

the redis connection is passed in since it is in the multi statement mode in order to produce one roundtrip to the redis server



238
239
240
# File 'lib/taskinator/persistence.rb', line 238

def instance
  @instance
end

Instance Method Details

#incr_task_countObject



336
337
338
# File 'lib/taskinator/persistence.rb', line 336

def incr_task_count
  @task_count += 1
end

#task_countObject



332
333
334
# File 'lib/taskinator/persistence.rb', line 332

def task_count
  @task_count
end

#visitObject

the starting point for serializing the instance



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/taskinator/persistence.rb', line 250

def visit
  @hmset = []
  @hmset << @key

  @hmset += [:type, @instance.class.name]

  @instance.accept(self)

  # add the process uuid and root key, for easy access later!
  @hmset += [:process_uuid, @root.uuid]

  # add the default state
  @hmset += [:state, :initial]

  # NB: splat args
  @conn.hmset(*@hmset)

  self
end

#visit_args(attribute) ⇒ Object



320
321
322
323
324
325
326
327
328
329
330
# File 'lib/taskinator/persistence.rb', line 320

def visit_args(attribute)
  values = @instance.send(attribute)
  yaml = Taskinator::Persistence.serialize(values)

  # greater than 2 MB?
  if (yaml.bytesize / (1024.0**2)) > 2
    Taskinator.logger.warn("Large argument data detected for '#{self.to_s}'. Consider using intrinsic types instead, or try to reduce the amount of data provided.")
  end

  @hmset += [attribute, yaml]
end

#visit_attribute(attribute) ⇒ Object



291
292
293
294
# File 'lib/taskinator/persistence.rb', line 291

def visit_attribute(attribute)
  value = @instance.send(attribute)
  @hmset += [attribute, value] if value
end

#visit_attribute_enum(attribute, type) ⇒ Object



301
302
303
# File 'lib/taskinator/persistence.rb', line 301

def visit_attribute_enum(attribute, type)
  visit_attribute(attribute)
end

#visit_attribute_time(attribute) ⇒ Object



296
297
298
299
# File 'lib/taskinator/persistence.rb', line 296

def visit_attribute_time(attribute)
  value = @instance.send(attribute)
  @hmset += [attribute, value.iso8601(3)] if value
end

#visit_process(attribute) ⇒ Object



270
271
272
273
274
275
276
# File 'lib/taskinator/persistence.rb', line 270

def visit_process(attribute)
  process = @instance.send(attribute)
  if process
    @hmset += [attribute, process.uuid]
    RedisSerializationVisitor.new(@conn, process, @base_visitor).visit
  end
end

#visit_process_reference(attribute) ⇒ Object



305
306
307
308
# File 'lib/taskinator/persistence.rb', line 305

def visit_process_reference(attribute)
  process = @instance.send(attribute)
  @hmset += [attribute, process.uuid] if process
end

#visit_task_reference(attribute) ⇒ Object



310
311
312
313
# File 'lib/taskinator/persistence.rb', line 310

def visit_task_reference(attribute)
  task = @instance.send(attribute)
  @hmset += [attribute, task.uuid] if task
end

#visit_tasks(tasks) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/taskinator/persistence.rb', line 278

def visit_tasks(tasks)
  tasks.each do |task|
    RedisSerializationVisitor.new(@conn, task, @base_visitor).visit
    @conn.rpush "#{@key}:tasks", task.uuid
    unless task.is_a?(Task::SubProcess)
      incr_task_count unless self == @base_visitor
      @base_visitor.incr_task_count
    end
  end
  @conn.set("#{@key}.count", tasks.count)
  @conn.set("#{@key}.pending", tasks.count)
end

#visit_type(attribute) ⇒ Object



315
316
317
318
# File 'lib/taskinator/persistence.rb', line 315

def visit_type(attribute)
  type = @instance.send(attribute)
  @hmset += [attribute, type.name] if type
end