Class: Synapse::FileOutput

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/synapse/file_output.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

configure_logger_for, #log, logger_for

Constructor Details

#initialize(opts) ⇒ FileOutput



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/synapse/file_output.rb', line 9

def initialize(opts)
  unless opts.has_key?("output_directory")
    raise ArgumentError, "flat file generation requires an output_directory key"
  end

  begin
    FileUtils.mkdir_p(opts['output_directory'])
  rescue SystemCallError => err
    raise ArgumentError, "provided output directory #{opts['output_directory']} is not present or creatable"
  end

  @opts = opts
  @name = 'file_output'
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/synapse/file_output.rb', line 7

def name
  @name
end

#optsObject (readonly)

Returns the value of attribute opts.



7
8
9
# File 'lib/synapse/file_output.rb', line 7

def opts
  @opts
end

Instance Method Details

#clean_old_watchers(current_watchers) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/synapse/file_output.rb', line 57

def clean_old_watchers(current_watchers)
  # Cleanup old services that Synapse no longer manages
  FileUtils.cd(@opts['output_directory']) do
    present_files = Dir.glob('*.json')
    managed_files = current_watchers.collect {|watcher| "#{watcher.name}.json"}
    files_to_purge = present_files.select {|svc| not managed_files.include?(svc)}
    log.info "synapse: purging unknown service files #{files_to_purge}" if files_to_purge.length > 0
    FileUtils.rm(files_to_purge)
  end
end

#tick(watchers) ⇒ Object



24
25
# File 'lib/synapse/file_output.rb', line 24

def tick(watchers)
end

#update_config(watchers) ⇒ Object



27
28
29
30
31
32
# File 'lib/synapse/file_output.rb', line 27

def update_config(watchers)
  watchers.each do |watcher|
    write_backends_to_file(watcher.name, watcher.backends)
  end
  clean_old_watchers(watchers)
end

#write_backends_to_file(service_name, new_backends) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/synapse/file_output.rb', line 34

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 sevice 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