redis_ring 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile.lock CHANGED
@@ -2,12 +2,14 @@ PATH
2
2
  remote: .
3
3
  specs:
4
4
  redis_ring (0.0.1)
5
+ daemons-mikehale
5
6
  json
6
7
  sinatra
7
8
 
8
9
  GEM
9
10
  remote: http://rubygems.org/
10
11
  specs:
12
+ daemons-mikehale (1.0.14)
11
13
  diff-lcs (1.1.2)
12
14
  json (1.5.1)
13
15
  mocha (0.9.12)
@@ -20,7 +22,7 @@ GEM
20
22
  rspec-expectations (2.5.0)
21
23
  diff-lcs (~> 1.1.2)
22
24
  rspec-mocks (2.5.0)
23
- sinatra (1.1.3)
25
+ sinatra (1.2.0)
24
26
  rack (~> 1.1)
25
27
  tilt (>= 1.2.2, < 2.0)
26
28
  tilt (1.2.2)
@@ -0,0 +1,9 @@
1
+ if File.directory?("/proc")
2
+ class Daemons::Pid
3
+
4
+ def self.running?(pid)
5
+ return File.exist?("/proc/#{pid}")
6
+ end
7
+
8
+ end
9
+ end
@@ -2,10 +2,11 @@ module RedisRing
2
2
 
3
3
  class Application
4
4
 
5
- attr_reader :shards, :configuration
5
+ attr_reader :shards, :configuration, :process_manager
6
6
 
7
7
  def initialize(configuration)
8
8
  @configuration = configuration
9
+ @process_manager = ProcessManager.new
9
10
  @shards = {}
10
11
  end
11
12
 
@@ -18,14 +19,19 @@ module RedisRing
18
19
  end
19
20
 
20
21
  @shards.each do |shard_no, shard|
21
- shard.start
22
+ @process_manager.start_shard(shard)
22
23
  end
24
+
25
+ @process_manager.run
23
26
  end
24
27
 
25
28
  def stop
29
+ @process_manager.halt
30
+
26
31
  @shards.each do |shard_no, shard|
27
- shard.stop
32
+ @process_manager.stop_shard(shard)
28
33
  end
34
+
29
35
  @shards = {}
30
36
  end
31
37
 
@@ -46,6 +46,8 @@ USAGE
46
46
  Application.instance.start
47
47
 
48
48
  WebInterface.run!(:port => config.base_port)
49
+
50
+ Application.instance.stop
49
51
  end
50
52
 
51
53
  end
@@ -0,0 +1,55 @@
1
+ module RedisRing
2
+
3
+ class ShardAlreadyStarted < StandardError; end
4
+
5
+ class ProcessManager
6
+
7
+ def initialize
8
+ @shards = {}
9
+ end
10
+
11
+ def run
12
+ @continue_running = true
13
+ Thread.new do
14
+ monitor_processes_loop
15
+ end
16
+ end
17
+
18
+ def halt
19
+ @continue_running = false
20
+ end
21
+
22
+ def start_shard(shard)
23
+ if shards.key?(shard.shard_number)
24
+ raise ShardAlreadyStarted.new("Shard: #{shard.shard_number} already started!")
25
+ end
26
+
27
+ shards[shard.shard_number] = shard
28
+
29
+ shard.start
30
+ end
31
+
32
+ def stop_shard(shard)
33
+ shards.delete(shard.shard_number)
34
+ shard.stop
35
+ end
36
+
37
+ protected
38
+
39
+ attr_reader :shards
40
+
41
+ def monitor_processes_loop
42
+ while(@continue_running) do
43
+ shards.each do |shard_no, shard|
44
+ unless shard.alive?
45
+ puts "Restarting shard #{shard_no}"
46
+ shard.start
47
+ end
48
+ end
49
+ sleep(1)
50
+ end
51
+ end
52
+
53
+ end
54
+
55
+ end
@@ -32,28 +32,30 @@ module RedisRing
32
32
  end
33
33
 
34
34
  def start
35
- shard_config.save
36
- @pid = fork_redis_server
37
- @status = :started
35
+ if @status == :started
36
+ @task.start unless @task.running?
37
+ else
38
+ shard_config.save
39
+ @task = fork_redis_server
40
+ @status = :started
41
+ end
38
42
  end
39
43
 
40
44
  def stop
41
- send_kill_signal
45
+ @task.stop
42
46
  @status = :stopped
43
47
  end
44
48
 
45
- protected
46
-
47
49
  def alive?
48
- @pid && File.exist?("/proc/#{@pid}")
50
+ @task && @task.running?
49
51
  end
50
52
 
51
- def fork_redis_server
52
- spawn(shard_config.redis_path, shard_config.config_file_name)
53
- end
53
+ protected
54
54
 
55
- def send_kill_signal
56
- system("kill -QUIT #{pid}")
55
+ def fork_redis_server
56
+ Daemons.call(:multiple => true) do
57
+ exec(shard_config.redis_path, shard_config.config_file_name)
58
+ end
57
59
  end
58
60
 
59
61
  end
@@ -1,3 +1,3 @@
1
1
  module RedisRing
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/redis_ring.rb CHANGED
@@ -5,10 +5,14 @@ require 'fileutils'
5
5
 
6
6
  require 'sinatra'
7
7
  require 'json'
