Skip to content

Commit

Permalink
Reuse serf instance
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Apr 28, 2015
1 parent 03375b8 commit a062a20
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions lib/droonga/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ def start
logger.trace("start: start")
@state.on_ready = lambda do
on_ready
serf = Serf.new(@name.to_s)
serf.set_tag(Serf::Tag.internal_node_name, @internal_name)
end
@state.on_failure = lambda do
Expand Down Expand Up @@ -146,12 +145,10 @@ def create_dispatcher

def export_last_message_timestamp_to_cluster
logger.trace("export_last_message_timestamp_to_cluster: start")
@last_message_timestamp ||= read_last_message_timestamp
@last_message_timestamp ||= read_last_message_timestamp_file
if @last_message_timestamp
timestamp = @last_message_timestamp
serf = Serf.new(@name)
old_timestamp = serf.last_message_timestamp
old_timestamp = Time.parse(old_timestamp) if old_timestamp
old_timestamp = read_last_message_timestamp_tag
logger.trace("export_last_message_timestamp_to_cluster: check",
:old => old_timestamp,
:current => @last_message_timestamp)
Expand All @@ -167,7 +164,7 @@ def export_last_message_timestamp_to_cluster

def export_last_message_timestamp_to_file
logger.trace("export_last_message_timestamp_to_file: start")
old_timestamp = read_last_message_timestamp
old_timestamp = read_last_message_timestamp_file
logger.trace("export_last_message_timestamp_to_file: check",
:loaded => old_timestamp,
:current => @last_message_timestamp)
Expand All @@ -189,7 +186,7 @@ def run_last_message_timestamp_observer
path = Path.last_message_timestamp
observer = FileObserver.new(@loop, path)
observer.on_change = lambda do
timestamp = read_last_message_timestamp
timestamp = read_last_message_timestamp_file
logger.trace("last message stamp file is modified",
:loaded => timestamp,
:current => @last_message_timestamp)
Expand All @@ -205,14 +202,24 @@ def run_last_message_timestamp_observer
observer
end

def read_last_message_timestamp
def read_last_message_timestamp_file
file = Path.last_message_timestamp
return nil unless file.exist?
timestamp = file.read
return nil if timestamp.nil? or timestamp.empty?
Time.parse(timestamp)
end

def read_last_message_timestamp_tag
old_timestamp = serf.last_message_timestamp
old_timestamp = Time.parse(old_timestamp) if old_timestamp
old_timestamp
end

def serf
@serf ||= Serf.new(@name.to_s)
end

def log_tag
"engine"
end
Expand Down

0 comments on commit a062a20

Please sign in to comment.