Class: Droonga::SerfDownloader

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

Instance Method Summary collapse

Constructor Details

#initialize(output_path) ⇒ SerfDownloader

Returns a new instance of SerfDownloader.



36
37
38
39
# File 'lib/droonga/serf_downloader.rb', line 36

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

Instance Method Details

#downloadObject



41
42
43
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
# File 'lib/droonga/serf_downloader.rb', line 41

def download
  detect_platform
  version = "0.6.3"
  url_base = "https://dl.bintray.com/mitchellh/serf"
  base_name = "#{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.")
  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.")
  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