Class: Dynflow::Dispatcher::ClientDispatcher::PingCache

Inherits:
Object
  • Object
show all
Defined in:
lib/dynflow/dispatcher/client_dispatcher.rb

Overview

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.

Constant Summary collapse

TIME_FORMAT =

Format string used for formating and parsing times

'%Y-%m-%d %H:%M:%S.%L'
DEFAULT_MAX_AGE =
60

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(world, max_age = DEFAULT_MAX_AGE) ⇒ PingCache



57
58
59
60
61
# 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

Class Method Details

.format_time(time = Time.now) ⇒ String

Formats time into a string



44
45
46
# File 'lib/dynflow/dispatcher/client_dispatcher.rb', line 44

def self.format_time(time = Time.now)
  time.strftime(TIME_FORMAT)
end

.load_time(time) ⇒ Time

Parses time from a string



52
53
54
# File 'lib/dynflow/dispatcher/client_dispatcher.rb', line 52

def self.load_time(time)
  Time.strptime(time, TIME_FORMAT)
end

Instance Method Details

#add_record(id, time = Time.now) ⇒ Object

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



67
68
69
70
71
72
# 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) ⇒ TrueClass, ...

Looks into the cache whether the world has an executor



80
81
82
# File 'lib/dynflow/dispatcher/client_dispatcher.rb', line 80

def executor?(id)
  @executor[id]
end

#fresh_record?(id) ⇒ TrueClass, FalseClass

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



90
91
92
93
94
95
96
# 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