mikehale-daemons 1.0.12.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,111 @@
1
+ require 'daemons/pid'
2
+
3
+
4
+ module Daemons
5
+
6
+ # === What is a Pid-File?
7
+ # A <i>Pid-File</i> is a file containing the <i>process identification number</i>
8
+ # (pid) that is stored in a well-defined location of the filesystem thus allowing other
9
+ # programs to find out the pid of a running script.
10
+ #
11
+ # Daemons needs the pid of the scripts that are currently running in the background
12
+ # to send them so called _signals_. Daemons uses the +TERM+ signal to tell the script
13
+ # to exit when you issue a +stop+ command.
14
+ #
15
+ # === How does a Pid-File look like?
16
+ #
17
+ # Pid-Files generated by Daemons have to following format:
18
+ # <scriptname>.rb<number>.pid
19
+ # (Note that <tt><number></tt> is omitted if only one instance of the script can
20
+ # run at any time)
21
+ #
22
+ # Each file just contains one line with the pid as string (for example <tt>6432</tt>).
23
+ #
24
+ # === Where are the Pid-Files stored?
25
+ #
26
+ # Daemons is configurable to store the Pid-Files relative to three different locations:
27
+ # 1. in a directory relative to the directory where the script (the one that is supposed to run
28
+ # as a daemon) resides (<tt>:script</tt> option for <tt>:dir_mode</tt>)
29
+ # 2. in a directory given by <tt>:dir</tt> (<tt>:normal</tt> option for <tt>:dir_mode</tt>)
30
+ # 3. in the preconfigured directory <tt>/var/run</tt> (<tt>:system</tt> option for <tt>:dir_mode</tt>)
31
+ #
32
+ class PidFile < Pid
33
+
34
+ attr_reader :dir, :progname, :multiple, :number
35
+
36
+ def PidFile.find_files(dir, progname, delete = false)
37
+ files = Dir[File.join(dir, "#{progname}*.pid")]
38
+
39
+ files.delete_if {|f| not (File.file?(f) and File.readable?(f))}
40
+ if delete
41
+ files.delete_if do |f|
42
+ pid = File.open(f) {|h| h.read}.to_i
43
+ rsl = ! Pid.running?(pid)
44
+ if rsl
45
+ puts "pid-file for killed process #{pid} found (#{f}), deleting."
46
+ begin; File.unlink(f); rescue ::Exception; end
47
+ end
48
+ rsl
49
+ end
50
+ end
51
+
52
+ return files
53
+ end
54
+
55
+ def PidFile.existing(path)
56
+ new_instance = PidFile.allocate
57
+
58
+ new_instance.instance_variable_set(:@path, path)
59
+
60
+ def new_instance.filename
61
+ return @path
62
+ end
63
+
64
+ return new_instance
65
+ end
66
+
67
+ def initialize(dir, progname, multiple = false)
68
+ @dir = File.expand_path(dir)
69
+ @progname = progname
70
+ @multiple = multiple
71
+ @number = nil
72
+ @number = 0 if multiple
73
+
74
+ if multiple
75
+ while File.exist?(filename) and @number < 1024
76
+ @number += 1
77
+ end
78
+
79
+ if @number == 1024
80
+ raise RuntimeException('cannot run more than 1024 instances of the application')
81
+ end
82
+ end
83
+ end
84
+
85
+ def filename
86
+ File.join(@dir, "#{@progname}#{ @number or '' }.pid")
87
+ end
88
+
89
+ def exist?
90
+ File.exist? filename
91
+ end
92
+
93
+ def pid=(p)
94
+ File.open(filename, 'w') {|f|
95
+ f.puts p #Process.pid
96
+ }
97
+ end
98
+
99
+ def cleanup
100
+ File.delete(filename)
101
+ end
102
+
103
+ def pid
104
+ File.open(filename) {|f|
105
+ return f.gets.to_i
106
+ }
107
+ end
108
+
109
+ end
110
+
111
+ end
@@ -0,0 +1,10 @@
1
+ require 'daemons/pid'
2
+
3
+
4
+ module Daemons
5
+
6
+ class PidMem < Pid
7
+ attr_accessor :pid
8
+ end
9
+
10
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mikehale-daemons
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.12.1
5
+ platform: ruby
6
+ authors:
7
+ - Michael Hale
8
+ - Thomas Uehlinger
9
+ - Travis Whitton
10
+ - Chris Kline
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+
15
+ date: 2009-02-24 00:00:00 -08:00
16
+ default_executable:
17
+ dependencies: []
18
+
19
+ description: This is Daemons 1.0.10 with the addition of Chris Kline's fix from http://blog.rapleaf.com/dev/?p=19 Includes ability to change the process uid/gid. Also logdir can be specified seperate from piddir. Daemons provides an easy way to wrap existing ruby scripts (for example a self-written server) to be run as a daemon and to be controlled by simple start/stop/restart commands. If you want, you can also use daemons to run blocks of ruby code in a daemon process and to control these processes from the main application. Besides this basic functionality, daemons offers many advanced features like exception backtracing and logging (in case your ruby script crashes) and monitoring and automatic restarting of your processes if they crash. Daemons includes the daemonize.rb script written by Travis Whitton to do the daemonization process.
20
+ email: mikehale@gmail.com
21
+ executables: []
22
+
23
+ extensions: []
24
+
25
+ extra_rdoc_files: []
26
+
27
+ files:
28
+ - daemons.gemspec
29
+ - lib/daemons/application.rb
30
+ - lib/daemons/application_group.rb
31
+ - lib/daemons/cmdline.rb
32
+ - lib/daemons/controller.rb
33
+ - lib/daemons/daemonize.rb
34
+ - lib/daemons/exceptions.rb
35
+ - lib/daemons/monitor.rb
36
+ - lib/daemons/pid.rb
37
+ - lib/daemons/pidfile.rb
38
+ - lib/daemons/pidmem.rb
39
+ - lib/daemons/etc_extension.rb
40
+ - lib/daemons/change_privilege.rb
41
+ - lib/daemons.rb
42
+ - LICENSE
43
+ - Rakefile
44
+ - README
45
+ - Releases
46
+ - TODO
47
+ has_rdoc: false
48
+ homepage: http://github.com/mikehale/daemons
49
+ post_install_message:
50
+ rdoc_options: []
51
+
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.2.0
70
+ signing_key:
71
+ specification_version: 2
72
+ summary: A toolkit to convert your script to a controllable daemon (with Chris Kline's fix)
73
+ test_files: []
74
+