daemonizer 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/daemonizer.gemspec +2 -2
- data/lib/daemonizer/config.rb +1 -4
- data/lib/daemonizer/dsl.rb +1 -5
- data/lib/daemonizer/process_manager.rb +1 -1
- data/lib/daemonizer/worker.rb +40 -60
- data/lib/daemonizer/worker_pool.rb +2 -3
- metadata +4 -4
data/Rakefile
CHANGED
@@ -5,7 +5,7 @@ begin
|
|
5
5
|
require 'jeweler'
|
6
6
|
Jeweler::Tasks.new do |gemspec|
|
7
7
|
gemspec.name = "daemonizer"
|
8
|
-
gemspec.summary = "Daemonizer allows you to easily create custom daemons on ruby. Supporting
|
8
|
+
gemspec.summary = "Daemonizer allows you to easily create custom daemons on ruby. Supporting prefork model"
|
9
9
|
gemspec.description = "Inspired by bundler and rack. Mostly built on top of Alexey Kovyrin's loops code. http://github.com/kovyrin/loops"
|
10
10
|
gemspec.email = "glebpom@gmail.com"
|
11
11
|
gemspec.homepage = "http://github.com/glebpom/daemonizer"
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.6
|
data/daemonizer.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{daemonizer}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.6"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Gleb Pomykalov"]
|
@@ -44,7 +44,7 @@ Gem::Specification.new do |s|
|
|
44
44
|
s.rdoc_options = ["--charset=UTF-8"]
|
45
45
|
s.require_paths = ["lib"]
|
46
46
|
s.rubygems_version = %q{1.3.7}
|
47
|
-
s.summary = %q{Daemonizer allows you to easily create custom daemons on ruby. Supporting
|
47
|
+
s.summary = %q{Daemonizer allows you to easily create custom daemons on ruby. Supporting prefork model}
|
48
48
|
|
49
49
|
if s.respond_to? :specification_version then
|
50
50
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
data/lib/daemonizer/config.rb
CHANGED
@@ -33,7 +33,6 @@ module Daemonizer
|
|
33
33
|
def init_defaults
|
34
34
|
@options[:before_init] ||= nil
|
35
35
|
@options[:after_init] ||= nil
|
36
|
-
@options[:engine] ||= :fork
|
37
36
|
@options[:workers] ||= 1
|
38
37
|
@options[:log_file] ||= "log/#{@pool}.log"
|
39
38
|
@options[:poll_period] ||= 5
|
@@ -45,8 +44,6 @@ module Daemonizer
|
|
45
44
|
|
46
45
|
def validate
|
47
46
|
raise ConfigError, "Workers count should be more then zero" if @options[:workers] < 1
|
48
|
-
raise ConfigError, "Engine #{@options[:engine]} is not known" unless [:fork, :thread].include?(@options[:engine])
|
49
|
-
|
50
47
|
raise ConfigError, "Poll period should be more then zero" if @options[:poll_period] < 1
|
51
48
|
if @options[:handler]
|
52
49
|
raise ConfigError, "Handler should be a class" unless @options[:handler].is_a?(Class)
|
@@ -61,7 +58,7 @@ module Daemonizer
|
|
61
58
|
end
|
62
59
|
end
|
63
60
|
|
64
|
-
[:
|
61
|
+
[:workers, :poll_period, :root, :cow_friendly].each do |method|
|
65
62
|
define_method method do
|
66
63
|
@options[method.to_sym]
|
67
64
|
end
|
data/lib/daemonizer/dsl.rb
CHANGED
@@ -13,7 +13,7 @@ module Daemonizer
|
|
13
13
|
def start_workers(&blk)
|
14
14
|
raise ArgumentError, "Need a worker block!" unless block_given?
|
15
15
|
|
16
|
-
@worker_pools[@config.pool] = WorkerPool.new(@config.pool, self,
|
16
|
+
@worker_pools[@config.pool] = WorkerPool.new(@config.pool, self, &blk)
|
17
17
|
@worker_pools[@config.pool].start_workers(@config.workers)
|
18
18
|
end
|
19
19
|
|
data/lib/daemonizer/worker.rb
CHANGED
@@ -3,7 +3,7 @@ module Daemonizer
|
|
3
3
|
attr_reader :name
|
4
4
|
attr_reader :pid
|
5
5
|
|
6
|
-
def initialize(name, pm,
|
6
|
+
def initialize(name, pm, worker_id, &blk)
|
7
7
|
raise ArgumentError, "Need a worker block!" unless block_given?
|
8
8
|
|
9
9
|
@name = name
|
@@ -23,80 +23,60 @@ module Daemonizer
|
|
23
23
|
|
24
24
|
def run
|
25
25
|
return if shutdown?
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
File.umask 0000
|
26
|
+
@pid = Kernel.fork do
|
27
|
+
Dir.chdir '/'
|
28
|
+
File.umask 0000
|
30
29
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
else
|
49
|
-
logger.error("Worker exited: #{message} at #{e.backtrace.first}")
|
50
|
-
end
|
30
|
+
STDIN.reopen '/dev/null'
|
31
|
+
STDOUT.reopen '/dev/null', 'a'
|
32
|
+
STDERR.reopen STDOUT
|
33
|
+
|
34
|
+
@pid = Process.pid
|
35
|
+
GDC.set "#{@pid}/#{@worker_id}"
|
36
|
+
normal_exit = false
|
37
|
+
begin
|
38
|
+
$0 = "#{@name} worker: instance #{@worker_id}\0"
|
39
|
+
@worker_block.call(@worker_id)
|
40
|
+
normal_exit = true
|
41
|
+
exit(0)
|
42
|
+
rescue Exception => e
|
43
|
+
message = SystemExit === e ? "exit(#{e.status})" : e.to_s
|
44
|
+
if SystemExit === e and e.success?
|
45
|
+
if normal_exit
|
46
|
+
logger.info("Worker finished: normal return")
|
51
47
|
else
|
52
|
-
logger.error("Worker exited
|
48
|
+
logger.error("Worker exited: #{message} at #{e.backtrace.first}")
|
53
49
|
end
|
54
|
-
|
50
|
+
else
|
51
|
+
logger.error("Worker exited with error: #{message}\n #{e.backtrace.join("\n ")}")
|
55
52
|
end
|
53
|
+
logger.debug("Terminating #{@name} worker: #{@pid}")
|
56
54
|
end
|
57
|
-
elsif @engine == 'thread'
|
58
|
-
@thread = Thread.start do
|
59
|
-
@worker_block.call(@worker_id)
|
60
|
-
end
|
61
|
-
else
|
62
|
-
raise ArgumentError, "Invalid engine name: #{@engine}"
|
63
55
|
end
|
64
56
|
rescue Exception => e
|
65
57
|
logger.error("Exception from worker: #{e} at #{e.backtrace.first}")
|
66
58
|
end
|
67
59
|
|
68
60
|
def running?(verbose = false)
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
return false
|
79
|
-
end
|
80
|
-
elsif @engine == 'thread'
|
81
|
-
@thread && @thread.alive?
|
82
|
-
else
|
83
|
-
raise ArgumentError, "Invalid engine name: #{@engine}"
|
61
|
+
return false unless @pid
|
62
|
+
begin
|
63
|
+
Process.waitpid(@pid, Process::WNOHANG)
|
64
|
+
res = Process.kill(0, @pid)
|
65
|
+
logger.debug("KILL(#{@pid}) = #{res}") if verbose
|
66
|
+
return true
|
67
|
+
rescue Errno::ESRCH, Errno::ECHILD, Errno::EPERM => e
|
68
|
+
logger.error("Exception from kill: #{e} at #{e.backtrace.first}") if verbose
|
69
|
+
return false
|
84
70
|
end
|
85
71
|
end
|
86
72
|
|
87
73
|
def stop(force = false)
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
logger.error("Exception from kill: #{e} at #{e.backtrace.first}")
|
95
|
-
end
|
96
|
-
elsif @engine == 'thread'
|
97
|
-
force && !defined?(::JRuby) ? @thread.kill! : @thread.kill
|
98
|
-
else
|
99
|
-
raise ArgumentError, "Invalid engine name: #{@engine}"
|
74
|
+
begin
|
75
|
+
sig = force ? 'SIGKILL' : 'SIGTERM'
|
76
|
+
logger.debug("Sending #{sig} to ##{@pid}")
|
77
|
+
Process.kill(sig, @pid)
|
78
|
+
rescue Errno::ESRCH, Errno::ECHILD, Errno::EPERM=> e
|
79
|
+
logger.error("Exception from kill: #{e} at #{e.backtrace.first}")
|
100
80
|
end
|
101
81
|
end
|
102
82
|
end
|
@@ -2,11 +2,10 @@ module Daemonizer
|
|
2
2
|
class WorkerPool
|
3
3
|
attr_reader :name, :logger
|
4
4
|
|
5
|
-
def initialize(name, pm,
|
5
|
+
def initialize(name, pm, &blk)
|
6
6
|
@name = name
|
7
7
|
@pm = pm
|
8
8
|
@worker_block = blk
|
9
|
-
@engine = engine
|
10
9
|
@workers = []
|
11
10
|
@logger = @pm.logger
|
12
11
|
end
|
@@ -18,7 +17,7 @@ module Daemonizer
|
|
18
17
|
def start_workers(number)
|
19
18
|
logger.debug "Creating #{number} workers for #{name} pool..."
|
20
19
|
number.times do |i|
|
21
|
-
@workers << Worker.new(name, @pm,
|
20
|
+
@workers << Worker.new(name, @pm, i+1, &@worker_block)
|
22
21
|
end
|
23
22
|
end
|
24
23
|
|
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: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 6
|
10
|
+
version: 0.1.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Gleb Pomykalov
|
@@ -113,6 +113,6 @@ rubyforge_project:
|
|
113
113
|
rubygems_version: 1.3.7
|
114
114
|
signing_key:
|
115
115
|
specification_version: 3
|
116
|
-
summary: Daemonizer allows you to easily create custom daemons on ruby. Supporting
|
116
|
+
summary: Daemonizer allows you to easily create custom daemons on ruby. Supporting prefork model
|
117
117
|
test_files: []
|
118
118
|
|