Method: Synapse::ConfigGenerator::FileOutput#write_backends_to_file

Defined in:
lib/synapse/config_generator/file_output.rb

#write_backends_to_file(service_name, new_backends) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/synapse/config_generator/file_output.rb', line 44

def write_backends_to_file(service_name, new_backends)
  data_path = File.join(opts['output_directory'], "#{service_name}.json")
  begin
    old_backends = JSON.load(File.read(data_path))
  rescue Errno::ENOENT
    old_backends = nil
  end

  if old_backends == new_backends
    # Prevent modifying the file unless something has actually changed
    # This way clients can set watches on this file and update their
    # internal state only when the smartstack state has actually changed
    return false
  else
    # Atomically write new service configuration file
    temp_path = File.join(opts['output_directory'],
                          ".#{service_name}.json.tmp")
    File.open(temp_path, 'w', 0644) {|f| f.write(new_backends.to_json)}
    FileUtils.mv(temp_path, data_path)
    return true
  end
end