Class: OnlineGHAProvider

Inherits:
GHAProvider show all
Defined in:
lib/gh-archive.rb

Defined Under Namespace

Classes: Cache, DownloadArchiveException

Instance Method Summary collapse

Methods inherited from GHAProvider

#exclude, #include, #logger=

Methods included from GHAUtils

#each_time, #get_gha_filename, #read_gha_file, #read_gha_file_content

Constructor Details

#initialize(max_retries = 3, proactive = false, proactive_pool_size = 10) ⇒ OnlineGHAProvider

Returns a new instance of OnlineGHAProvider.



124
125
126
127
128
129
130
131
132
# File 'lib/gh-archive.rb', line 124

def initialize(max_retries = 3, proactive = false, proactive_pool_size = 10)
    super()
    
    @max_retries = max_retries
    @proactive = proactive
    @proactive_pool_size = proactive_pool_size
    @pool = Thread.pool(proactive_pool_size)
    @cache = Cache.new
end

Instance Method Details

#cache(current_time) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/gh-archive.rb', line 169

def cache(current_time)
    @logger.info("Full cache. Waiting for some free slot...") if @cache.full?
    while @cache.full?
        sleep 1
    end
    @max_retries.times do
        begin
            filename = self.get_gha_filename(current_time)
            URI.open("http://data.gharchive.org/#{filename}") do |gz|
                content = self.read_gha_file(gz)
                @cache.put(filename, content)
                return
            end
        rescue Errno::ECONNRESET => e
            @logger.warn("A server error temporary prevented the download of #{current_time}: " + e.message)
            next
        rescue OpenURI::HTTPError => e
            code = e.io.status[0]
            if code.start_with?("5")
                @logger.warn("A server error temporary prevented the download of #{current_time}: " + e.message)
                next
            else
                raise e
            end
        end
    end
end

#each(from = Time.gm(2015, 1, 1), to = Time.now) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/gh-archive.rb', line 197

def each(from = Time.gm(2015, 1, 1), to = Time.now)
    if @proactive
        any_ready = Thread.promise
        
        @logger.info("Proactively scheduling download tasks...")
        self.each_time(from, to) do |current_time|
            @pool.process(current_time) do |current_time|
                cache(current_time)
                any_ready << true
                @logger.info("Proactively cached #{current_time}. Cache size: #{@cache.size}")
            end
        end
        
        ~any_ready
        @logger.info("Download tasks successfully scheduled!")
    end
    
    super
end

#get(current_time) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/gh-archive.rb', line 134

def get(current_time)        
    @max_retries.times do
        begin
            filename = self.get_gha_filename(current_time)
            
            if @proactive
                @logger.info("Waiting for cache to have #{current_time}...") unless @cache.has?(filename)
                
                while !@cache.has?(filename)
                    sleep 1
                end

                return @cache.get(filename)
            else
                URI.open("http://data.gharchive.org/#{filename}") do |gz|
                    return self.read_gha_file(gz)
                end
            end
        rescue Errno::ECONNRESET => e
            @logger.warn("A server error temporary prevented the download of #{current_time}: " + e.message)
            next
        rescue OpenURI::HTTPError => e
            code = e.io.status[0]
            if code.start_with?("5")
                @logger.warn("A server error temporary prevented the download of #{current_time}: " + e.message)
                next
            else
                raise e
            end
        end
    end
    
    raise DownloadArchiveException, "Exceeded maximum number of tentative downloads for #{current_time}."
end