redreloader 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 07958a6733f800644c2dc175acee41e5e62113b7
4
- data.tar.gz: bca54c86f52c6651881379bd56619236441c430a
3
+ metadata.gz: c8206d6f3e0d9744770bad233bd3aa8f87333305
4
+ data.tar.gz: 12c2445596b89e6ea858093f1b1175cf782a813a
5
5
  SHA512:
6
- metadata.gz: 2bb094bf91c15fda1d7a0e84d4db93004b5efa8143da2456024968ccc8a6b8a074d15fbf8d0aae2c24500b8896ba01adb048e6ebfb6914719b27045f1110b697
7
- data.tar.gz: 02725eb98a788527207eb1c2dc96a8d2fdfe1fb85a3a0044e1e205930ca4a6d40ccb29585eec5564b0e5d9588958e74fdae4063f1f5459143fd049e979ab1677
6
+ metadata.gz: afd2c7d5061cc33b8ef00738ea676f44f8e5777a8b9715053b35223a850358b26a3a9b34b8b5aa3f88622002c5356382681c185b4ad9ec8859414d9a17f43381
7
+ data.tar.gz: 6fe84c38de23979a2eaf406a14cf82ee83cf7397e026edfe1fe895a0ed0929154ae7c614c21e5f19a7fe48c57c4b5f5d9727e16e6410bc08c1dd40e25c74b401
data/CHANGELOG CHANGED
@@ -1,2 +1,3 @@
1
+ v0.3. use Pub/sub to watch for changes instead of polling
1
2
  v0.2. fix dependency in binary
2
3
  v0.1. redreloader downloads a file from redis and runs a command on change
data/bin/redreloader CHANGED
@@ -9,7 +9,7 @@ fn ||= '/etc/redreloader.conf'
9
9
  config = YAML::load(File.read(fn))
10
10
  puts config.inspect
11
11
 
12
- Redreloader.redis = Redis.new(config['redis'])
12
+ client = Redreloader.new(config['redis'])
13
13
 
14
14
  def log(str)
15
15
  puts "[#{Time.now}] ---> #{str}"
@@ -23,14 +23,17 @@ end
23
23
 
24
24
  digest = Redreloader.calcmd5(File.read(config['file']))
25
25
 
26
- run! config['command']
26
+ #run! config['command']
27
27
 
28
- loop do
29
- Redreloader.if_changed_from(config['key'], digest) do |newdata, newdigest|
30
- log "File changed"
28
+ begin
29
+ client.process_changes(config['key'], digest) do |newdata, newdigest|
30
+ log "File changed (#{newdata.length} bytes)"
31
31
  File.open(config['file'], 'w') {|io| io.write(newdata) }
32
32
  digest = newdigest
33
33
  run! config['command']
34
34
  end
35
- sleep config['interval'] || 1
35
+ rescue Redis::BaseConnectionError => error
36
+ log "#{error}, retrying in 3"
37
+ sleep 3
38
+ retry
36
39
  end
data/lib/redreloader.rb CHANGED
@@ -3,41 +3,55 @@ require 'zlib'
3
3
  require 'digest/md5'
4
4
 
5
5
  class Redreloader
6
- class << self
7
- attr_accessor :redis
8
- attr_accessor :ttl
9
-
10
- def [](key)
11
- val = with_binary_redis { @redis.getrange(key, 16, -1) }
12
- Zlib::Inflate.inflate(val) if val && val != ""
13
- end
14
-
15
- def []=(key, val)
16
- with_binary_redis { @redis.set(key, calcmd5(val) + Zlib::Deflate.deflate(val), ex: @ttl) }
17
- end
6
+ def initialize(redis_config)
7
+ @redis = Redis.new(redis_config)
8
+ @redis_sub = Redis.new(redis_config)
9
+ end
18
10
 
19
- def calcmd5(val)
20
- Digest::MD5.digest val
21
- end
11
+ def [](key)
12
+ val = with_binary_redis { @redis.getrange(key, 16, -1) }
13
+ Zlib::Inflate.inflate(val) if val && val != ""
14
+ end
22
15
 
23
- def digest(key)
24
- data = with_binary_redis { @redis.getrange(key, 0, 15) }
25
- data if data != ""
26
- end
16
+ def []=(key, val)
17
+ with_binary_redis { @redis.set(key, self.class.calcmd5(val) + Zlib::Deflate.deflate(val)) }
18
+ end
19
+
20
+ def self.calcmd5(val)
21
+ Digest::MD5.digest val
22
+ end
27
23
 
