redis-spawn 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/README.md ADDED
@@ -0,0 +1,183 @@
1
+ redis-spawn
2
+ ===========
3
+
4
+ An extension to redis-rb to facilitate spawning a redis-server specifically
5
+ for your app.
6
+
7
+ Why?
8
+ ----
9
+
10
+ Redis focuses on providing its services to a single application at a time,
11
+ and is geared towards providing scalability for whatever application you are
12
+ using it for. It doesn't really expect you to try and use a single server
13
+ instance and dataset for more than one application at a time: there is only
14
+ very limited access control for the whole server, and no real way of
15
+ partitioning the keyspace (you can namespace, but you can't access control a
16
+ namespace or any specific subset of keys). One solution is to run a server
17
+ instance for each application and dataset you want to use. The goal of
18
+ redis-spawn is to allow you to manage your own redis-server instances from
19
+ within your Ruby application.
20
+
21
+ Usage
22
+ -----
23
+
24
+ To start a server with default options, simply create a new
25
+ Redis::SpawnServer instance:
26
+
27
+ ```ruby
28
+ require 'redis/spawn'
29
+
30
+ my_server_instance = Redis::SpawnServer.new
31
+ my_server_instance.pid
32
+ => 7459
33
+ ```
34
+
35
+ When a Redis::SpawnServer instance is created, the initialize method sets up
36
+ the options for the server, and then starts it (this can be overridden - see
37
+ below). You should then be able to see the server in your process list:
38
+
39
+ ```sh
40
+ $ ps -fp 7459
41
+ PID TTY STAT TIME COMMAND
42
+ 7459 pts/4 S+ 0:00 redis-server /tmp/redis-spawned.7457.config
43
+ ```
44
+
45
+ ### Default Options
46
+
47
+ When starting a server with default options, redis-spawn will create a Redis
48
+ config file named `/tmp/redis-spawned.[pid of parent process].config`, and
49
+ populate it with the default server options. For the most part, these
50
+ defaults correspond to the defaults you expect to find in a vanilla
51
+ `redis.conf`, with a few notable exceptions:
52
+
53
+ * `dir` defaults to `/tmp/redis-spawned.[ppid].data`
54
+ * `logfile` defaults to `"/tmp/redis-spawned.[ppid].log`
55
+ * `unixsocket` defaults to `/tmp/redis-spawned.[ppid].sock`
56
+ * `port` defaults to `0`
57
+ * `bind` defaults to `127.0.0.1`
58
+ * `daemonize` is not set (i.e. server won't daemonize)
59
+
60
+ In all cases, [ppid] corresponds to the PID of the Ruby process that spawns
61
+ the server.
62
+
63
+ ### Connecting
64
+
65
+ By defaults, spawned servers use unix domain sockets, and don't bind to a
66
+ network port. Continuing the above example you can connect as follows:
67
+
68
+ ```ruby
69
+ redis = Redis.new(:path => my_server_instance.socket)
70
+ ```
71
+
72
+ Or from the command line:
73
+
74
+ ```sh
75
+ $ redis-cli -s /tmp/redis-spawned.7457.sock
76
+ ```
77
+
78
+ ### Shutdown and Cleanup
79
+
80
+ Spawned servers will automatically shutdown when your program exits. You can
81
+ also manually shut down:
82
+
83
+ ```ruby
84
+ my_server_instance.shutdown
85
+ => 1
86
+ my_server_instance.pid
87
+ => nil
88
+ ```
89
+
90
+ By default, redis-spawn will cleanup the socket, log, and config files that
91
+ were automatically created. The data directory is not removed: you will
92
+ need to clean this up yourself if you don't need it (@todo support data dir
93
+ removal).
94
+
95
+ ### Server Options
96
+
97
+ You can set :server_opts parameter when initializing to control server
98
+ configuration options. The value of this parameter should be a hash of Redis
99
+ config key/value pairs:
100
+
101
+ ```ruby
102
+ my_server_opts = {:port => 6379, :bind => 192.168.0.1}
103
+ my_net_server = Redis::SpawnServer.new(:server_opts => my_server_opts)
104
+ ```
105
+
106
+ :server_opts keys are Ruby symbols corresponding to names of Redis config
107
+ keys. Underscores in symbol names get translated to dashes in the config
108
+ file e.g. :hash_max_zipmap_value corresponds to hash-max-zipmap-value.
109
+
110
+ Values are expected to be strings or supply strings via #to_s in the usual
111
+ way. There is one exception: if the value is an array, the configuration
112
+ line will be written multiple times, once for each value of the string. For
113
+ example:
114
+
115
+ ```ruby
116
+ :save => ["900 1", "300 10", "60 10000"]
117
+ ```
118
+
119
+ becomes
120
+
121
+ ```
122
+ save 900 1
123
+ save 300 10
124
+ save 60 10000
125
+ ```
126
+
127
+ ### Other options
128
+
129
+ There are several options which you can pass to Redis::SpawnServer.new,
130
+ which are as follows:
131
+
132
+ #### `:generated_config_file`
133
+
134
+ This allows you to override the name/path of the autogenerated config file.
135
+
136
+ #### `:config_file`
137
+
138
+ Allows you to supply your own pre-existing config file. If you pass this
139
+ parameter, redis-spawn will not generate a config file and won't attempt to
140
+ clean up any files on shutdown.
141
+
142
+ #### `:cleanup_files`
143
+
144
+ This controls which files get automatically cleaned up when the server is
145
+ shut down. When setting this parameter, pass an array of symbols
146
+ corresponding to the files you want cleaning up:
147
+
148
+ ```ruby
149
+ :cleanup_files => [:socket, :config]
150
+ ```
151
+
152
+ The default for this parameter is `[:socket, :log, :config]`, unless the
153
+ the `:config_file` parameter is set, in which case the default is to not
154
+ clean up any files unless `:cleanup_files` is set explicitly.
155
+
156
+ At present, there is no built in mechanism for cleaning up the data directory
157
+ - you will always need to do this manually.
158
+
159
+ #### `:start`
160
+
161
+ This allows you to prevent the server from automatically being started, e.g.
162
+
163
+ ```ruby
164
+ # Don't want to start striaght away
165
+ my_server = Redis::SpawnServer(:start => false)
166
+ # ...
167
+ # Now we're ready to start
168
+ my_server.start
169
+ ```
170
+
171
+ Contact and Contributing
172
+ ------------------------
173
+
174
+ The homepage for this project is
175
+
176
+ http://github.com/LichP/redis-spawn
177
+
178
+ Any feedback, suggestions, etc are very welcome. If you have bugfixes and/or
179
+ contributions, feel free to fork, branch, and send a pull request.
180
+
181
+ Enjoy :-)
182
+
183
+ Phil Stewart, October 2011
@@ -1,5 +1,5 @@
1
1
  class Redis
