sponges 0.1.0 → 0.1.1

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 CHANGED
@@ -5,16 +5,21 @@ stressed and eager to work. `sponges` helps you build this army of sponges, to
5
5
  control them, and, well, to kill them gracefully. Making them stressed and eager
6
6
  to work is your job. :)
7
7
 
8
+ Basically, sponges is a ruby supervisor that forks processes and controls their
9
+ execution and termination. For example the following will start a supervision
10
+ daemon and 8 processes of "a_worker".
11
+ ```bash
12
+ ruby a_worker.rb start -d -s 8
13
+ ```
14
+ If you kill the supervisor it will cleanly
15
+ terminate the child processes.
16
+
8
17
  Internally, `sponges` strongly relies on Unix signals.
9
18
 
10
19
  ## Is it any good?
11
20
 
12
21
  [Yes.](http://news.ycombinator.com/item?id=3067434)
13
22
 
14
- ## Production ready ?
15
-
16
- Not yet, but soon. Beware, api can change in 0.0.x.
17
-
18
23
  ## Installation
19
24
 
20
25
  Ruby 1.9.2 (or superior) and Redis are required.
@@ -95,6 +100,11 @@ Stop workers with a `QUIT` signal :
95
100
  ruby example.rb stop
96
101
  ```
97
102
 
103
+ Stop workers with a `KILL` signal :
104
+ ``` bash
105
+ ruby example.rb kill
106
+ ```
107
+
98
108
  Stop workers with a `HUP` signal :
99
109
  ``` bash
100
110
  ruby example.rb stop -g
data/lib/sponges/cli.rb CHANGED
@@ -18,6 +18,11 @@ module Sponges
18
18
  Sponges::Commander.new(Sponges::Configuration.worker_name, options).stop
19
19
  end
20
20
 
21
+ desc "Kill workers"
22
+ def kill(options = {})
23
+ Sponges::Commander.new(Sponges::Configuration.worker_name, options).kill
24
+ end
25
+
21
26
  option :daemonize, type: :boolean
22
27
  option :size, type: :numeric
23
28
  option :gracefully, type: :boolean
@@ -9,24 +9,30 @@ module Sponges
9
9
  @redis = Nest.new('sponges', Configuration.redis || Redis.new)[Socket.gethostname]
10
10
  end
11
11
 
12
- def stop
12
+ # Kills the supervisor, and then all workers.
13
+ #
14
+ def kill
15
+ Sponges.logger.info "Runner #{@name} kill message received."
16
+ stop :KILL
17
+ Array(@redis[:worker][@name][:pids].smembers).each do|pid|
18
+ kill_process(:KILL, pid, "Worker")
19
+ end
20
+ end
21
+
22
+ # Stops supervisor, signal depends on options given by Boson.
23
+ #
24
+ def stop(signal = nil)
25
+ signal ||= gracefully? ? :HUP : :QUIT
13
26
  Sponges.logger.info "Runner #{@name} stop message received."
14
27
  if pid = @redis[:worker][@name][:supervisor].get
15
- begin
16
- Process.kill gracefully? ? :HUP : :QUIT, pid.to_i
17
- while alive?(pid.to_i) do
18
- Sponges.logger.info "Supervisor #{pid} still alive"
19
- sleep 0.5
20
- end
21
- Sponges.logger.info "Supervisor #{pid} has stopped."
22
- rescue Errno::ESRCH => e
23
- Sponges.logger.error e
24
- end
28
+ kill_process(signal, pid)
25
29
  else
26
30
  Sponges.logger.info "No supervisor found."
27
31
  end
28
32
  end
29
33
 
34
+ # Increment workers's pool by one.
35
+ #
30
36
  def increment
31
37
  Sponges.logger.info "Runner #{@name} increment message received."
32
38
  if pid = @redis[:worker][@name][:supervisor].get
@@ -40,6 +46,8 @@ module Sponges
40
46
  end
41
47
  end
42
48
 
49
+ # Decrement workers's pool by one.
50
+ #
43
51
  def decrement
44
52
  Sponges.logger.info "Runner #{@name} decrement message received."
45
53
  if pid = @redis[:worker][@name][:supervisor].get
@@ -55,6 +63,20 @@ module Sponges
55
63
 
56
64
  private
57
65
 
66
+ def kill_process(signal, pid, type = "Supervisor")
67
+ pid = pid.to_i
68
+ begin
69
+ Process.kill signal, pid
70
+ while alive?(pid) do
71
+ Sponges.logger.info "#{type} #{pid} still alive"
72
+ sleep 0.5
73
+ end
74
+ Sponges.logger.info "#{type} #{pid} has stopped."
75
+ rescue Errno::ESRCH => e
76
+ Sponges.logger.error e
77
+ end
78
+ end
79
+
58
80
  def alive?(pid)
59
81
  begin
60
82
  Process.kill 0, pid
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module Sponges
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.1"
4
4
  end
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.1.0
4
+ version: 0.1.1
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-30 00:00:00.000000000 Z
12
+ date: 2012-11-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: boson
@@ -75,9 +75,10 @@ dependencies:
75
75
  - - ~>
76
76
  - !ruby/object:Gem::Version
77
77
  version: 2.10.0
78
- description: When I build some worker, I want them to be like an army of spongebob,
79
- always stressed and eager to work. sponges helps you to build this army of sponge,
80
- to control them, and, well, kill them gracefully.
78
+ description: When I build workers, I want them to be like an army of spongebobs, always
79
+ stressed and eager to work. sponges helps you build this army of sponges, to control
80
+ them, and, well, to kill them gracefully. Making them stressed and eager to work
81
+ is your job. :)
81
82
  email:
82
83
  - jboyer@af83.com
83
84
  executables: []
@@ -116,5 +117,5 @@ rubyforge_project:
116
117
  rubygems_version: 1.8.24
117
118
  signing_key:
118
119
  specification_version: 3
119
- summary: Turn any ruby object in a daemons controlling an army of sponges.
120
+ summary: Turn any ruby object to a daemon controlling an army of sponges.
120
121
  test_files: []