distributed_cache 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -0
- data/Gemfile.lock +2 -0
- data/VERSION +1 -1
- data/distributed_cache.gemspec +9 -3
- data/lib/distributed_cache/bundle.rb +76 -0
- data/lib/distributed_cache/config.rb +26 -2
- data/lib/distributed_cache/manifest.rb +29 -9
- data/lib/distributed_cache/utils.rb +24 -4
- data/lib/distributed_cache.rb +9 -8
- data/spec/distributed_cache_spec.rb +158 -0
- data/spec/helper.rb +60 -0
- metadata +19 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bae74c33b8445bc66d391fedd024530242ed2bcd
|
4
|
+
data.tar.gz: 2c3e0c5bfbceb79feb61d11e4e499c64b3c56683
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 923dcf7aeadaa1a6abeaa641b5291cae63387c4123a1d3e79158979ad9cf08667dac1075d46f83929977ca5dee7bccc4b050423735b6f5d4641b2c7334f83a53
|
7
|
+
data.tar.gz: 3a407ee819ddf9807510077e772763a5d10200e863681e943a43fb6f6e0daccf8e2219381cead738be88d1624177bc02506aec37adfd9cff7a61d744c643b075
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
data/distributed_cache.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "distributed_cache"
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Doug Youch"]
|
12
|
-
s.date = "2013-
|
12
|
+
s.date = "2013-08-01"
|
13
13
|
s.description = "Creates tar files of cache directories for distribution"
|
14
14
|
s.email = "doug@sessionm.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -28,9 +28,12 @@ Gem::Specification.new do |s|
|
|
28
28
|
"VERSION",
|
29
29
|
"distributed_cache.gemspec",
|
30
30
|
"lib/distributed_cache.rb",
|
31
|
+
"lib/distributed_cache/bundle.rb",
|
31
32
|
"lib/distributed_cache/config.rb",
|
32
33
|
"lib/distributed_cache/manifest.rb",
|
33
|
-
"lib/distributed_cache/utils.rb"
|
34
|
+
"lib/distributed_cache/utils.rb",
|
35
|
+
"spec/distributed_cache_spec.rb",
|
36
|
+
"spec/helper.rb"
|
34
37
|
]
|
35
38
|
s.homepage = "http://github.com/sessionm/distributed_cache"
|
36
39
|
s.licenses = ["MIT"]
|
@@ -45,15 +48,18 @@ Gem::Specification.new do |s|
|
|
45
48
|
s.add_runtime_dependency(%q<autoload>, [">= 0"])
|
46
49
|
s.add_development_dependency(%q<jeweler>, [">= 0"])
|
47
50
|
s.add_development_dependency(%q<rspec>, [">= 0"])
|
51
|
+
s.add_development_dependency(%q<fakeweb>, [">= 0"])
|
48
52
|
else
|
49
53
|
s.add_dependency(%q<autoload>, [">= 0"])
|
50
54
|
s.add_dependency(%q<jeweler>, [">= 0"])
|
51
55
|
s.add_dependency(%q<rspec>, [">= 0"])
|
56
|
+
s.add_dependency(%q<fakeweb>, [">= 0"])
|
52
57
|
end
|
53
58
|
else
|
54
59
|
s.add_dependency(%q<autoload>, [">= 0"])
|
55
60
|
s.add_dependency(%q<jeweler>, [">= 0"])
|
56
61
|
s.add_dependency(%q<rspec>, [">= 0"])
|
62
|
+
s.add_dependency(%q<fakeweb>, [">= 0"])
|
57
63
|
end
|
58
64
|
end
|
59
65
|
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
|
3
|
+
module DistributedCache
|
4
|
+
class Bundle
|
5
|
+
attr_reader :config, :cache_name, :new_files
|
6
|
+
|
7
|
+
def initialize(config, cache_name)
|
8
|
+
@config = config
|
9
|
+
@cache_name = cache_name
|
10
|
+
|
11
|
+
@new_files = []
|
12
|
+
end
|
13
|
+
|
14
|
+
def install
|
15
|
+
download_manifest
|
16
|
+
|
17
|
+
manifest.files.each do |file, md5|
|
18
|
+
tar_file = create_tar_file file
|
19
|
+
if ! valid_tar_file?(tar_file, md5)
|
20
|
+
download_file file, tar_file
|
21
|
+
raise "invalid tar file (#{tar_file}) with md5 (#{md5})" unless valid_tar_file?(tar_file, md5)
|
22
|
+
@new_files << tar_file
|
23
|
+
elsif @new_files.size > 0
|
24
|
+
@new_files << tar_file
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
Dir.chdir(cache_dir) do
|
29
|
+
@new_files.each do |tar_file|
|
30
|
+
DistributedCache::Utils.untar tar_file
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def cache_dir
|
36
|
+
config.cache_dir.tap do |dir|
|
37
|
+
FileUtils.mkdir_p(dir) unless File.exists?(dir)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def valid_tar_file?(file, md5)
|
42
|
+
File.exists?(file) && Digest::MD5.file(file).to_s == md5
|
43
|
+
end
|
44
|
+
|
45
|
+
def create_tar_file(file)
|
46
|
+
"#{config.bundle_dir}/#{file}".tap do |f|
|
47
|
+
dir = File.dirname f
|
48
|
+
FileUtils.mkdir_p(dir) unless File.exists?(dir)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def latest_dir
|
53
|
+
"#{config.bundle_dir}/#{cache_name}".tap do |dir|
|
54
|
+
FileUtils.mkdir_p(dir) unless File.exists?(dir)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def download_manifest
|
59
|
+
download_file "#{cache_name}/latest/manifest.yml", manifest_file
|
60
|
+
end
|
61
|
+
|
62
|
+
def download_file(remote_file, local_file)
|
63
|
+
open(local_file, 'wb+') do |f|
|
64
|
+
f.write open("http://#{config.file_server_with_port}/#{config.remote_bundle_dir}/#{remote_file}", 'rb').read
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def manifest_file
|
69
|
+
"#{latest_dir}/manifest.yml"
|
70
|
+
end
|
71
|
+
|
72
|
+
def manifest
|
73
|
+
@manifest ||= DistributedCache::Manifest.load(config, manifest_file)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -1,7 +1,31 @@
|
|
1
1
|
module DistributedCache
|
2
2
|
class Config < Struct.new(:cache_dir,
|
3
|
-
:
|
4
|
-
:file_servers
|
3
|
+
:bundle_dir,
|
4
|
+
:file_servers,
|
5
|
+
:file_server_port,
|
6
|
+
:remote_bundle_dir
|
5
7
|
)
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
self.file_server_port = 80
|
11
|
+
self.remote_bundle_dir = 'cache/bundles'
|
12
|
+
self.cache_dir = "#{Rails.root}/cache" if defined?(Rails)
|
13
|
+
self.bundle_dir = "#{Rails.root}/bundles" if defined?(Rails)
|
14
|
+
end
|
15
|
+
|
16
|
+
def next_file_server
|
17
|
+
@first_file_server_idx ||= rand(self.file_servers.size)
|
18
|
+
self.file_servers[@first_file_server_idx].tap do
|
19
|
+
@first_file_server_idx = (@first_file_server_idx + 1) % self.file_servers.size
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def file_server_with_port
|
24
|
+
if self.file_server_port == 80
|
25
|
+
next_file_server
|
26
|
+
else
|
27
|
+
"#{next_file_server}:#{self.file_server_port}"
|
28
|
+
end
|
29
|
+
end
|
6
30
|
end
|
7
31
|
end
|
@@ -1,31 +1,45 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
1
3
|
module DistributedCache
|
2
4
|
class Manifest
|
3
5
|
attr_reader :config
|
6
|
+
attr_accessor :data, :date_format
|
4
7
|
|
5
8
|
DEFAULT_DATE_FORMAT = '%Y%m%d'
|
6
9
|
|
7
|
-
module
|
10
|
+
module Field
|
8
11
|
CACHE_NAME = 'cache_name'
|
9
12
|
DATE = 'date'
|
10
13
|
DATE_FORMAT = 'date_format'
|
11
14
|
CACHE_VERSION = 'cache_version'
|
12
15
|
FILES = 'files'
|
13
16
|
end
|
14
|
-
include
|
17
|
+
include Field
|
15
18
|
|
16
|
-
def initialize(config,
|
19
|
+
def initialize(config, name=nil, version=nil)
|
17
20
|
@config = config
|
18
|
-
@
|
19
|
-
CACHE_NAME =>
|
20
|
-
CACHE_VERSION =>
|
21
|
-
DATE => date,
|
21
|
+
@data = {
|
22
|
+
CACHE_NAME => name,
|
23
|
+
CACHE_VERSION => version,
|
22
24
|
FILES => []
|
23
25
|
}
|
24
26
|
load
|
25
27
|
end
|
26
28
|
|
27
29
|
def files
|
28
|
-
data[FILES]
|
30
|
+
@data[FILES]
|
31
|
+
end
|
32
|
+
|
33
|
+
def cache_name
|
34
|
+
@data[CACHE_NAME]
|
35
|
+
end
|
36
|
+
|
37
|
+
def cache_version
|
38
|
+
@data[CACHE_VERSION]
|
39
|
+
end
|
40
|
+
|
41
|
+
def add_file(name, file)
|
42
|
+
@data[FILES] << [name, Digest::MD5.file(file).to_s]
|
29
43
|
end
|
30
44
|
|
31
45
|
def [](key)
|
@@ -51,7 +65,7 @@ module DistributedCache
|
|
51
65
|
end
|
52
66
|
|
53
67
|
def date_format
|
54
|
-
@
|
68
|
+
@date_format || DEFAULT_DATE_FORMAT
|
55
69
|
end
|
56
70
|
|
57
71
|
def date
|
@@ -89,6 +103,12 @@ module DistributedCache
|
|
89
103
|
FileUtils.symlink bundle_dir, latest_bundle_dir
|
90
104
|
end
|
91
105
|
|
106
|
+
def self.load(config, manifest_file)
|
107
|
+
new(config).tap do |manifest|
|
108
|
+
manifest.data = YAML.load_file(manifest_file)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
92
112
|
private
|
93
113
|
|
94
114
|
def load
|
@@ -15,10 +15,30 @@ module DistributedCache
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def self.tar(name, files)
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
18
|
+
cmd =
|
19
|
+
if name[-3..-1] == 'tgz'
|
20
|
+
"tar zcf #{name} #{files}"
|
21
|
+
else
|
22
|
+
"tar cf #{name} #{files}"
|
23
|
+
end
|
24
|
+
|
25
|
+
`#{cmd}`.tap do
|
26
|
+
status = $?
|
27
|
+
raise "failed to (#{cmd}), status #{status}" unless status == 0
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.untar(file)
|
32
|
+
cmd =
|
33
|
+
if file[-3..-1] == 'tgz'
|
34
|
+
"tar zxf #{file}"
|
35
|
+
else
|
36
|
+
"tar xf #{file}"
|
37
|
+
end
|
38
|
+
|
39
|
+
`#{cmd}`.tap do
|
40
|
+
status = $?
|
41
|
+
raise "failed to (#{cmd}), status #{status}" unless status == 0
|
22
42
|
end
|
23
43
|
end
|
24
44
|
end
|
data/lib/distributed_cache.rb
CHANGED
@@ -5,14 +5,11 @@ module DistributedCache
|
|
5
5
|
autoload :Config, 'distributed_cache/config'
|
6
6
|
autoload :Utils, 'distributed_cache/utils'
|
7
7
|
autoload :Manifest, 'distributed_cache/manifest'
|
8
|
+
autoload :Bundle, 'distributed_cache/bundle'
|
8
9
|
|
9
|
-
@@config
|
10
|
+
@@config = nil
|
10
11
|
def self.config
|
11
|
-
@@config ||= DistributedCache::Config.new
|
12
|
-
if defined?(Rails)
|
13
|
-
c.cache_dir = "#{Rails.root}/cache"
|
14
|
-
end
|
15
|
-
end
|
12
|
+
@@config ||= DistributedCache::Config.new
|
16
13
|
end
|
17
14
|
|
18
15
|
def self.configure
|
@@ -24,7 +21,7 @@ module DistributedCache
|
|
24
21
|
# cache_version - this is the version of the klass being saved, guards against changes to model where new fields are added or removed ex: 20130731
|
25
22
|
# update_cache - this method caches data to disk, it is passed the manifest object. This can be used to store things like the last_id. So on later runs you can just pull the new records out of the database.
|
26
23
|
# ex models = self.where("id > #{manifest['last_id'].to_i").all.each { |m| m.cache_model }; manifest['last_id'] = models.map(&:id).max
|
27
|
-
def self.
|
24
|
+
def self.update_remote_cache(klass)
|
28
25
|
raise "cache_dir not configured" if self.config.cache_dir.nil?
|
29
26
|
raise "bundle_dir not configured" if self.config.bundle_dir.nil?
|
30
27
|
|
@@ -35,16 +32,20 @@ module DistributedCache
|
|
35
32
|
return unless klass.update_cache(manifest)
|
36
33
|
|
37
34
|
tar_name = "#{manifest.cache_path}/#{klass.cache_name}-#{manifest.files.size + 1}.#{manifest.full? ? 'tgz' : 'tar'}"
|
38
|
-
manifest.files << tar_name
|
39
35
|
tar_file = "#{config.bundle_dir}/#{tar_name}"
|
40
36
|
Dir.chdir(config.cache_dir) do
|
41
37
|
DistributedCache::Utils.tar tar_file, klass.cache_name
|
42
38
|
end
|
39
|
+
manifest.add_file tar_name, tar_file
|
43
40
|
|
44
41
|
manifest.save
|
45
42
|
manifest.update_latest_bundle_dir
|
46
43
|
end
|
47
44
|
|
45
|
+
def self.update_local_cache(cache_name)
|
46
|
+
DistributedCache::Bundle.new(self.config, cache_name).install
|
47
|
+
end
|
48
|
+
|
48
49
|
def self.sync
|
49
50
|
self.config.file_servers.each do |server|
|
50
51
|
DistributedCache::Utils.rsync config.bundle_dir, server
|
@@ -0,0 +1,158 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
describe DistributedCache do
|
4
|
+
context 'config' do
|
5
|
+
it "should be able to configure itself" do
|
6
|
+
configure_test_distributed_cache
|
7
|
+
|
8
|
+
DistributedCache.config.file_servers.should == ['localhost']
|
9
|
+
DistributedCache.config.cache_dir = "#{BASE_TMP_DIR}/cache"
|
10
|
+
DistributedCache.config.bundle_dir = "#{BASE_TMP_DIR}/bundles"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context "update_remote_cache" do
|
15
|
+
before(:each) do
|
16
|
+
configure_test_distributed_cache
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should create the full cache tar file" do
|
20
|
+
DistributedCache.update_remote_cache CachableKlass
|
21
|
+
DistributedCache::Manifest.new(DistributedCache.config, CachableKlass.cache_name, CachableKlass.cache_version).tap do |manifest|
|
22
|
+
manifest.cache_name.should == CachableKlass.cache_name
|
23
|
+
manifest.cache_version.should == CachableKlass.cache_version
|
24
|
+
manifest.files.size.should == 1
|
25
|
+
manifest.files[0][0].should == "#{CachableKlass.cache_name}/#{Time.now.strftime('%Y%m%d')}/#{CachableKlass.cache_version}/test-1.tgz"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should add the partial data as another tar file" do
|
30
|
+
DistributedCache.update_remote_cache CachableKlass
|
31
|
+
DistributedCache.update_remote_cache CachableKlass
|
32
|
+
DistributedCache::Manifest.new(DistributedCache.config, CachableKlass.cache_name, CachableKlass.cache_version).tap do |manifest|
|
33
|
+
manifest.cache_name.should == CachableKlass.cache_name
|
34
|
+
manifest.cache_version.should == CachableKlass.cache_version
|
35
|
+
manifest.files.size.should == 2
|
36
|
+
manifest.files[0][0].should == "#{CachableKlass.cache_name}/#{Time.now.strftime('%Y%m%d')}/#{CachableKlass.cache_version}/test-1.tgz"
|
37
|
+
manifest.files[1][0].should == "#{CachableKlass.cache_name}/#{Time.now.strftime('%Y%m%d')}/#{CachableKlass.cache_version}/test-2.tar"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "update_local_cache" do
|
43
|
+
def update_remote_cache_for_context_update_local_cache
|
44
|
+
old_bundle_dir = DistributedCache.config.bundle_dir
|
45
|
+
old_cache_dir = DistributedCache.config.cache_dir
|
46
|
+
|
47
|
+
DistributedCache.configure do |config|
|
48
|
+
config.bundle_dir = "#{BASE_TMP_DIR}/remote/cache/bundles"
|
49
|
+
config.cache_dir = "#{BASE_TMP_DIR}/remote/cache/cache"
|
50
|
+
end
|
51
|
+
|
52
|
+
DistributedCache.update_remote_cache CachableKlass
|
53
|
+
|
54
|
+
DistributedCache.configure do |config|
|
55
|
+
config.bundle_dir = old_bundle_dir
|
56
|
+
config.cache_dir = old_cache_dir
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
before(:each) do
|
61
|
+
configure_test_distributed_cache
|
62
|
+
update_remote_cache_for_context_update_local_cache
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should be able to install the full tar" do
|
66
|
+
cache_path = "#{CachableKlass.cache_name}/#{Time.now.strftime('%Y%m%d')}/#{CachableKlass.cache_version}"
|
67
|
+
manifest_file = "#{BASE_TMP_DIR}/remote/cache/bundles/test/latest/manifest.yml"
|
68
|
+
tar_file_1 = "#{BASE_TMP_DIR}/remote/cache/bundles/#{cache_path}/test-1.tgz"
|
69
|
+
|
70
|
+
File.exists?(manifest_file).should be_true
|
71
|
+
File.exists?(tar_file_1).should be_true
|
72
|
+
FakeWeb.register_uri :get, "http://localhost/cache/bundles/test/latest/manifest.yml", :body => manifest_file
|
73
|
+
FakeWeb.register_uri :get, "http://localhost/cache/bundles/#{cache_path}/test-1.tgz", :body => tar_file_1
|
74
|
+
DistributedCache.update_local_cache CachableKlass.cache_name
|
75
|
+
|
76
|
+
[['full1', 'test1'], ['full2', 'test2'], ['full3', 'test3']].each do |name, data|
|
77
|
+
file = "#{DistributedCache.config.cache_dir}/#{CachableKlass.cache_name}/#{name}"
|
78
|
+
File.exists?(file).should be_true
|
79
|
+
File.read(file).should == data
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should use the partial tar override existing cache files" do
|
84
|
+
cache_path = "#{CachableKlass.cache_name}/#{Time.now.strftime('%Y%m%d')}/#{CachableKlass.cache_version}"
|
85
|
+
manifest_file = "#{BASE_TMP_DIR}/remote/cache/bundles/test/latest/manifest.yml"
|
86
|
+
tar_file_1 = "#{BASE_TMP_DIR}/remote/cache/bundles/#{cache_path}/test-1.tgz"
|
87
|
+
tar_file_2 = "#{BASE_TMP_DIR}/remote/cache/bundles/#{cache_path}/test-2.tar"
|
88
|
+
|
89
|
+
File.exists?(manifest_file).should be_true
|
90
|
+
File.exists?(tar_file_1).should be_true
|
91
|
+
FakeWeb.register_uri :get, "http://localhost/cache/bundles/test/latest/manifest.yml", :body => manifest_file
|
92
|
+
FakeWeb.register_uri :get, "http://localhost/cache/bundles/#{cache_path}/test-1.tgz", :body => tar_file_1
|
93
|
+
|
94
|
+
DistributedCache.update_local_cache CachableKlass.cache_name
|
95
|
+
|
96
|
+
[['full1', 'test1'], ['full2', 'test2'], ['full3', 'test3']].each do |name, data|
|
97
|
+
file = "#{DistributedCache.config.cache_dir}/#{CachableKlass.cache_name}/#{name}"
|
98
|
+
File.exists?(file).should be_true
|
99
|
+
File.read(file).should == data
|
100
|
+
end
|
101
|
+
|
102
|
+
update_remote_cache_for_context_update_local_cache
|
103
|
+
|
104
|
+
FakeWeb.clean_registry
|
105
|
+
File.exists?(tar_file_2).should be_true
|
106
|
+
FakeWeb.register_uri :get, "http://localhost/cache/bundles/test/latest/manifest.yml", :body => manifest_file
|
107
|
+
FakeWeb.register_uri :get, "http://localhost/cache/bundles/#{cache_path}/test-2.tar", :body => tar_file_2
|
108
|
+
|
109
|
+
DistributedCache.update_local_cache CachableKlass.cache_name
|
110
|
+
|
111
|
+
[['full1', 'test1'], ['full2', 'changed2'], ['full3', 'test3'], ['partial1', '1'], ['partial2', '2']].each do |name, data|
|
112
|
+
file = "#{DistributedCache.config.cache_dir}/#{CachableKlass.cache_name}/#{name}"
|
113
|
+
File.exists?(file).should be_true
|
114
|
+
File.read(file).should == data
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
it "should download the tar file if it is missing locally and untar all the files after it" do
|
119
|
+
update_remote_cache_for_context_update_local_cache
|
120
|
+
|
121
|
+
cache_path = "#{CachableKlass.cache_name}/#{Time.now.strftime('%Y%m%d')}/#{CachableKlass.cache_version}"
|
122
|
+
manifest_file = "#{BASE_TMP_DIR}/remote/cache/bundles/test/latest/manifest.yml"
|
123
|
+
tar_file_1 = "#{BASE_TMP_DIR}/remote/cache/bundles/#{cache_path}/test-1.tgz"
|
124
|
+
tar_file_2 = "#{BASE_TMP_DIR}/remote/cache/bundles/#{cache_path}/test-2.tar"
|
125
|
+
|
126
|
+
File.exists?(manifest_file).should be_true
|
127
|
+
File.exists?(tar_file_1).should be_true
|
128
|
+
File.exists?(tar_file_2).should be_true
|
129
|
+
FakeWeb.register_uri :get, "http://localhost/cache/bundles/test/latest/manifest.yml", :body => manifest_file
|
130
|
+
FakeWeb.register_uri :get, "http://localhost/cache/bundles/#{cache_path}/test-1.tgz", :body => tar_file_1
|
131
|
+
FakeWeb.register_uri :get, "http://localhost/cache/bundles/#{cache_path}/test-2.tar", :body => tar_file_2
|
132
|
+
|
133
|
+
DistributedCache.update_local_cache CachableKlass.cache_name
|
134
|
+
|
135
|
+
[['full1', 'test1'], ['full2', 'changed2'], ['full3', 'test3'], ['partial1', '1'], ['partial2', '2']].each do |name, data|
|
136
|
+
file = "#{DistributedCache.config.cache_dir}/#{CachableKlass.cache_name}/#{name}"
|
137
|
+
File.exists?(file).should be_true
|
138
|
+
File.read(file).should == data
|
139
|
+
end
|
140
|
+
|
141
|
+
local_tar_file_1 = "#{DistributedCache.config.bundle_dir}/#{cache_path}/test-1.tgz"
|
142
|
+
File.exists?(local_tar_file_1).should be_true
|
143
|
+
File.unlink local_tar_file_1
|
144
|
+
|
145
|
+
FakeWeb.clean_registry
|
146
|
+
FakeWeb.register_uri :get, "http://localhost/cache/bundles/test/latest/manifest.yml", :body => manifest_file
|
147
|
+
FakeWeb.register_uri :get, "http://localhost/cache/bundles/#{cache_path}/test-1.tgz", :body => tar_file_1
|
148
|
+
|
149
|
+
DistributedCache.update_local_cache CachableKlass.cache_name
|
150
|
+
|
151
|
+
[['full1', 'test1'], ['full2', 'changed2'], ['full3', 'test3'], ['partial1', '1'], ['partial2', '2']].each do |name, data|
|
152
|
+
file = "#{DistributedCache.config.cache_dir}/#{CachableKlass.cache_name}/#{name}"
|
153
|
+
File.exists?(file).should be_true
|
154
|
+
File.read(file).should == data
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
data/spec/helper.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
|
4
|
+
begin
|
5
|
+
Bundler.setup(:default, :development)
|
6
|
+
rescue Bundler::BundlerError => e
|
7
|
+
$stderr.puts e.message
|
8
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
9
|
+
exit e.status_code
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'fakeweb'
|
13
|
+
|
14
|
+
class CachableKlass
|
15
|
+
def self.cache_name
|
16
|
+
'test'
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.cache_version
|
20
|
+
19871009
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.cache_file(name, data="test")
|
24
|
+
File.open("#{DistributedCache.config.cache_dir}/#{self.cache_name}/#{name}", 'wb+') { |f| f.write data }
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.update_cache(manifeset)
|
28
|
+
if manifeset.full?
|
29
|
+
cache_file 'full1', 'test1'
|
30
|
+
cache_file 'full2', 'test2'
|
31
|
+
cache_file 'full3', 'test3'
|
32
|
+
else
|
33
|
+
cache_file 'partial1', '1'
|
34
|
+
cache_file 'partial2', '2'
|
35
|
+
cache_file 'full2', 'changed2'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
BASE_TMP_DIR = "#{Dir.pwd}/tmp/rspec"
|
41
|
+
|
42
|
+
def configure_test_distributed_cache
|
43
|
+
FileUtils.rm_rf BASE_TMP_DIR
|
44
|
+
|
45
|
+
DistributedCache.configure do |config|
|
46
|
+
config.file_servers = ['localhost']
|
47
|
+
config.cache_dir = "#{BASE_TMP_DIR}/cache"
|
48
|
+
config.bundle_dir = "#{BASE_TMP_DIR}/bundles"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
RSpec.configure do |config|
|
53
|
+
config.before(:each) do
|
54
|
+
FakeWeb.allow_net_connect = false
|
55
|
+
FakeWeb.clean_registry
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
60
|
+
require 'distributed_cache'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: distributed_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Doug Youch
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-08-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: autoload
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: fakeweb
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
55
69
|
description: Creates tar files of cache directories for distribution
|
56
70
|
email: doug@sessionm.com
|
57
71
|
executables: []
|
@@ -71,9 +85,12 @@ files:
|
|
71
85
|
- VERSION
|
72
86
|
- distributed_cache.gemspec
|
73
87
|
- lib/distributed_cache.rb
|
88
|
+
- lib/distributed_cache/bundle.rb
|
74
89
|
- lib/distributed_cache/config.rb
|
75
90
|
- lib/distributed_cache/manifest.rb
|
76
91
|
- lib/distributed_cache/utils.rb
|
92
|
+
- spec/distributed_cache_spec.rb
|
93
|
+
- spec/helper.rb
|
77
94
|
homepage: http://github.com/sessionm/distributed_cache
|
78
95
|
licenses:
|
79
96
|
- MIT
|