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.
Format string used for formating and parsing times
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 43 def self.format_time(time = Time.now) time.strftime(TIME_FORMAT) end
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 51 def self.load_time(time) Time.strptime(time, TIME_FORMAT) end
@param world [World] the world to which the PingCache belongs
# File lib/dynflow/dispatcher/client_dispatcher.rb, line 56 def initialize(world, max_age = DEFAULT_MAX_AGE) @world = world @max_age = max_age @executor = {} end
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 66 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
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 79 def executor?(id) @executor[id] end
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 89 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
# File lib/dynflow/dispatcher/client_dispatcher.rb, line 99 def find_world(id) @world.coordinator.find_records(:id => id, :class => ['Dynflow::Coordinator::ExecutorWorld', 'Dynflow::Coordinator::ClientWorld']).first end