sponges 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +11 -2
- data/lib/sponges/cli.rb +13 -1
- data/lib/sponges/commander.rb +26 -0
- data/lib/sponges/supervisor.rb +23 -7
- data/lib/sponges/version.rb +1 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -103,6 +103,16 @@ ruby example.rb stop -g
|
|
103
103
|
In this case, you will have to trap the `HUP` signal, and handle a clean stop
|
104
104
|
from each workers. The point is to wait for a task to be done before quitting.
|
105
105
|
|
106
|
+
Increment worker's pool size :
|
107
|
+
``` bash
|
108
|
+
ruby example.rb increment # will add a worker to the pool.
|
109
|
+
```
|
110
|
+
|
111
|
+
Decrement worker's pool size :
|
112
|
+
``` bash
|
113
|
+
ruby example.rb decrement # will remove a worker to the pool.
|
114
|
+
```
|
115
|
+
|
106
116
|
Show a list of workers and their children.
|
107
117
|
``` bash
|
108
118
|
ruby example.rb list
|
@@ -110,8 +120,7 @@ ruby example.rb list
|
|
110
120
|
|
111
121
|
## TODO
|
112
122
|
|
113
|
-
*
|
114
|
-
* Increment / decrement workers pool size with `TTIN` and `TTOUT`.
|
123
|
+
* More specs.
|
115
124
|
* Check on OSX.
|
116
125
|
|
117
126
|
## Copyright
|
data/lib/sponges/cli.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
module Sponges
|
3
3
|
# This class concern is to expose a nice CLI interface.
|
4
4
|
#
|
5
|
-
class Cli
|
5
|
+
class Cli < Boson::Runner
|
6
6
|
option :daemonize, type: :boolean
|
7
7
|
option :size, type: :numeric
|
8
8
|
desc "Start workers"
|
@@ -32,6 +32,18 @@ module Sponges
|
|
32
32
|
start(options)
|
33
33
|
end
|
34
34
|
|
35
|
+
desc "Increment workers pool size"
|
36
|
+
def increment(options = {})
|
37
|
+
Sponges::Commander.new(Sponges::Configuration.worker_name, options).
|
38
|
+
increment
|
39
|
+
end
|
40
|
+
|
41
|
+
desc "Decrement workers pool size"
|
42
|
+
def decrement(options = {})
|
43
|
+
Sponges::Commander.new(Sponges::Configuration.worker_name, options).
|
44
|
+
decrement
|
45
|
+
end
|
46
|
+
|
35
47
|
desc "Show running processes"
|
36
48
|
def list
|
37
49
|
redis = Nest.new('sponges')
|
data/lib/sponges/commander.rb
CHANGED
@@ -27,6 +27,32 @@ module Sponges
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
+
def increment
|
31
|
+
Sponges.logger.info "Runner #{@name} increment message received."
|
32
|
+
if pid = @redis[:worker][@name][:supervisor].get
|
33
|
+
begin
|
34
|
+
Process.kill :TTIN, pid.to_i
|
35
|
+
rescue Errno::ESRCH => e
|
36
|
+
Sponges.logger.error e
|
37
|
+
end
|
38
|
+
else
|
39
|
+
Sponges.logger.info "No supervisor found."
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def decrement
|
44
|
+
Sponges.logger.info "Runner #{@name} decrement message received."
|
45
|
+
if pid = @redis[:worker][@name][:supervisor].get
|
46
|
+
begin
|
47
|
+
Process.kill :TTOU, pid.to_i
|
48
|
+
rescue Errno::ESRCH => e
|
49
|
+
Sponges.logger.error e
|
50
|
+
end
|
51
|
+
else
|
52
|
+
Sponges.logger.info "No supervisor found."
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
30
56
|
private
|
31
57
|
|
32
58
|
def alive?(pid)
|
data/lib/sponges/supervisor.rb
CHANGED
@@ -53,6 +53,18 @@ module Sponges
|
|
53
53
|
handle_signal signal
|
54
54
|
end
|
55
55
|
end
|
56
|
+
trap(:TTIN) do
|
57
|
+
Sponges.logger.warn "Supervisor increment child's pool by one."
|
58
|
+
fork_children
|
59
|
+
end
|
60
|
+
trap(:TTOU) do
|
61
|
+
Sponges.logger.warn "Supervisor decrement child's pool by one."
|
62
|
+
if pids.first
|
63
|
+
kill_one(pids.first, :HUP)
|
64
|
+
else
|
65
|
+
Sponges.logger.warn "No more child to kill."
|
66
|
+
end
|
67
|
+
end
|
56
68
|
trap(:CHLD) do
|
57
69
|
pids.each do |pid|
|
58
70
|
begin
|
@@ -83,13 +95,17 @@ module Sponges
|
|
83
95
|
|
84
96
|
def kill_them_all(signal)
|
85
97
|
pids.each do |pid|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
98
|
+
kill_one(pid, signal)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def kill_one(pid, signal)
|
103
|
+
begin
|
104
|
+
Process.kill signal, pid.to_i
|
105
|
+
@pids.srem pid
|
106
|
+
Sponges.logger.info "Child #{pid} receive a #{signal} signal."
|
107
|
+
rescue Errno::ESRCH => e
|
108
|
+
# Don't panic
|
93
109
|
end
|
94
110
|
end
|
95
111
|
|
data/lib/sponges/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sponges
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-10-
|
12
|
+
date: 2012-10-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: boson
|