Class: Droonga::Serf::Downloader

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/droonga/serf/downloader.rb

Defined Under Namespace

Classes: DownloadFailed

Constant Summary collapse

MAX_RETRY_COUNT =
5
RETRY_INTERVAL =
10
TARGET_VERSION =
"0.6.3"

Instance Method Summary collapse

Constructor Details

#initialize(output_path) ⇒ Downloader

Returns a new instance of Downloader.



39
40
41
42
# File 'lib/droonga/serf/downloader.rb', line 39

def initialize(output_path)
  @output_path = output_path
  @retry_count = 0
end

Instance Method Details

#downloadObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/droonga/serf/downloader.rb', line 44

def download
  detect_platform
  url_base = "https://dl.bintray.com/mitchellh/serf"
  base_name = "#{TARGET_VERSION}_#{@os}_#{@architecture}.zip"
  connection = Faraday.new(url_base) do |builder|
    builder.response(:follow_redirects)
    builder.adapter(Faraday.default_adapter)
  end
  response = connection.get(base_name)
  absolete_output_path = @output_path.expand_path
  Dir.mktmpdir do |dir|
    Archive::Zip.extract(StringIO.new(response.body),
                         dir,
                         :directories => false)
    FileUtils.mv("#{dir}/serf", absolete_output_path.to_s)
    FileUtils.chmod(0755, absolete_output_path.to_s)
  end
rescue Archive::Zip::UnzipError => archive_error
  logger.warn("Downloaded zip file is broken.",
              :detail => archive_error)
  if @retry_count < MAX_RETRY_COUNT
    @retry_count += 1
    sleep(RETRY_INTERVAL * @retry_count)
    download
  else
    raise DownloadFailed.new("Couldn't download serf executable. Try it later.")
  end
rescue Faraday::ConnectionFailed => network_error
  logger.warn("Connection failed.",
              :detail => network_error)
  if @retry_count < MAX_RETRY_COUNT
    @retry_count += 1
    sleep(RETRY_INTERVAL * @retry_count)
    download
  else
    raise DownloadFailed.new("Couldn't download serf executable. Try it later.")
  end
end