Skip to content

Commit

Permalink
remove vcr. threading issues
Browse files Browse the repository at this point in the history
  • Loading branch information
bleonard committed Dec 31, 2012
1 parent bfeddae commit f6316c5
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 33 deletions.
5 changes: 2 additions & 3 deletions Gemfile
Expand Up @@ -6,7 +6,6 @@ gem 'i18n'
gem 'tzinfo'

gem 'octokit'
gem 'faraday'
gem 'vcr'
gem 'terminal-table'
gem 'erubis'
gem 'erubis'
gem 'hashie'
4 changes: 1 addition & 3 deletions Gemfile.lock
Expand Up @@ -27,7 +27,6 @@ GEM
netrc (~> 0.7.7)
terminal-table (1.4.5)
tzinfo (0.3.35)
vcr (2.3.0)

PLATFORMS
ruby
Expand All @@ -36,9 +35,8 @@ DEPENDENCIES
active_support
commander
erubis
faraday
hashie
i18n
octokit
terminal-table
tzinfo
vcr
2 changes: 1 addition & 1 deletion activity.rb
Expand Up @@ -157,7 +157,7 @@ def self.load(username, start_time, end_time)
key = "#{username}/#{start_time.to_i}-#{end_time.to_i}"
out = self.new(key)
if stats = cacher.read(key)
out.import(stats)
### out.import(stats)
end
out
end
Expand Down
2 changes: 1 addition & 1 deletion hubtime.rb
Expand Up @@ -8,9 +8,9 @@
require 'fileutils'
require 'yaml'
require 'erubis'
require 'hashie'

load 'cacher.rb'
load 'vcr.rb'
load 'hub_config.rb'
load 'commit.rb'
load 'github.rb'
Expand Down
39 changes: 23 additions & 16 deletions repo.rb
@@ -1,11 +1,15 @@
# -*- encoding : utf-8 -*-

class Repo
attr_reader :client, :repo_name, :username, :start_time, :end_time
attr_reader :client, :cacher
attr_reader :repo_name, :username, :start_time, :end_time
def initialize(client, repo_name, username, start_time, end_time)
@client = client
@repo_name = repo_name
@username = username
@start_time = start_time
@end_time = end_time
@cacher = Cacher.new("#{repo_name}")
end

def commits(&block)
Expand All @@ -18,14 +22,14 @@ def commits(&block)
end
end

protected

def shas(&block)
eq_count = 5 # number of last five shas to be the same, allows caching even if end-time changes but no new commits
eq_cache_key = nil

cache = Cacher.new("#{repo_name}/#{username}/shas/")
total_cache_key = "#{start_time.to_i}-#{end_time.to_i}"
if cached = cache.read(total_cache_key)
total_cache_key = "#{username}/shas/times/#{start_time.to_i}-#{end_time.to_i}"
if cached = cacher.read(total_cache_key)
# fully cached
cached.each { |sha| block.call sha }
return
Expand All @@ -37,8 +41,8 @@ def shas(&block)
block.call sha

if shas.size == eq_count
eq_cache_key = "#{start_time.to_i}-#{shas[0...eq_count].join("_")}"
if cached = cache.read(eq_cache_key)
eq_cache_key = "#{username}/shas/stamps/#{start_time.to_i}-#{shas[0...eq_count].join("_")}"
if cached = cacher.read(eq_cache_key)
# given them the rest
# MAYBE: could also do this halfway through
# would call, concat, skip and change the key below
Expand All @@ -49,8 +53,8 @@ def shas(&block)
end
end

cache.write(eq_cache_key, shas) if eq_cache_key
cache.write(total_cache_key, shas)
cacher.write(eq_cache_key, shas) if eq_cache_key
cacher.write(total_cache_key, shas)
return
end

Expand Down Expand Up @@ -91,21 +95,24 @@ def commits_window(since_time, until_time)
options[:until] = until_time.iso8601

if Time.now < until_time
# NO VCR because now is in the window
# No caching because now is in the window
return client.commits(repo_name, "master", options)
else
# it's over, cache it
# puts "#{repo_name}/#{username}/commits/#{since_time.to_i}-#{until_time.to_i}"
VCR.use_cassette("#{repo_name}/#{username}/commits/#{since_time.to_i}-#{until_time.to_i}", :record => :new_episodes) do
return client.commits(repo_name, "master", options)
end
cache_key = "#{username}/commits/#{since_time.to_i}-#{until_time.to_i}"
hashie = cacher.read(cache_key)
return hashie if hashie
hashie = client.commits(repo_name, "master", options)
cacher.write(cache_key, hashie)
end
end

def fetch_sha(sha)
VCR.use_cassette("#{repo_name}/shas/#{sha}", :record => :new_episodes) do
return client.commit(repo_name, sha)
end
cache_key = "shas/#{sha}"
hashie = cacher.read(cache_key)
return hashie if hashie
hashie = client.commit(repo_name, sha)
cacher.write(cache_key, hashie)
end


Expand Down
9 changes: 0 additions & 9 deletions vcr.rb

This file was deleted.

0 comments on commit f6316c5

Please sign in to comment.