daemonizer 0.3.10 → 0.4.0
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/VERSION +1 -1
- data/daemonizer.gemspec +1 -1
- data/lib/daemonizer/config.rb +23 -18
- data/lib/daemonizer/dsl.rb +11 -2
- data/lib/daemonizer/engine.rb +18 -4
- data/lib/daemonizer/handler.rb +6 -9
- metadata +4 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
data/daemonizer.gemspec
CHANGED
data/lib/daemonizer/config.rb
CHANGED
@@ -10,11 +10,12 @@ module Daemonizer
|
|
10
10
|
init_defaults
|
11
11
|
validate
|
12
12
|
initialize_handler
|
13
|
+
@handler
|
13
14
|
end
|
14
15
|
|
15
16
|
def initialize_handler
|
16
17
|
if @options[:start]
|
17
|
-
@handler = FakeHandler.new(@options[:prepare], @options[:start], @options)
|
18
|
+
@handler = FakeHandler.new(@options[:prepare], @options[:start], @options[:handler_options])
|
18
19
|
@options[:start] = @options[:prepare] = nil
|
19
20
|
elsif
|
20
21
|
@handler = @options[:handler].new(@options[:handler_options])
|
@@ -30,8 +31,26 @@ module Daemonizer
|
|
30
31
|
@options[:pid_file] ||= "pid/#{@pool}.pid"
|
31
32
|
@options[:handler] ||= nil
|
32
33
|
@options[:handler_options] ||= {}
|
34
|
+
@options[:callbacks] ||= {}
|
33
35
|
@options[:cow_friendly] = true if @options[:cow_friendly].nil?
|
34
36
|
end
|
37
|
+
|
38
|
+
def validate_file(filename)
|
39
|
+
# file validation
|
40
|
+
if File.exist?(filename)
|
41
|
+
if !File.file?(filename)
|
42
|
+
raise ConfigError, "'#{filename}' is not a regular file"
|
43
|
+
elsif !File.writable?(filename)
|
44
|
+
raise ConfigError, "'#{filename}' is not writable!"
|
45
|
+
end
|
46
|
+
else # ensure directory is writable
|
47
|
+
dir = File.dirname(filename)
|
48
|
+
if not File.writable?(dir)
|
49
|
+
raise ConfigError, "'#{dir}' is not writable!"
|
50
|
+
end
|
51
|
+
File.open(filename, 'w') { |f| f.write('') } #creating empty file
|
52
|
+
end
|
53
|
+
end
|
35
54
|
|
36
55
|
def validate
|
37
56
|
raise ConfigError, "Workers count should be more then zero" if @options[:workers] < 1
|
@@ -47,25 +66,11 @@ module Daemonizer
|
|
47
66
|
raise ConfigError, "start should be set" if @options[:start].nil?
|
48
67
|
raise ConfigError, "start should have block" unless @options[:start].is_a?(Proc)
|
49
68
|
end
|
50
|
-
|
51
|
-
|
52
|
-
if File.exist?(self.log_file)
|
53
|
-
if !File.file?(self.log_file)
|
54
|
-
raise ConfigError, "'#{self.log_file}' is not a regular file"
|
55
|
-
elsif !File.writable?(self.log_file)
|
56
|
-
raise ConfigError, "'#{self.log_file}' is not writable!"
|
57
|
-
end
|
58
|
-
else # ensure directory is writable
|
59
|
-
dir = File.dirname(self.log_file)
|
60
|
-
if not File.writable?(dir)
|
61
|
-
raise ConfigError, "'#{dir}' is not writable!"
|
62
|
-
end
|
63
|
-
File.open(self.log_file, 'w') { |f| f.write('') } #creating empty file
|
64
|
-
end
|
65
|
-
|
69
|
+
validate_file(self.log_file)
|
70
|
+
validate_file(self.pid_file)
|
66
71
|
end
|
67
72
|
|
68
|
-
[:workers, :poll_period, :root, :cow_friendly].each do |method|
|
73
|
+
[:workers, :poll_period, :root, :cow_friendly, :callbacks].each do |method|
|
69
74
|
define_method method do
|
70
75
|
@options[method.to_sym]
|
71
76
|
end
|
data/lib/daemonizer/dsl.rb
CHANGED
@@ -25,8 +25,17 @@ class Daemonizer::Dsl
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
-
|
29
|
-
|
28
|
+
CALLBACKS = [:before_prepare, :after_prepare, :before_start]
|
29
|
+
def set_callback(callback, &block)
|
30
|
+
return unless CALLBACKS.include?(callback.to_sym)
|
31
|
+
@options[:callbacks] ||= {}
|
32
|
+
@options[:callbacks][callback.to_sym] = block
|
33
|
+
end
|
34
|
+
|
35
|
+
CALLBACKS.each do |callback|
|
36
|
+
define_method callback.to_sym do |&block|
|
37
|
+
set_callback callback.to_sym, &block
|
38
|
+
end
|
30
39
|
end
|
31
40
|
|
32
41
|
def not_cow_friendly
|
data/lib/daemonizer/engine.rb
CHANGED
@@ -7,6 +7,13 @@ module Daemonizer
|
|
7
7
|
Daemonizer.logger_context = "#{Process.pid}/monitor"
|
8
8
|
end
|
9
9
|
|
10
|
+
def run_callback(callback, *args)
|
11
|
+
if block = @config.callbacks[callback.to_sym]
|
12
|
+
Daemonizer.logger.info "Running :#{callback} callback"
|
13
|
+
block.call(*args)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
10
17
|
def start!
|
11
18
|
@pm = ProcessManager.new(@config)
|
12
19
|
|
@@ -15,7 +22,7 @@ module Daemonizer
|
|
15
22
|
@pm.start_workers do |process_id|
|
16
23
|
@config.handler.worker_id = process_id
|
17
24
|
@config.handler.workers_count = @config.workers
|
18
|
-
@config.
|
25
|
+
run_callback(:before_start, Daemonizer.logger, process_id, @config.workers)
|
19
26
|
@config.handler.start
|
20
27
|
end
|
21
28
|
rescue Exception => e
|
@@ -32,9 +39,12 @@ module Daemonizer
|
|
32
39
|
Daemonizer.logger.info "COW-friendly feature is not supported by currently used ruby version"
|
33
40
|
end
|
34
41
|
end
|
35
|
-
Daemonizer.logger.info "Workers count is #{config.workers}"
|
36
42
|
@config.handler.logger = Daemonizer.logger
|
37
|
-
|
43
|
+
run_callback(:before_prepare, Daemonizer.logger)
|
44
|
+
Daemonizer.logger.info "Workers count is #{config.workers}"
|
45
|
+
@config.handler.prepare(init_block) do
|
46
|
+
run_callback(:after_prepare, Daemonizer.logger)
|
47
|
+
end
|
38
48
|
rescue Exception => e
|
39
49
|
log_error(e)
|
40
50
|
end
|
@@ -52,6 +62,7 @@ module Daemonizer
|
|
52
62
|
begin
|
53
63
|
@config.handler.worker_id = 1
|
54
64
|
@config.handler.workers_count = 1
|
65
|
+
run_callback(:before_start, Daemonizer.logger, 1, 1)
|
55
66
|
@config.handler.start
|
56
67
|
rescue Exception => e
|
57
68
|
log_error(e)
|
@@ -59,7 +70,10 @@ module Daemonizer
|
|
59
70
|
end
|
60
71
|
|
61
72
|
begin
|
62
|
-
|
73
|
+
run_callback(:before_prepare, Daemonizer.logger)
|
74
|
+
@config.handler.prepare(init_block) do
|
75
|
+
run_callback(:after_prepare, Daemonizer.logger)
|
76
|
+
end
|
63
77
|
rescue Exception => e
|
64
78
|
log_error(e)
|
65
79
|
end
|
data/lib/daemonizer/handler.rb
CHANGED
@@ -8,8 +8,11 @@ module Daemonizer
|
|
8
8
|
@handler_options = handler_options
|
9
9
|
end
|
10
10
|
|
11
|
-
def prepare(block)
|
12
|
-
|
11
|
+
def prepare(starter, &block)
|
12
|
+
if block_given?
|
13
|
+
yield
|
14
|
+
end
|
15
|
+
starter.call
|
13
16
|
end
|
14
17
|
|
15
18
|
def option(key)
|
@@ -19,12 +22,6 @@ module Daemonizer
|
|
19
22
|
nil
|
20
23
|
end
|
21
24
|
end
|
22
|
-
|
23
|
-
def after_fork
|
24
|
-
if block = option(:after_fork)
|
25
|
-
block.call(Daemonizer.logger, @worker_id, @workers_count)
|
26
|
-
end
|
27
|
-
end
|
28
25
|
end
|
29
26
|
|
30
27
|
class FakeHandler < Handler
|
@@ -34,7 +31,7 @@ module Daemonizer
|
|
34
31
|
super(handler_options)
|
35
32
|
end
|
36
33
|
|
37
|
-
def prepare(block)
|
34
|
+
def prepare(starter, &block)
|
38
35
|
if @prepare
|
39
36
|
@prepare.call(Daemonizer.logger, block)
|
40
37
|
else
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: daemonizer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 15
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 4
|
9
|
+
- 0
|
10
|
+
version: 0.4.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Gleb Pomykalov
|