light-daemon 0.9.7 → 0.9.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README +10 -13
  2. data/lib/light_daemon.rb +37 -39
  3. metadata +18 -40
data/README CHANGED
@@ -1,7 +1,8 @@
1
- light-daemon is a ruby gem. It could help developers quickly build a daemon which prefok worker processes and monitor them.
1
+ Light-daemon is a ruby gem. It could help developers quickly build a daemon which preforks worker processes and monitors them.
2
+
2
3
  The usage is very simple:
3
4
 
4
- #---------------------------------------------------------------------------------------------
5
+ #------------------------------------------------------------------------------
5
6
  require 'rubygems'
6
7
  require 'light_daemon'
7
8
 
@@ -16,19 +17,17 @@ The usage is very simple:
16
17
  end
17
18
  end
18
19
 
19
- LightDaemon::Daemon.start(Client.new, :children=> 2, :pid_file => "/tmp/light-daemon.pid" )
20
- #---------------------------------------------------------------------------------------------
20
+ LightDaemon::Daemon.start(Client.new, :children=> 2, :pid_file => "/tmp/light-daemon.pid")
21
+ #------------------------------------------------------------------------------
21
22
 
22
23
  What you need to do is:
24
+
23
25
  1. Create your worker class. Define a method "call" and put the real work into it.
24
26
  2. The method "call" needs to return true for the daemon to continuously call it.
25
- 3. You need to tell the daemon how many worker processes you want and a filename for daemon to store
26
- the PID of the daemon process.
27
- 4. If the method "call" returns false, the daemon will kill this work process and create a new one.
28
- This would be very helpful if your code might have memory leaking and you want it to restart after
29
- certain criteria is met. An example is as following:
27
+ 3. You need to tell the daemon how many worker processes you want and a filename for daemon to store the PID of the daemon process.
28
+ 4. If the method "call" returns false, the daemon will kill this worker process and create a new one. This would be very helpful if your code might have memory leaking and you want it to restart after certain criteria is met. An example is as following:
30
29
 
31
- #---------------------------------------------------------------------------------------------
30
+ #------------------------------------------------------------------------------
32
31
  require 'rubygems'
33
32
  require 'light_daemon'
34
33
 
@@ -46,12 +45,10 @@ What you need to do is:
46
45
  end
47
46
 
48
47
  LightDaemon::Daemon.start(Client.new, :children=> 2, :pid_file => "/tmp/light-daemon.pid" )
49
- #---------------------------------------------------------------------------------------------
50
-
48
+ #------------------------------------------------------------------------------
51
49
 
52
50
 
53
51
 
54
52
  Yi Zhang
55
53
  yzhang[dot]wa[at]gmail[dot]com
56
54
  3/11/2012
57
-
@@ -5,7 +5,7 @@ module LightDaemon
5
5
  class << self
6
6
  def start(obj, options={})
7
7
  @options = self.make_options(options)
8
- @pid_file = @options[:pid_file]
8
+ @pid_file = @options[:pid_file]
9
9
  if self.get_pid
10
10
  raise "The pid file \"#{@pid_file}\" existed already. You need to stop the daemon first."
11
11
  end
@@ -15,21 +15,21 @@ module LightDaemon
15
15
  def stop(pid_file=DEFAULT_PID_FILE)
16
16
  @pid_file = pid_file
17
17
  if(pid = self.get_pid)
18
- begin
19
- Process.kill("TERM", pid)
20
- rescue Errno::ESRCH
21
- # no such process. do nothing
18
+ begin
19
+ Process.kill("TERM", pid)
20
+ rescue Errno::ESRCH
21
+ # no such process. do nothing
22
22
  end
23
- self.clear_pid
23
+ self.clear_pid
24
24
  end
25
25
  end
26
26
 
27
27
  def get_pid
28
28
  return nil unless File.exist?(@pid_file)
29
- begin
30
- File.open(@pid_file) {|f| f.read}.to_i
29
+ begin
30
+ File.open(@pid_file) {|f| f.read}.to_i
31
31
  rescue
32
- nil
32
+ nil
33
33
  end
34
34
  end
35
35
 
@@ -45,7 +45,7 @@ module LightDaemon
45
45
  options[:children] ||= 1
46
46
  options[:monitor_cycle] ||= 10 # seconds
47
47
  options[:pid_file] ||= DEFAULT_PID_FILE
48
- options
48
+ options
49
49
  end
50
50
 
51
51
  # for debugging
@@ -74,19 +74,19 @@ module LightDaemon
74
74
  @processes.each do |pid|
75
75
  Process.kill("TERM", pid)
