Navigation Menu

Skip to content

Commit

Permalink
Write serf port number file by custom event "change_port"
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Jun 26, 2014
1 parent 7df161f commit a134b44
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion lib/droonga/command/serf_event_handler.rb
Expand Up @@ -33,11 +33,13 @@ def run

def initialize
@serf = ENV["SERF"] || Serf.path
@serf_rpc_address = ENV["SERF_RPC_ADDRESS"] || "127.0.0.1:7373"
@serf_port = ENV["SERF_PORT"] || Serf.default_port
@serf_rpc_address = ENV["SERF_RPC_ADDRESS"] || "127.0.0.1:#{@serf_port}"
end

def run
parse_event
process_event

output_live_nodes
true
Expand All @@ -46,11 +48,22 @@ def run
private
def parse_event
@event_name = ENV["SERF_EVENT"]
@payload = nil
case @event_name
when "user"
@event_name += ":#{ENV["SERF_USER_EVENT"]}"
@payload = $stdin.gets
when "query"
@event_name += ":#{ENV["SERF_USER_QUERY"]}"
@payload = $stdin.gets
end
end

def process_event
if @event_name == "user:change_port" or
@event_name == "query:change_port"
serf_port = @payload.to_i
output_port_file(serf_port)
end
end

Expand All @@ -72,6 +85,14 @@ def output_live_nodes
path = Path.live_nodes
nodes = live_nodes
file_contents = JSON.pretty_generate(nodes)
output(path, file_contents)
end

def output_port_file(port)
output(Serf.port_file, port)
end

def output(path, file_contents)
FileUtils.mkdir_p(path.parent.to_s)
# Don't output the file directly to prevent loading of incomplete file!
Tempfile.open(path.basename.to_s, path.parent.to_s, "w") do |output|
Expand Down

0 comments on commit a134b44

Please sign in to comment.