8
+ require 'daemons'
9
+
10
+ require 'monkey_patches'
8
11
 
9
12
  require 'redis_ring/configuration'
10
13
  require 'redis_ring/shard_config'
11
14
  require 'redis_ring/shard'
12
15
  require 'redis_ring/application'
13
16
  require 'redis_ring/web_interface'
17
+ require 'redis_ring/process_manager'
14
18
  require 'redis_ring/cli'
data/redis_ring.gemspec CHANGED
@@ -16,6 +16,7 @@ Gem::Specification.new do |s|
16
16
 
17
17
  s.add_dependency 'sinatra'
18
18
  s.add_dependency 'json'
19
+ s.add_dependency 'daemons-mikehale'
19
20
 
20
21
  s.add_development_dependency 'rspec'
21
22
  s.add_development_dependency 'mocha'
@@ -4,7 +4,7 @@ describe RedisRing::Application do
4
4
 
5
5
  describe "#shards_hash" do
6
6
  before(:each) do
7
- RedisRing::Shard.any_instance.stubs(:fork_redis_server => 123)
7
+ RedisRing::Shard.any_instance.stubs(:fork_redis_server => stub(:start => true, :stop => true, :running? => true))
8
8
  RedisRing::ShardConfig.any_instance.stubs(:save)
9
9
  RedisRing::ShardConfig.any_instance.stubs(:alive? => true)
10
10
 
@@ -5,9 +5,9 @@ describe RedisRing::Shard do
5
5
  describe "possible statuses" do
6
6
  before(:each) do
7
7
  @shard = RedisRing::Shard.new(RedisRing::ShardConfig.new(0, RedisRing::Configuration.new))
8
- @pid = 123
8
+ @task = stub(:start => true, :stop => true)
9
9
  @shard.shard_config.stubs(:save)
10
- @shard.stubs(:fork_redis_server => @pid)
10
+ @shard.stubs(:fork_redis_server => @task)
11
11
  @shard.stubs(:send_kill_signal)
12
12
  end
13
13
 
@@ -16,14 +16,14 @@ describe RedisRing::Shard do
16
16
  end
17
17
 
18
18
  it "should be running if started and alive" do
19
- @shard.stubs(:alive? => true)
19
+ @task.stubs(:running? => true)
20
20
  @shard.start
21
21
 
22
22
  @shard.status.should == :running
23
23
  end
24
24
 
25
25
  it "should be stopping if started then stopped but still alive" do
26
- @shard.stubs(:alive? => true)
26
+ @task.stubs(:running? => true)
27
27
  @shard.start
28
28
  @shard.stop
29
29
 
@@ -31,7 +31,7 @@ describe RedisRing::Shard do
31
31
  end
32
32
 
33
33
  it "should be stopped if started then stopped and not alive" do
34
- @shard.stubs(:alive? => false)
34
+ @task.stubs(:running? => false)
35
35
  @shard.start
36
36
  @shard.stop
37
37
 
@@ -39,7 +39,7 @@ describe RedisRing::Shard do
39
39
  end
40
40
 
41
41
  it "should be dead if started but not alive" do
42
- @shard.stubs(:alive? => false)
42
+ @task.stubs(:running? => false)
43
43
  @shard.start
44
44
 
45
45
  @shard.status.should == :dead
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Adam Pohorecki
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-03-08 00:00:00 +01:00
17
+ date: 2011-03-11 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -44,7 +44,7 @@ dependencies:
44
44
  type: :runtime
45
45
  version_requirements: *id002
46
46
  - !ruby/object:Gem::Dependency
47
- name: rspec
47
+ name: daemons-mikehale
48
48
  prerelease: false
49
49
  requirement: &id003 !ruby/object:Gem::Requirement
50
50
  none: false
@@ -54,10 +54,10 @@ dependencies:
54
54
  segments:
55
55
  - 0
56
56
  version: "0"
57
- type: :development
57
+ type: :runtime
58
58
  version_requirements: *id003
59
59
  - !ruby/object:Gem::Dependency
60
- name: mocha
60
+ name: rspec
61
61
  prerelease: false
62
62
  requirement: &id004 !ruby/object:Gem::Requirement
63
63
  none: false
@@ -69,6 +69,19 @@ dependencies:
69
69
  version: "0"
70
70
  type: :development
71
71
  version_requirements: *id004
72
+ - !ruby/object:Gem::Dependency
73
+ name: mocha
74
+ prerelease: false
75
+ requirement: &id005 !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ type: :development
84
+ version_requirements: *id005
72
85
  description: RedisRing is a solution to run multiple small Redis instances intead of a single large one.
73
86
  email:
74
87
  - adam@pohorecki.pl
@@ -87,10 +100,12 @@ files:
87
100
  - bin/redis-ring
88
101
  - config/redis-ring.sample.yml
89
102
  - config/redis.conf.erb
103
+ - lib/monkey_patches.rb
90
104
  - lib/redis_ring.rb
91
105
  - lib/redis_ring/application.rb
92
106
  - lib/redis_ring/cli.rb
93
107
  - lib/redis_ring/configuration.rb
108
+ - lib/redis_ring/process_manager.rb
94
109
  - lib/redis_ring/shard.rb
95
110
  - lib/redis_ring/shard_config.rb
96
111
  - lib/redis_ring/version.rb