sponges 0.6.0 → 0.7.0

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: c6cece34aa41d2f8dad71f3d2135e7abe8aea29c
4
- data.tar.gz: 9c07a08501a29f2c51c4b46d02ac3216aebfd11e
3
+ metadata.gz: 08f5c47703d30df8dc08b6e3982cdc2093890f34
4
+ data.tar.gz: 6d9af0ec47db5670cfb51287d92343698e8b06fc
5
5
  SHA512:
6
- metadata.gz: 163974d2f7704a4c564e6ec237de28daf0ce83ee5e2d25d3f984a3debb2162dc0442dcc66349bcabbc0f7cc227fd951d65d088f2f0d1bf128d5f8fbbff427c7c
7
- data.tar.gz: a60e3ae1fcffd326f4863db94b4d9005762eea6816736b03ce7250a28bba5fae9c77e174a4965ff27218c520ba943c6d781d5cf7b38719d9c990f49eac1d6750
6
+ metadata.gz: 3b10980701ade47d210a5b9df3e7c6ca3281d9594f19702f5811211d839f80100eaca8a6705e27b237dcac32f9ceecdb31ca15573f0623fed897b5efcdde1ebc
7
+ data.tar.gz: 6a66267ddf49b8e9ee0486d8058874c5df696b01933ce18c0551210e49c9371079d6fcebafe300b832a7e67e07b275470f58d4092415130422816dbcb7740149
data/README.md CHANGED
@@ -13,6 +13,7 @@ to work is your job. :)
13
13
  Basically, sponges is a ruby supervisor that forks processes and controls their
14
14
  execution and termination. For example the following will start a supervision
15
15
  daemon and 8 processes of "a_worker".
16
+
16
17
  ```bash
17
18
  ruby a_worker.rb start -d -s 8
18
19
  ```
@@ -69,10 +70,11 @@ class Worker
69
70
  end
70
71
 
71
72
  Sponges.configure do |config|
72
- config.logger = MyCustomLogger.new # optionnal
73
- config.redis = Redis.new # optionnal
74
- config.size = 3
75
- config.daemonize = true
73
+ config.logger = MyCustomLogger.new # optionnal
74
+ config.redis = Redis.new # optionnal
75
+ config.size = 3 # optionnal, default to cpu's size
76
+ config.daemonize = true # optionnal, default to false
77
+ config.port = 5032 # optionnal, default to 5032
76
78
  config.after_fork do
77
79
  puts "Execute code when a child process is created"
78
80
  end
@@ -146,6 +148,58 @@ Show a list of workers and their children.
146
148
  ruby example.rb list
147
149
  ```
148
150
 
151
+ ## Http supervision
152
+
153
+ sponges provides an http interface to supervise pool's activity, and to expose
154
+ pids. Http supervision can be enable in configuration:
155
+
156
+
157
+ ``` ruby
158
+ Sponges.configure do |config|
159
+ config.port = 3333
160
+ end
161
+ ```
162
+
163
+ By default, sponges listens on port 5032, and responds in json. Here is an
164
+ example of response:
165
+
166
+ ``` javascript
167
+ {
168
+ "supervisor":{
169
+ "pid":11537,
170
+ "pctcpu":0.0,
171
+ "pctmem":0.22,
172
+ "created_at":"2013-03-05 15:21:04 +0100"
173
+ },
174
+ "children":[
175
+ {
176
+ "pid":11540,
177
+ "pctcpu":0.0,
178
+ "pctmem":0.21,
179
+ "created_at":"2013-03-05 15:21:04 +0100"
180
+ },
181
+ {
182
+ "pid":11543,
183
+ "pctcpu":0.0,
184
+ "pctmem":0.21,
185
+ "created_at":"2013-03-05 15:21:04 +0100"
186
+ },
187
+ {
188
+ "pid":11546,
189
+ "pctcpu":0.0,
190
+ "pctmem":0.21,
191
+ "created_at":"2013-03-05 15:21:04 +0100"
192
+ },
193
+ {
194
+ "pid":11549,
195
+ "pctcpu":0.0,
196
+ "pctmem":0.21,
197
+ "created_at":"2013-03-05 15:21:04 +0100"
198
+ }
199
+ ]
200
+ }
201
+ ```
202
+
149
203
  ## Stores
150
204
 
151
205
  sponges can store pids in memory or in redis. Memory is the default store. The
@@ -155,7 +209,7 @@ To select the redis store, you need to add `nest` to your application's
155
209
  Gemfile, and do the following.
156
210
 
157
211
  ``` ruby
158
- gem "sponges"
212
+ gem "nest"
159
213
  ```
160
214
 
161
215
  ``` ruby
@@ -166,16 +220,6 @@ end
166
220
 
167
221
  ## Roadmap
168
222
 
169
- ### Version 0.6
170
-
171
- * Deprecation notice about Redis store removal
172
-
173
-
174
- ### Version 0.7
175
-
176
- * Inclusion of Http supervision
177
-
178
-
179
223
  ### Version 1.0
180
224
 
181
225
  * Removal of Redis store
data/lib/sponges.rb CHANGED
@@ -4,8 +4,13 @@ require 'socket'
4
4
  require 'logger'
5
5
  require 'machine'
6
6
  require 'forwardable'
7
+ require 'socket'
8
+ require 'json'
9
+ require_relative 'sponges/version'
7
10
  require_relative 'sponges/configuration'
8
11
  require_relative 'sponges/handler'
12
+ require_relative 'sponges/response'
13
+ require_relative 'sponges/listener'
9
14
  require_relative 'sponges/supervisor'
10
15
  require_relative 'sponges/runner'
