daemonizer 0.4.14 → 0.4.15
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/daemonizer.gemspec +1 -1
- data/lib/daemonizer.rb +35 -17
- data/lib/daemonizer/cli.rb +27 -25
- data/lib/daemonizer/config.rb +7 -2
- data/lib/daemonizer/dsl.rb +15 -11
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.15
|
data/daemonizer.gemspec
CHANGED
data/lib/daemonizer.rb
CHANGED
@@ -7,7 +7,7 @@ require 'simple-statistics'
|
|
7
7
|
|
8
8
|
|
9
9
|
module Daemonizer
|
10
|
-
|
10
|
+
|
11
11
|
def self.root=(value)
|
12
12
|
@@root = value
|
13
13
|
end
|
@@ -19,12 +19,22 @@ module Daemonizer
|
|
19
19
|
File.dirname(daemonfile)
|
20
20
|
end
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
|
+
def self.find_daemonfile(daemonfile_name)
|
24
|
+
previous = nil
|
25
|
+
current = File.expand_path(Dir.pwd)
|
26
|
+
|
27
|
+
until !File.directory?(current) || current == previous
|
28
|
+
filename = File.join(current, daemonfile_name)
|
29
|
+
return filename if File.file?(filename)
|
30
|
+
current, previous = File.expand_path("..", current), current
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
23
34
|
def self.daemonfile=(daemonfile_name)
|
24
|
-
@@daemonfile =
|
25
|
-
@@daemonfile
|
35
|
+
@@daemonfile = find_daemonfile(daemonfile_name)
|
26
36
|
end
|
27
|
-
|
37
|
+
|
28
38
|
def self.daemonfile
|
29
39
|
if defined?(@@daemonfile)
|
30
40
|
@@daemonfile
|
@@ -32,43 +42,51 @@ module Daemonizer
|
|
32
42
|
"Demfile"
|
33
43
|
end
|
34
44
|
end
|
35
|
-
|
45
|
+
|
36
46
|
def self.logger_context=(str)
|
37
47
|
@@logger_context = str
|
38
48
|
end
|
39
|
-
|
49
|
+
|
40
50
|
def self.logger_context
|
41
51
|
@@logger_context
|
42
52
|
end
|
43
|
-
|
53
|
+
|
54
|
+
def self.log_level=(level)
|
55
|
+
@@log_level = level
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.log_level
|
59
|
+
@@log_level ||= :info
|
60
|
+
end
|
61
|
+
|
44
62
|
def self.init_logger(name, log_file)
|
45
63
|
@@logger_file = File.open(log_file, File::WRONLY | File::APPEND)
|
46
64
|
@@logger_file.sync = true
|
47
65
|
@@logger = Logger.new(@@logger_file)
|
48
66
|
set_logger_common_options
|
49
67
|
end
|
50
|
-
|
68
|
+
|
51
69
|
def self.set_logger_common_options
|
52
|
-
@@logger.sev_threshold = Logger::INFO
|
70
|
+
@@logger.sev_threshold = Logger::const_get(Daemonizer.log_level.to_s.upcase) || Logger::INFO
|
53
71
|
@@logger.formatter = Proc.new do |severity, datetime, progname, msg|
|
54
72
|
"%s %s -- %s -- %s\n" % [ datetime.strftime("%Y-%m-%d %H:%M:%S"), severity, Daemonizer.logger_context, msg ]
|
55
73
|
end
|
56
74
|
end
|
57
|
-
|
75
|
+
|
58
76
|
def self.reopen_log_file
|
59
77
|
true #do not need it in append-only mode
|
60
78
|
end
|
61
|
-
|
79
|
+
|
62
80
|
def self.flush_logger
|
63
81
|
@@logger_file.flush
|
64
82
|
end
|
65
|
-
|
83
|
+
|
66
84
|
def self.init_console_logger(name)
|
67
85
|
@@logger_file = STDOUT
|
68
86
|
@@logger = Logger.new(@@logger_file)
|
69
87
|
set_logger_common_options
|
70
88
|
end
|
71
|
-
|
89
|
+
|
72
90
|
def self.logger
|
73
91
|
if defined?(@@logger)
|
74
92
|
@@logger
|
@@ -80,8 +98,8 @@ module Daemonizer
|
|
80
98
|
def self.[](pool)
|
81
99
|
find_pools(pool).first or nil
|
82
100
|
end
|
83
|
-
|
84
|
-
def self.find_pools(pool_name = nil)
|
101
|
+
|
102
|
+
def self.find_pools(pool_name = nil)
|
85
103
|
pools = Dsl.evaluate(daemonfile)
|
86
104
|
|
87
105
|
if pool_name
|
@@ -95,7 +113,7 @@ module Daemonizer
|
|
95
113
|
pools.values
|
96
114
|
end
|
97
115
|
end
|
98
|
-
|
116
|
+
|
99
117
|
end
|
100
118
|
|
101
119
|
require File.dirname(__FILE__) + '/../lib/daemonizer/autoload'
|
data/lib/daemonizer/cli.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'thor'
|
2
2
|
require 'rubygems/config_file'
|
3
3
|
|
4
|
-
module Daemonizer
|
4
|
+
module Daemonizer
|
5
5
|
class CLI < Thor
|
6
6
|
check_unknown_options!
|
7
7
|
|
@@ -11,7 +11,7 @@ module Daemonizer
|
|
11
11
|
super
|
12
12
|
Daemonizer.daemonfile = options[:daemonfile] || "Daemonfile"
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
desc "start", "Start pool"
|
16
16
|
def start(pool_name = nil)
|
17
17
|
control_pools_loop(pool_name, "successfully started") do |pool|
|
@@ -24,7 +24,7 @@ module Daemonizer
|
|
24
24
|
print_pool pool.name, "Starting pool"
|
25
25
|
|
26
26
|
app_name = "#{pool.name} monitor\0"
|
27
|
-
|
27
|
+
|
28
28
|
Daemonize.daemonize(app_name)
|
29
29
|
|
30
30
|
Dir.chdir(Daemonizer.root) # Make sure we're in the working directory
|
@@ -41,7 +41,7 @@ module Daemonizer
|
|
41
41
|
end
|
42
42
|
return true
|
43
43
|
end
|
44
|
-
|
44
|
+
|
45
45
|
desc "stop", "Stop pool"
|
46
46
|
def stop(pool_name = nil)
|
47
47
|
control_pools_loop(pool_name, "successfully stoped") do |pool|
|
@@ -69,9 +69,9 @@ module Daemonizer
|
|
69
69
|
desc "list", "List of pools"
|
70
70
|
def list
|
71
71
|
puts "List of configured pools:"
|
72
|
-
puts ""
|
72
|
+
puts ""
|
73
73
|
Daemonizer.find_pools(nil).each do |pool|
|
74
|
-
puts " * #{pool.name}"
|
74
|
+
puts " * #{pool.name}"
|
75
75
|
end
|
76
76
|
puts ""
|
77
77
|
return true
|
@@ -88,19 +88,19 @@ module Daemonizer
|
|
88
88
|
if pool_name.nil?
|
89
89
|
puts "You should supply pool_name to debug"
|
90
90
|
exit 1
|
91
|
-
end
|
92
|
-
control_pools_loop(pool_name, "execution ended", true) do |pool|
|
91
|
+
end
|
92
|
+
control_pools_loop(pool_name, "execution ended", true) do |pool|
|
93
93
|
STDOUT.sync = true
|
94
|
-
|
94
|
+
|
95
95
|
engine = Engine.new(pool)
|
96
96
|
engine.debug!
|
97
|
-
|
97
|
+
|
98
98
|
print_pool pool.name, " Done!"
|
99
99
|
exit(0)
|
100
100
|
end
|
101
|
-
return true
|
101
|
+
return true
|
102
102
|
end
|
103
|
-
|
103
|
+
|
104
104
|
desc "stats", "Pools statistics"
|
105
105
|
def stats(pool_name = nil)
|
106
106
|
Daemonizer.find_pools(pool_name).each do |pool|
|
@@ -108,29 +108,31 @@ module Daemonizer
|
|
108
108
|
statistics.print
|
109
109
|
end
|
110
110
|
end
|
111
|
-
|
111
|
+
|
112
112
|
private
|
113
113
|
def control_pools_loop(pool_name, message = nil, debug = false, &block)
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
else
|
119
|
-
Daemonizer.init_logger(pool.name.to_s, pool.log_file)
|
120
|
-
end
|
114
|
+
if debug
|
115
|
+
pool = Daemonizer.find_pools(pool_name).first
|
116
|
+
Daemonizer.init_console_logger(pool.name.to_s)
|
117
|
+
begin
|
121
118
|
yield(pool)
|
119
|
+
rescue Interrupt => e
|
120
|
+
puts "Interrupted from keyboard"
|
122
121
|
end
|
123
|
-
|
122
|
+
else
|
123
|
+
Daemonizer.find_pools(pool_name).each do |pool|
|
124
|
+
Process.fork do
|
125
|
+
Daemonizer.init_logger(pool.name.to_s, pool.log_file)
|
126
|
+
yield(pool)
|
127
|
+
end
|
124
128
|
Process.wait
|
125
129
|
if $?.exitstatus == 0 and message
|
126
|
-
print_pool pool.name, message
|
130
|
+
print_pool pool.name, message
|
127
131
|
end
|
128
|
-
rescue Interrupt => e
|
129
|
-
puts "Interrupted from keyboard"
|
130
132
|
end
|
131
133
|
end
|
132
134
|
end
|
133
|
-
|
135
|
+
|
134
136
|
def print_pool(pool_name, message)
|
135
137
|
puts "#{pool_name}: #{message}"
|
136
138
|
end
|
data/lib/daemonizer/config.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
module Daemonizer
|
2
2
|
class Config
|
3
|
+
VALID_LOG_LEVELS = [:debug, :info, :warn, :error, :fatal]
|
4
|
+
|
3
5
|
class ConfigError < StandardError; end
|
4
6
|
|
5
7
|
attr_reader :pool, :handler
|
@@ -34,8 +36,9 @@ module Daemonizer
|
|
34
36
|
@options[:callbacks] ||= {}
|
35
37
|
@options[:on_poll] ||= []
|
36
38
|
@options[:cow_friendly] = true if @options[:cow_friendly].nil?
|
39
|
+
@options[:log_level] ||= :info
|
37
40
|
end
|
38
|
-
|
41
|
+
|
39
42
|
def validate_file(filename)
|
40
43
|
# file validation
|
41
44
|
if File.exist?(filename)
|
@@ -56,6 +59,7 @@ module Daemonizer
|
|
56
59
|
def validate
|
57
60
|
raise ConfigError, "Workers count should be more then zero" if @options[:workers] < 1
|
58
61
|
raise ConfigError, "Poll period should be more then zero" if @options[:poll_period] < 1
|
62
|
+
raise ConfigError, "Log level should be one of [#{VALID_LOG_LEVELS.map(&:to_s).join(',')}]" unless VALID_LOG_LEVELS.include?(@options[:log_level].to_sym)
|
59
63
|
if @options[:handler]
|
60
64
|
raise ConfigError, "Handler should be a class" unless @options[:handler].is_a?(Class)
|
61
65
|
raise ConfigError, "Handler should respond to :start" unless @options[:handler].public_instance_methods.include?('start')
|
@@ -67,6 +71,7 @@ module Daemonizer
|
|
67
71
|
raise ConfigError, "start should be set" if @options[:start].nil?
|
68
72
|
raise ConfigError, "start should have block" unless @options[:start].is_a?(Proc)
|
69
73
|
end
|
74
|
+
|
70
75
|
validate_file(self.log_file)
|
71
76
|
validate_file(self.pid_file)
|
72
77
|
end
|
@@ -82,7 +87,7 @@ module Daemonizer
|
|
82
87
|
File.join(Daemonizer.root, @options[method.to_sym])
|
83
88
|
end
|
84
89
|
end
|
85
|
-
|
90
|
+
|
86
91
|
def name
|
87
92
|
@pool
|
88
93
|
end
|
data/lib/daemonizer/dsl.rb
CHANGED
@@ -13,7 +13,7 @@ class Daemonizer::Dsl
|
|
13
13
|
@pool = :default
|
14
14
|
@configs = {}
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
def set_option(option, value = nil, &block)
|
18
18
|
@options[:handler_options] ||= {}
|
19
19
|
if not value.nil?
|
@@ -24,7 +24,7 @@ class Daemonizer::Dsl
|
|
24
24
|
raise DslError, "you should supply block or value to :set_option"
|
25
25
|
end
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
CALLBACKS = [:before_prepare, :after_prepare, :before_start]
|
29
29
|
def set_callback(callback, &block)
|
30
30
|
return unless CALLBACKS.include?(callback.to_sym)
|
@@ -32,13 +32,17 @@ class Daemonizer::Dsl
|
|
32
32
|
@options[:callbacks][callback.to_sym] ||= []
|
33
33
|
@options[:callbacks][callback.to_sym] << block
|
34
34
|
end
|
35
|
-
|
35
|
+
|
36
36
|
CALLBACKS.each do |callback|
|
37
37
|
define_method callback.to_sym do |&block|
|
38
38
|
set_callback callback.to_sym, &block
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
+
def log_level(level)
|
43
|
+
@options[:log_level] = level.to_sym
|
44
|
+
end
|
45
|
+
|
42
46
|
def on_poll(&block)
|
43
47
|
@options[:on_poll] ||= []
|
44
48
|
@options[:on_poll] << block
|
@@ -48,31 +52,31 @@ class Daemonizer::Dsl
|
|
48
52
|
def not_cow_friendly
|
49
53
|
@options[:cow_friendly] = false
|
50
54
|
end
|
51
|
-
|
55
|
+
|
52
56
|
def handler(handler)
|
53
57
|
@options[:handler] = handler
|
54
58
|
end
|
55
|
-
|
59
|
+
|
56
60
|
def poll_period(seconds)
|
57
61
|
@options[:poll_period] = seconds.to_i
|
58
62
|
end
|
59
|
-
|
63
|
+
|
60
64
|
def log_file(log)
|
61
65
|
@options[:log_file] = log
|
62
66
|
end
|
63
|
-
|
67
|
+
|
64
68
|
def workers(num)
|
65
69
|
@options[:workers] = num.to_i
|
66
70
|
end
|
67
|
-
|
71
|
+
|
68
72
|
def prepare(&blk)
|
69
73
|
@options[:prepare] = blk
|
70
74
|
end
|
71
|
-
|
75
|
+
|
72
76
|
def start(&blk)
|
73
77
|
@options[:start] = blk
|
74
78
|
end
|
75
|
-
|
79
|
+
|
76
80
|
def pid_file(pid)
|
77
81
|
@options[:pid_file] = pid
|
78
82
|
end
|
@@ -95,7 +99,7 @@ class Daemonizer::Dsl
|
|
95
99
|
@options = options
|
96
100
|
@pool = nil
|
97
101
|
end
|
98
|
-
|
102
|
+
|
99
103
|
def config_copy
|
100
104
|
options = @options.dup
|
101
105
|
options[:handler_options] = @options[:handler_options].dup if @options[:handler_options]
|
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: 17
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 4
|
9
|
-
-
|
10
|
-
version: 0.4.
|
9
|
+
- 15
|
10
|
+
version: 0.4.15
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Gleb Pomykalov
|