76
76
  end
77
-
77
+
78
78
  sleep(3)
79
79
  options[:children].times do
80
- @processes.size.times do
81
- pid = @processes.shift
82
- if process_alive?(pid)
83
- @processes << pid
84
- sleep(3)
80
+ @processes.size.times do
81
+ pid = @processes.shift
82
+ if process_alive?(pid)
83
+ @processes << pid
84
+ sleep(3)
85
85
  end
86
- end
86
+ end
87
87
  end
88
- @processes.each do |pid|
89
- Process.kill(9, pid)
88
+ @processes.each do |pid|
89
+ Process.kill(9, pid)
90
90
  end
91
91
  end
92
92
  $keep_running = false
@@ -110,35 +110,29 @@ module LightDaemon
110
110
  @options[:children].times do
111
111
  if pid = fork
112
112
  @processes << pid
113
- Process.detach(pid)
113
+ Process.detach(pid)
114
114
  else
115
- set_child
116
- while($keep_running)
117
- break unless @obj.send(:call)
118
- end
119
- return
120
- end
115
+ run_child
116
+ return
117
+ end
121
118
  end
122
119
 
123
120
  while($keep_running)
124
121
  sleep(@options[:monitor_cycle])
125
122
 
126
- @processes.each_with_index do |pid, index|
123
+ @processes.each_with_index do |pid, index|
127
124
  unless process_alive?(pid)
128
125
  if pid = fork
129
- @processes[index] = pid
130
- Process.detach(pid)
131
- sleep(2)
132
- else
133
- set_child
134
- while($keep_running)
135
- break unless @obj.send(:call)
136
- end
126
+ @processes[index] = pid
127
+ Process.detach(pid)
128
+ sleep(2)
129
+ else
130
+ run_child
137
131
  return
138
- end
132
+ end
139
133
  else
140
134
  end
141
- sleep(1)
135
+ sleep(1)
142
136
  end
143
137
  end
144
138
  self.class.clear_pid(@options[:pid_file])
@@ -148,7 +142,7 @@ module LightDaemon
148
142
  def daemonize
149
143
  fork && exit
150
144
  unless Process.setsid
151
- raise 'cannot detach from controlling terminal'
145
+ raise 'cannot detach from controlling terminal'
152
146
  end
153
147
 
154
148
  trap 'SIGHUP', 'IGNORE'
@@ -160,8 +154,12 @@ module LightDaemon
160
154
  STDERR.sync = true
161
155
  end
162
156
 
163
- def set_child
157
+ def run_child
164
158
  @is_child = true
159
+ target_obj = (@obj.class.name == 'String')? Object.const_get(@obj).new : @obj
160
+ while($keep_running)
161
+ break unless target_obj.send(:call)
162
+ end
165
163
  end
166
164
  end
167
165
  end
metadata CHANGED
@@ -1,68 +1,46 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: light-daemon
3
- version: !ruby/object:Gem::Version
4
- hash: 53
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.8
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 9
9
- - 7
10
- version: 0.9.7
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Yi Zhang
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2012-03-11 00:00:00 -08:00
19
- default_executable:
12
+ date: 2012-11-21 00:00:00.000000000 Z
20
13
  dependencies: []
21
-
22
14
  description: A very simple and light ruby daemon API
23
15
  email: yzhang.wa@gmail.com
24
16
  executables: []
25
-
26
17
  extensions: []
27
-
28
18
  extra_rdoc_files: []
29
-
30
- files:
19
+ files:
31
20
  - README
32
21
  - lib/light_daemon.rb
33
- has_rdoc: true
34
22
  homepage: http://github.com/yzhanginwa/light-daemon
35
23
  licenses: []
36
-
37
24
  post_install_message:
38
25
  rdoc_options: []
39
-
40
- require_paths:
26
+ require_paths:
41
27
  - lib
42
- required_ruby_version: !ruby/object:Gem::Requirement
28
+ required_ruby_version: !ruby/object:Gem::Requirement
43
29
  none: false
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- hash: 3
48
- segments:
49
- - 0
50
- version: "0"
51
- required_rubygems_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
35
  none: false
53
- requirements:
54
- - - ">="
55
- - !ruby/object:Gem::Version
56
- hash: 3
57
- segments:
58
- - 0
59
- version: "0"
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
60
40
  requirements: []
61
-
62
41
  rubyforge_project:
63
- rubygems_version: 1.6.2
42
+ rubygems_version: 1.8.24
64
43
  signing_key:
65
44
  specification_version: 3
66
45
  summary: Light Daemon is awesome!
67
46
  test_files: []
68
-