Class: Teakflake::Server

Inherits:
Object
  • Object
show all
Includes:
LogsForMyFamily::LocalLogger
Defined in:
lib/teakflake/server.rb

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/teakflake/server.rb', line 63

def initialize
  @hostname = Socket.gethostname
  load_application_config!
  LogsForMyFamily.configuration.backends = [FileLogWriter.new("#{ENV['LOGS_DIRECTORY']}/teakflake.log")]
  id_assigner = StaticWorkerId.new(
    ZK::Client::Threaded.new(config[:zookeeper_servers].join(',')),
    config[:datacenter_id],
    ENV['WORKER_ID'].to_i,
    "http://#{Addrinfo.getaddrinfo(@hostname, nil, nil, :STREAM,nil, Socket::AI_CANONNAME).first.canonname}:#{config[:server_port]}",
    ProcessClock.new
  )
  id_assigner.sanity_check_peers
  id_assigner.register_worker_id
  @id_worker = IdWorker.new(id_assigner)
end

Instance Method Details

#call(env) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/teakflake/server.rb', line 79

def call(env)
  parse_json(env)
  case env['PATH_INFO']
  when '/id'
    get_id(env)
  when '/metadata'
    (env)
  else
    get_404(env)
  end
end

#configObject



42
43
44
# File 'lib/teakflake/server.rb', line 42

def config
  @config ||= {}.freeze
end

#load_application_config!Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/teakflake/server.rb', line 46

def load_application_config!
  path = File.join(ENV.fetch('CREDENTIALS_DIRECTORY', 'config'), 'application.yml')
  if File.exists?(path)
    yaml = File.read(path)
    parsed_config = YAML.safe_load(yaml, symbolize_names: true).freeze
    if(parsed_config)
      @config = parsed_config
      parsed_config.each do |(key, value)|
        str_key = key.to_s
        env_key = str_key.upcase
        # Only set ENV_VAR_LIKE config keys in env.
        ENV[env_key] = value.to_s if env_key == str_key
      end
    end
  end
end