class Dynflow::Dispatcher::ClientDispatcher::PingCache

Class used for reducing the number of sent Pings among worlds. World’s coordinator record include the time when was the world seen for the last time. This class can be used to query this information and determine whether the record is “fresh enough” or whether the Ping really needs to be sent.

Constants

DEFAULT_MAX_AGE
TIME_FORMAT

Format string used for formating and parsing times

Public Class Methods

format_time(time = Time.now) click to toggle source

Formats time into a string

@param time [Time] the time to format @return [String] the formatted time

# File lib/dynflow/dispatcher/client_dispatcher.rb, line 44
def self.format_time(time = Time.now)
  time.strftime(TIME_FORMAT)
end
load_time(time) click to toggle source

Parses time from a string

@param time [String] the time string to parse @return [Time] the parsed time

# File lib/dynflow/dispatcher/client_dispatcher.rb, line 52
def self.load_time(time)
  Time.strptime(time, TIME_FORMAT)
end
new(world, max_age = DEFAULT_MAX_AGE) click to toggle source

@param world [World] the world to which the PingCache belongs

# File lib/dynflow/dispatcher/client_dispatcher.rb, line 57
def initialize(world, max_age = DEFAULT_MAX_AGE)
  @world = world
  @max_age = max_age
  @executor = {}
end

Public Instance Methods

add_record(id, time = Time.now) click to toggle source

Records when was the world seen into the world’s coordinator record

@param id [String] Id of the world to be added to the cache @param time [Time] Time when was the world last seen

# File lib/dynflow/dispatcher/client_dispatcher.rb, line 67
def add_record(id, time = Time.now)
  record = find_world id
  @executor[id] ||= record.data[:class] == 'Dynflow::Coordinator::ExecutorWorld'
  record.data[:meta].update(:last_seen => self.class.format_time(time))
  @world.coordinator.update_record(record)
end
executor?(id) click to toggle source

Looks into the cache whether the world has an executor

@param id [String] Id of the world @return [TrueClass] if the world has an executor @return [FalseClass] if the world is a client world @return [NilClass] if unknown

# File lib/dynflow/dispatcher/client_dispatcher.rb, line 80
def executor?(id)
  @executor[id]
end
fresh_record?(id) click to toggle source

Loads the coordinator record from the database and checks whether the world was last seen within the time limit

@param id [String] Id of the world to be checked @return [TrueClass] if the world was last seen within the limit @return [FalseClass] if the world was last seen after the limit passed

# File lib/dynflow/dispatcher/client_dispatcher.rb, line 90
def fresh_record?(id)
  record = find_world(id)
  return false if record.nil?
  @executor[id] = record.data[:class] == 'Dynflow::Coordinator::ExecutorWorld'
  time = self.class.load_time(record.data[:meta][:last_seen])
  time >= Time.now - @max_age
end

Private Instance Methods

find_world(id) click to toggle source
# File lib/dynflow/dispatcher/client_dispatcher.rb, line 100
def find_world(id)
  @world.coordinator.find_records(:id => id,
                                  :class => ['Dynflow::Coordinator::ExecutorWorld', 'Dynflow::Coordinator::ClientWorld']).first
end