11
16
  require_relative 'sponges/commander'
@@ -5,7 +5,8 @@ module Sponges
5
5
  class Configuration
6
6
  class << self
7
7
  ACCESSOR = [:worker_name, :worker, :logger, :redis, :size,
8
- :daemonize, :after_fork, :timeout, :gracefully, :store
8
+ :daemonize, :after_fork, :timeout, :gracefully, :store,
9
+ :port
9
10
  ]
10
11
  attr_accessor *ACCESSOR
11
12
 
@@ -32,6 +33,10 @@ module Sponges
32
33
  @store || :memory
33
34
  end
34
35
 
36
+ def port
37
+ @port || 5032
38
+ end
39
+
35
40
  def redis
36
41
  @redis ||= Redis.new.tap { warn_redis }
37
42
  end
@@ -44,6 +49,7 @@ module Sponges
44
49
  def warn_redis
45
50
  Sponges.logger.warn "Redis's store will be removed in version 1.0!"
46
51
  end
52
+
47
53
  end
48
54
  end
49
55
 
@@ -2,6 +2,7 @@
2
2
  module Sponges
3
3
  class Handler
4
4
  extend Forwardable
5
+
5
6
  attr_reader :supervisor, :queue, :notifier
6
7
 
7
8
  def initialize(supervisor)
@@ -0,0 +1,43 @@
1
+ # encoding: utf-8
2
+ module Sponges
3
+ class Listener
4
+ attr_reader :supervisor
5
+ CRLF = "\r\n"
6
+
7
+ def initialize(supervisor)
8
+ @supervisor = supervisor
9
+ end
10
+
11
+ def call
12
+ Socket.tcp_server_loop("0.0.0.0", port) {|c| handle_connection c }
13
+ end
14
+
15
+ private
16
+
17
+ def port
18
+ Sponges::Configuration.port
19
+ end
20
+
21
+ def handle_connection(connection)
22
+ response = Response.new(supervisor).to_json
23
+ connection.write headers(response)
24
+ connection.write response
25
+ connection.close
26
+ rescue Errno::EPIPE
27
+ # Resist to ping
28
+ end
29
+
30
+ def headers(response)
31
+ [
32
+ "HTTP/1.1 200 OK",
33
+ "Date: #{Time.now.utc}",
34
+ "Status: OK",
35
+ "Server: Sponges #{Sponges::VERSION} #{supervisor.name} edition",
36
+ "Content-Type: application/json; charset=utf-8",
37
+ "Content-Length: #{response.length}",
38
+ CRLF
39
+ ].join(CRLF)
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,42 @@
1
+ # encoding: utf-8
2
+ module Sponges
3
+ class Response
4
+ attr_reader :supervisor
5
+
6
+ def initialize(supervisor)
7
+ @supervisor = supervisor
8
+ end
9
+
10
+ def to_json
11
+ as_json.to_json
12
+ end
13
+
14
+ def as_json(opts={})
15
+ {
16
+ supervisor: process_information(supervisor.pid),
17
+ children: children_information
18
+ }
19
+ end
20
+
21
+ private
22
+
23
+ def process_information(pid)
24
+ info = Machine::ProcessStatus.new(pid)
25
+ {
26
+ pid: pid,
27
+ pctcpu: info.pctcpu,
28
+ pctmem: info.pctmem,
29
+ created_at: info.created_at
30
+
31
+ }
32
+ end
33
+
34
+ def children_information
35
+ supervisor.children_pids.map do |pid|
36
+ process_information(pid)
37
+ end
38
+ end
39
+
40
+ end
41
+ end
42
+
@@ -1,7 +1,10 @@
1
1
  # encoding: utf-8
2
2
  module Sponges
3
3
  class Supervisor
4
- attr_reader :store, :name, :options, :handler
4
+ extend Forwardable
5
+ attr_reader :store, :name, :options, :handler, :listener
6
+ def_delegator :@store, :supervisor_pid, :pid
7
+ def_delegator :@store, :children_pids
5
8
 
6
9
  def initialize(name, options, store, block)
7
10
  @name, @options, @store, @block = name, options, store, block
@@ -10,6 +13,7 @@ module Sponges
10
13
  store.register Process.pid
11
14
  @children_seen = 0
12
15
  @handler = Handler.new self
16
+ @listener = Listener.new(self)
13
17
  end
14
18
 
15
19
  def start
@@ -18,8 +22,8 @@ module Sponges
18
22
  options[:size].times do
19
23
  handler.push :TTIN
20
24
  end
21
- Sponges.logger.info "Supervisor started, waiting for messages."
22
- sleep
25
+ Sponges.logger.info "Supervisor started, waiting for messages, listening on port #{Sponges::Configuration.port}"
26
+ listener.call
23
27
  rescue SystemExit => exception
24
28
  raise exception
25
29
  rescue Exception => exception
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module Sponges
3
- VERSION = "0.6.0"
3
+ VERSION = "0.7.0"
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sponges
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - chatgris
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-03-01 00:00:00.000000000 Z
11
+ date: 2013-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: boson
@@ -28,16 +28,16 @@ dependencies:
28
28
  name: machine
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ~>
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: 0.0.4
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ~>
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: 0.0.4
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -69,6 +69,8 @@ files:
69
69
  - lib/sponges/commander.rb
70
70
  - lib/sponges/configuration.rb
71
71
  - lib/sponges/handler.rb
72
+ - lib/sponges/listener.rb
73
+ - lib/sponges/response.rb
72
74
  - lib/sponges/runner.rb
73
75
  - lib/sponges/store.rb
74
76
  - lib/sponges/store/memory.rb