2
2
  class SpawnServer
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
data/lib/redis/spawn.rb CHANGED
@@ -70,7 +70,7 @@ class Redis
70
70
  @opts = default_opts.merge(supplied_opts)
71
71
  self.server_opts = opts[:server_opts]
72
72
 
73
- @pid = opts[:start] ? self.start : 0
73
+ opts[:start] ? self.start : 0
74
74
 
75
75
  # Return the instance
76
76
  self
@@ -114,21 +114,39 @@ class Redis
114
114
  end
115
115
 
116
116
  # Start the server
117
- pid = fork { exec("redis-server #{@config_file}") }
117
+ @pid = fork { exec("redis-server #{@config_file}") }
118
118
  #logger.info("Spawned redis server with PID #{pid}")
119
119
 
120
120
  at_exit do
121
- begin
122
- Process.kill("TERM", pid) # Maybe make this configurable to allow the server to continue after exit
123
- rescue Errno::ESRCH
124
- # Already dead - do nothing
125
- end
126
- self.cleanup_files
121
+ # Maybe make this configurable to allow the server to continue after exit
122
+ self.shutdown!
127
123
  end
128
124
 
129
- pid
125
+ self.pid
126
+ end
127
+
128
+ def started?
129
+ self.pid ? true : false
130
+ end
131
+
132
+ def shutdown
133
+ if self.started?
134
+ self.shutdown!
135
+ else
136
+ nil
137
+ end
130
138
  end
131
139
 
140
+ def shutdown!
141
+ Process.kill("TERM", self.pid)
142
+ rescue Errno::ESRCH
143
+ # Already dead - do nothing
144
+ nil
145
+ ensure
146
+ @pid = nil
147
+ self.cleanup_files
148
+ end
149
+
132
150
  # Attribute write for server opts: merges supplied opts with defaults
133
151
  # to create fully populated server opts
134
152
  #
@@ -187,5 +205,9 @@ class Redis
187
205
  end
188
206
  end
189
207
 
208
+ def socket
209
+ self.server_opts[:unixsocket]
210
+ end
211
+
190
212
  end
191
213
  end
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
  - Phil Stewart
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-10-26 00:00:00 +01:00
17
+ date: 2011-10-29 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -57,7 +57,7 @@ extra_rdoc_files: []
57
57
  files:
58
58
  - lib/redis/spawn/version.rb
59
59
  - lib/redis/spawn.rb
60
- - README
60
+ - README.md
61
61
  - LICENSE
62
62
  - Rakefile
63
63
  - test/write_config_test.rb
data/README DELETED
@@ -1,8 +0,0 @@
1
- # redis-spawn
2
-
3
- An extension to redis-rb to facilitate spawning a redis-server specifically
4
- for your app.
5
-
6
- Phil Stewart, 201110
7
-
8
- License: MIT