module LittleDutch

Constants

CONFIG_FILE

Public Instance Methods

config() click to toggle source
# File lib/little-dutch.rb, line 12
def config
  @config ||= Configuration.new(CONFIG_FILE)
end
profile_instance?() click to toggle source
# File lib/little-dutch.rb, line 29
def profile_instance?
  url       = URI.parse(config.endpoint)
  url.path  = "/r/#{config.app_name}/#{config.instance_id}"
  url.query = { token: config.token }.to_param
  Faraday.get(url).status == 200
rescue Faraday::ConnectionFailed
  false
end
run() click to toggle source
# File lib/little-dutch.rb, line 16
def run
  stop
  @runner = Thread.new { run_loop }
end
start() click to toggle source
# File lib/little-dutch.rb, line 21
def start
  config.enabled ? run : :disabled
end
stop() click to toggle source
# File lib/little-dutch.rb, line 25
def stop
  @runner.try(:kill)
end

Private Instance Methods

connection() click to toggle source
# File lib/little-dutch.rb, line 51
def connection
  @connection ||= Faraday.new config.endpoint do |conn|
    conn.request :multipart
    conn.request :url_encoded
    conn.adapter :net_http
  end
end
post_json(file) click to toggle source
# File lib/little-dutch.rb, line 59
  def post_json(file)
    url       = URI.parse(config.endpoint)
    url.path  = "/r/#{config.app_name}/#{config.instance_id}/#{@runcount}"
    url.query = { token: config.token }.to_param
    file_path = file.tap(&:close).path
    curl_cmd  = <<-SHELL.lines.map(&:strip).join(' ; ')
      curl -X POST --retry 10 -H "Transfer-Encoding: chunked" -H "Content-Type: application/text" --data-binary @#{file_path} #{url.to_s}
      rm -f #{file}
    SHELL
    Process.spawn curl_cmd, out: config.logfile, err: config.logfile, in: '/dev/null'
  end
run_loop() click to toggle source
# File lib/little-dutch.rb, line 40
def run_loop
  @runcount = 0
  loop do
    if profile_instance?
      @runcount += 1
      post_json ObjectSpace.dump_all
    end
    sleep config.interval
  end
end