light-daemon 0.9.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.
Files changed (2) hide show
  1. data/lib/light_daemon.rb +114 -0
  2. metadata +67 -0
@@ -0,0 +1,114 @@
1
+ module LightDaemon
2
+ class Daemon
3
+ def initialize(obj, options={})
4
+ @obj = obj
5
+ @options = options
6
+ log("before daemonize")
7
+ daemonize
8
+ log("after daemonize")
9
+ @processes = []
10
+ signal_handling
11
+ end
12
+
13
+ def self.start(obj, options={})
14
+ self.new(obj, options).start
15
+ end
16
+
17
+ def signal_handling
18
+ $_simple_daemon_running = true
19
+ Signal.trap "TERM" do
20
+ log("Daemon received TERM !!!!!!!!!!!!!!!!!")
21
+ @processes.each do |pid|
22
+ Process.kill("TERM", pid)
23
+ end
24
+
25
+ sleep(3)
26
+ 3.times do
27
+ @processes.size.times do
28
+ pid = @processes.shift
29
+ if process_alive?(pid)
30
+ @processes << pid
31
+ sleep(3)
32
+ end
33
+ end
34
+ end
35
+ @processes.each do |pid|
36
+ Process.kill(9, pid)
37
+ end
38
+ $_simple_daemon_running = false
39
+ end
40
+ end
41
+
42
+ def log(msg)
43
+ File.open("/tmp/yi.txt", "a") do |f|
44
+ f.puts msg
45
+ end
46
+ end
47
+
48
+ def process_alive?(pid)
49
+ return false unless pid
50
+ begin
51
+ Process.kill(0, pid)
52
+ return true
53
+ rescue Errno::ESRCH
54
+ return false
55
+ rescue ::Exception # for example on EPERM (process exists but does not belong to us)
56
+ return true
57
+ end
58
+ end
59
+
60
+
61
+ def start
62
+ 5.times do
63
+ if pid = fork
64
+ @processes << pid
65
+ Process.detach(pid)
66
+ else
67
+ return @obj.send(:call)
68
+ end
69
+ end
70
+
71
+ while($_simple_daemon_running)
72
+ sleep(5)
73
+ @processes.each_with_index do |pid, index|
74
+ unless process_alive?(pid)
75
+ if pid = fork
76
+ @processes[index] = pid
77
+ Process.detach(pid)
78
+ else
79
+ return @obj.send(:call)
80
+ end
81
+ end
82
+ sleep(1)
83
+ end
84
+ end
85
+ end
86
+
87
+ def daemonize
88
+ fork && exit
89
+ unless Process.setsid
90
+ raise 'cannot detach from controlling terminal'
91
+ end
92
+
93
+ trap 'SIGHUP', 'IGNORE'
94
+ exit if fork
95
+
96
+ ObjectSpace.each_object(IO) do |io|
97
+ unless [STDIN, STDOUT, STDERR].include?(io)
98
+ begin
99
+ unless io.closed?
100
+ io.close
101
+ end
102
+ rescue ::Exception
103
+ end
104
+ end
105
+ end
106
+
107
+ begin; STDIN.reopen "/dev/null"; rescue ::Exception; end
108
+ begin; STDOUT.reopen "/dev/null"; rescue ::Exception; end
109
+ begin; STDERR.reopen STDOUT; rescue ::Exception; end
110
+ STDERR.sync = true
111
+
112
+ end
113
+ end
114
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: light-daemon
3
+ version: !ruby/object:Gem::Version
4
+ hash: 59
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 9
9
+ - 0
10
+ version: 0.9.0
11
+ platform: ruby
12
+ authors:
13
+ - Yi Zhang
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-03-08 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: A simple and light daemon
23
+ email: yzhang.wa@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - lib/light_daemon.rb
32
+ has_rdoc: true
33
+ homepage: http://rubygems.org/gems/light_daemon
34
+ licenses: []
35
+
36
+ post_install_message:
37
+ rdoc_options: []
38
+
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ hash: 3
47
+ segments:
48
+ - 0
49
+ version: "0"
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.6.2
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Light Daemon!
66
+ test_files: []
67
+