sponges 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -63,12 +63,19 @@ end
63
63
  Sponges.configure do |config|
64
64
  config.logger = MyCustomLogger.new # optionnal
65
65
  config.redis = Redis.new # optionnal
66
+ config.size = 3
67
+ config.daemonize = true
68
+ config.after_fork do
69
+ puts "Execute some when a child process is created"
70
+ end
71
+ config.on_chld do
72
+ puts "Execute code when a child process is killed"
66
73
  end
67
74
 
68
75
  # Register a pool named "worker_name".
69
76
  # Options are optionnal. Consider it as a default, options given by command have
70
77
  # the precedence.
71
- Sponges.start "worker_name", {size: 3, daemonize: true} do
78
+ Sponges.start "worker_name" do
72
79
  Worker.new({some: args}).run
73
80
  end
74
81
  ```
@@ -21,7 +21,6 @@ module Sponges
21
21
  def start(worker_name, options = {}, argv = ARGV, &block)
22
22
  Sponges::Configuration.worker_name = worker_name
23
23
  Sponges::Configuration.worker = block
24
- Sponges::Configuration.options = options
25
24
  Sponges::Cli.start(argv)
26
25
  end
27
26
  module_function :start
@@ -7,7 +7,10 @@ module Sponges
7
7
  option :size, type: :numeric
8
8
  desc "Start workers"
9
9
  def start(options = {})
10
- options = Sponges::Configuration.options.merge(options)
10
+ options = {
11
+ size: Sponges::Configuration.size,
12
+ daemonize: Sponges::Configuration.daemonize
13
+ }.reject{|k, v| v.nil?}.merge(options)
11
14
  Sponges::Runner.new(Sponges::Configuration.worker_name, options,
12
15
  Sponges::Configuration.worker
13
16
  ).start
@@ -4,7 +4,9 @@ module Sponges
4
4
  #
5
5
  class Configuration
6
6
  class << self
7
- ACCESSOR = [:worker_name, :worker, :logger, :redis, :options]
7
+ ACCESSOR = [:worker_name, :worker, :logger, :redis, :size,
8
+ :daemonize, :after_fork
9
+ ]
8
10
  attr_accessor *ACCESSOR
9
11
 
10
12
  def configure
@@ -17,6 +19,28 @@ module Sponges
17
19
  conf
18
20
  end
19
21
  end
22
+
23
+ def after_fork(&block)
24
+ Hook._after_fork = block
25
+ end
26
+
27
+ def on_chld(&block)
28
+ Hook._on_chld = block
29
+ end
30
+ end
31
+ end
32
+
33
+ class Hook
34
+ class << self
35
+ attr_accessor :_after_fork, :_on_chld
36
+
37
+ def after_fork
38
+ _after_fork.call if _after_fork.respond_to?(:call)
39
+ end
40
+
41
+ def on_chld
42
+ _on_chld.call if _on_chld.respond_to?(:call)
43
+ end
20
44
  end
21
45
  end
22
46
  end
@@ -36,6 +36,7 @@ module Sponges
36
36
  name = children_name
37
37
  pid = fork do
38
38
  $PROGRAM_NAME = name
39
+ Sponges::Hook.after_fork
39
40
  @block.call
40
41
  end
41
42
  Sponges.logger.info "Supervisor create a child with #{pid} pid."
@@ -71,6 +72,7 @@ module Sponges
71
72
  if dead
72
73
  Sponges.logger.warn "Child #{dead} died. Restarting a new one..."
73
74
  @pids.srem dead
75
+ Sponges::Hook.on_chld
74
76
  fork_children
75
77
  end
76
78
  rescue Errno::ECHILD => e
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module Sponges
3
- VERSION = "0.1.3"
3
+ VERSION = "0.2.0"
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.3
4
+ version: 0.2.0
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-11-09 00:00:00.000000000 Z
12
+ date: 2012-11-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: boson