28
- def if_changed_from(key, old_digest)
29
- if (new_digest = digest(key)) && new_digest != old_digest
30
- newdata = self[key]
31
- yield(newdata, new_digest) if newdata
24
+ def digest(key)
25
+ data = with_binary_redis { @redis.getrange(key, 0, 15) }
26
+ data if data != ""
27
+ end
28
+
29
+ def process_changes(key, old_digest, &bl)
30
+ @redis_sub.subscribe("__keyspace@0__:#{key}") do |on|
31
+ on.message do |channel, message|
32
+ old_digest = yield_if_changed(key, old_digest, &bl)
33
+ end
34
+
35
+ on.subscribe do |channel, message|
36
+ # run it here to avoid race condition of change between check and subscribe
37
+ old_digest = yield_if_changed(key, old_digest, &bl)
32
38
  end
33
39
  end
34
-
35
- def with_binary_redis
36
- original_encoding = Encoding.default_external
37
- Encoding.default_external = Encoding.find('binary')
38
- yield
39
- ensure
40
- Encoding.default_external = original_encoding
40
+ end
41
+
42
+ def yield_if_changed(key, old_digest, &bl)
43
+ if (new_digest = digest(key)) && new_digest != old_digest
44
+ newdata = self[key]
45
+ bl.call(newdata, new_digest) if newdata
41
46
  end
47
+ new_digest
48
+ end
49
+
50
+ def with_binary_redis
51
+ original_encoding = Encoding.default_external
52
+ Encoding.default_external = Encoding.find('binary')
53
+ yield
54
+ ensure
55
+ Encoding.default_external = original_encoding
42
56
  end
43
57
  end
@@ -2,7 +2,7 @@ redis:
2
2
  host: localhost
3
3
  port: 6379
4
4
  db: 0
5
+ timeout: 0
5
6
  command: cat myfile.txt
6
7
  file: myfile.txt
7
8
  key: config:myfile
8
- interval: 0.3
data/redreloader.gemspec CHANGED
@@ -1,14 +1,14 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: redreloader 0.2 ruby lib
2
+ # stub: redreloader 0.3 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "redreloader"
6
- s.version = "0.2"
6
+ s.version = "0.3"
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
9
9
  s.require_paths = ["lib"]
10
10
  s.authors = ["Andrew Snow "]
11
- s.date = "2014-08-30"
11
+ s.date = "2015-11-21"
12
12
  s.description = "Transfers file from redis and runs a command whenever it changes"
13
13
  s.email = "andrew@modulus.org"
14
14
  s.executables = ["redreloader"]
@@ -17,7 +17,7 @@ Gem::Specification.new do |s|
17
17
  s.homepage = "https://github.com/andys/redreloader"
18
18
  s.rdoc_options = ["--line-numbers", "--title", "Redreloader"]
19
19
  s.rubyforge_project = "redreloader"
20
- s.rubygems_version = "2.2.2"
20
+ s.rubygems_version = "2.4.6"
21
21
  s.summary = "Transfers file from redis and runs a command whenever it changes"
22
22
 
23
23
  if s.respond_to? :specification_version then
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redreloader
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: '0.3'
5
5
  platform: ruby
6
6
  authors:
7
7
  - 'Andrew Snow '
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-30 00:00:00.000000000 Z
11
+ date: 2015-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  description: Transfers file from redis and runs a command whenever it changes
@@ -46,24 +46,24 @@ licenses: []
46
46
  metadata: {}
47
47
  post_install_message:
48
48
  rdoc_options:
49
- - --line-numbers
50
- - --title
49
+ - "--line-numbers"
50
+ - "--title"
51
51
  - Redreloader
52
52
  require_paths:
53
53
  - lib
54
54
  required_ruby_version: !ruby/object:Gem::Requirement
55
55
  requirements:
56
- - - '>='
56
+ - - ">="
57
57
  - !ruby/object:Gem::Version
58
58
  version: '0'
59
59
  required_rubygems_version: !ruby/object:Gem::Requirement
60
60
  requirements:
61
- - - '>='
61
+ - - ">="
62
62
  - !ruby/object:Gem::Version
63
63
  version: '1.2'
64
64
  requirements: []
65
65
  rubyforge_project: redreloader
66
- rubygems_version: 2.2.2
66
+ rubygems_version: 2.4.6
67
67
  signing_key:
68
68
  specification_version: 4
69
69
  summary: Transfers file from redis and runs a command whenever it changes