basic_daemon 0.9.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,21 +1,28 @@
1
- A basic library for creating daemonized processes
2
- (c) 2009 Samuel Mullen (samullen)
1
+ = BasicDaemon
2
+
3
+ http://github.com/samullen/basic_daemon
3
4
 
4
- http://github.com/samullen/basic_daemon
5
+ A basic library for creating daemonized processes
5
6
 
6
7
  This library works with Ruby 1.8, Ruby 1.9, and JRuby and is licensed under the MIT License.
7
8
 
8
- Installation
9
+ == Installation
9
10
 
10
- sudo gem install basic_daemon
11
+ The basic_daemon gem is hosted on GemCutter (http://gemcutter.org). This
12
+ requires that you have http://gemcutter.org in your gem sources.
11
13
 
12
- Gettings Started
14
+ Install the basic_daemon gem:
13
15
 
14
- There are currently two ways of using the basic_daemon library: 1) Objected Oriented; 2) Functional.
16
+ sudo gem install basic_daemon
15
17
 
16
- Object Oriented
18
+ == Gettings Started
17
19
 
18
- The basic idea here is to subclass BasicDaemon and overwrite the "run" method.
20
+ There are currently two ways of using the basic_daemon library: 1) Objected
21
+ Oriented; 2) Functional.
22
+
23
+ === Object Oriented
24
+
25
+ The basic idea here is to subclass BasicDaemon and overwride the "run" method.
19
26
 
20
27
  require 'basic_daemon'
21
28
 
@@ -49,7 +56,7 @@ The basic idea here is to subclass BasicDaemon and overwrite the "run" method.
49
56
  exit!
50
57
  end
51
58
 
52
- Functional
59
+ === Functional
53
60
 
54
61
  Rather than subclassing BasicDaemon, a block is just passed to the start method.
55
62
 
@@ -57,8 +64,7 @@ Rather than subclassing BasicDaemon, a block is just passed to the start method.
57
64
 
58
65
  daemon = BasicDaemon.new
59
66
 
60
- if ARGV[0] == 'start'
61
- daemon.start do
67
+ process = Proc.new do
62
68
  foo = open("/tmp/out", "w")
63
69
 
64
70
  i = 1
@@ -71,24 +77,36 @@ Rather than subclassing BasicDaemon, a block is just passed to the start method.
71
77
  i += 1
72
78
  end
73
79
  end
80
+
81
+ if ARGV[0] == 'start'
82
+ daemon.start &process
74
83
  elsif ARGV[0] == 'stop'
75
84
  daemon.stop
76
85
  exit!
77
86
  elsif ARGV[0] == 'restart'
78
- daemon.restart
87
+ daemon.restart &process
79
88
  else
80
89
  STDERR.puts "Usage: foo_daemon.rb <start|stop|restart>"
81
90
  exit!
82
91
  end
83
92
 
84
- Arguments
93
+ === Arguments
85
94
 
86
- BasicDaemon creates file to store the process ID (PID). By default, this file is given the same name as the calling script sans the file extension and is stored in the /tmp directory. Also by default, the directory the calling script is working in is changed to the root directory ('/').
95
+ BasicDaemon creates file to store the process ID (PID). By default, this file
96
+ is given the same name as the calling script sans the file extension and is
97
+ stored in the /tmp directory. Also by default, the directory the calling script
98
+ is working in is changed to the root directory ('/').
87
99
 
88
- Each argument, :pidfile, :piddir, :workingdir, can be changed upon instantiation of the class.
100
+ Each argument, :pidfile, :piddir, :workingdir, can be changed upon
101
+ instantiation of the class.
89
102
 
90
- Example:
103
+ ==== Example:
91
104
 
92
105
  Most Linux users will want to instantiate thusly:
93
106
 
94
107
  BasicDaemon.new(:piddir => '/var/lock')
108
+
109
+ == Copyright
110
+
111
+ Copyright(c) 2009 Samuel Mullen (samullen). See LICENSE for details
112
+
data/Rakefile CHANGED
@@ -20,7 +20,6 @@ gem_spec = Gem::Specification.new do |s|
20
20
  s.test_files = Dir['test/**/*.rb']
21
21
  s.description = false
22
22
  s.files = [
23
- "History.rdoc",
24
23
  "LICENSE",
25
24
  "README.rdoc",
26
25
  "Rakefile",
@@ -7,6 +7,8 @@ pidfile = File.basename($PROGRAM_NAME, File.extname($PROGRAM_NAME))
7
7
 
8
8
  d = BasicDaemon.new({:pidfile => pidfile, :piddir => basedir, :workingdir => basedir})
9
9
 
10
+ # for restarts to work properly, you can't use anonymous blocks. In other
11
+ # words, blocks have to be assigned to a variable
10
12
  process = Proc.new do
11
13
  i = 1
12
14
  foo = open(basedir + "/out", "w")
@@ -1,7 +1,7 @@
1
1
  class BasicDaemon
2
2
  attr_accessor :workingdir, :pidfile, :piddir, :process
3
3
 
4
- VERSION = '0.9.1'
4
+ VERSION = '1.0.0'
5
5
 
6
6
  DEFAULT_OPTIONS = {
7
7
  :pidfile => File.basename($PROGRAM_NAME, File.extname($PROGRAM_NAME)),
@@ -9,14 +9,14 @@ class BasicDaemon
9
9
  :workingdir => '/'
10
10
  }
11
11
 
12
- # Instantiate a new BasicDaemon
13
- #
14
- # Takes an optional hash with the following symbol keys:
15
- # - :piddir = Directory to store the PID file in. Default is /tmp
16
- # - :pidfile = name of the file to store the PID in. default is the script
17
- # name sans extension.
18
- # - :workingdir = Directory to work from. Default is "/" and should probably
19
- # be left as such.
12
+ # Instantiate a new BasicDaemon
13
+ #
14
+ # Takes an optional hash with the following symbol keys:
15
+ # - :piddir = Directory to store the PID file in. Default is /tmp
16
+ # - :pidfile = name of the file to store the PID in. default is the script
17
+ # name sans extension.
18
+ # - :workingdir = Directory to work from. Default is "/" and should probably
19
+ # be left as such.
20
20
  def initialize(*args)
21
21
  opts = {}
22
22
 
@@ -77,22 +77,13 @@ class BasicDaemon
77
77
  fork && exit!
78
78
 
79
79
  at_exit do
80
- delpidfile
80
+ remove_pidfile
81
81
  end
82
82
 
83
83
  Dir::chdir(@workingdir) #----- chdir to working directory
84
84
  File::umask(0) #----- clear out file mode creation mask
85
85
 
86
- begin
87
- open(self.pidpath, "w") do |f|
88
- @pid = Process.pid
89
- f.puts @pid
90
- end
91
- rescue => e
92
- STDERR.puts "Error: Unable to open #{self.pidpath} for writing:\n\t" +
93
- "(#{e.class}) #{e.message}"
94
- exit!
95
- end
86
+ create_pidfile
96
87
 
97
88
  STDIN.reopen("/dev/null", 'r')
98
89
  STDOUT.reopen("/dev/null", "w")
@@ -169,8 +160,22 @@ class BasicDaemon
169
160
 
170
161
  private
171
162
 
172
- #----------------------------------------------------------------------------#
173
- def delpidfile
163
+ # Writes the process ID to the pidfile and defines @pid as such
164
+ def create_pidfile
165
+ begin
166
+ open(self.pidpath, "w") do |f|
167
+ @pid = Process.pid
168
+ f.puts @pid
169
+ end
170
+ rescue => e
171
+ STDERR.puts "Error: Unable to open #{self.pidpath} for writing:\n\t" +
172
+ "(#{e.class}) #{e.message}"
173
+ exit!
174
+ end
175
+ end
176
+
177
+ # removes the pidfile. Called on exit
178
+ def remove_pidfile
174
179
  begin
175
180
  File.unlink(self.pidpath)
176
181
  rescue => e
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: basic_daemon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
5
10
  platform: ruby
6
11
  authors:
7
12
  - Samuel Mullen
@@ -22,7 +27,6 @@ extensions: []
22
27
  extra_rdoc_files: []
23
28
 
24
29
  files:
25
- - History.rdoc
26
30
  - LICENSE
27
31
  - README.rdoc
28
32
  - Rakefile
@@ -45,18 +49,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
45
49
  requirements:
46
50
  - - ">="
47
51
  - !ruby/object:Gem::Version
52
+ segments:
53
+ - 0
48
54
  version: "0"
49
- version:
50
55
  required_rubygems_version: !ruby/object:Gem::Requirement
51
56
  requirements:
52
57
  - - ">="
53
58
  - !ruby/object:Gem::Version
59
+ segments:
60
+ - 0
54
61
  version: "0"
55
- version:
56
62
  requirements: []
57
63
 
58
64
  rubyforge_project:
59
- rubygems_version: 1.3.5
65
+ rubygems_version: 1.3.6
60
66
  signing_key:
61
67
  specification_version: 3
62
68
  summary: A simple ruby library for daemonizing processes
@@ -1,57 +0,0 @@
1
- = 0.9.0 - 20100216
2
-
3
- * Bumped the version to 0.9.0 because it's just about ready for a 1.0 release.
4
- * cleaned up tests and added a restart test to oo and function
5
- * Still need to work on documentation and fix the Rakefile
6
-
7
- = 0.1.5 - 20100118
8
-
9
- Doing some refactoring and broke out code which determines the PID into its
10
- own method "pid".
11
-
12
- Added LICENSE and updated TODO
13
-
14
- Updated Rakefile and tests
15
-
16
- = 0.1.4 - 20100115
17
-
18
- Provided basic documentation for the methods.
19
-
20
- = 0.1.3 - 20091230
21
-
22
- Breaking out tests
23
-
24
- * Broke out tests to organize by functionality
25
-
26
- = 0.1.2 - 20091230
27
-
28
- Switched back to Test::Unit
29
-
30
- * Migrated all RSpec tests to Test::Unit
31
- * Expanded tests to handle calling blocks
32
-
33
- = 0.1.1 - 20090925
34
-
35
- RSpec testing expansion.
36
-
37
- * continuing to add tests to the spec.
38
- * added a method to the library to determine if a PID is alive
39
-
40
- = 0.1.0 - 20090923
41
-
42
- Major bug fixes
43
-
44
- * The daemon now correctly forks off from the calling process.
45
- * The procedural means of executing a daemon have been eliminated.
46
-
47
- = 0.0.5 - 20090921
48
-
49
- Began moving tests to RSpec.
50
-
51
- = 0.0.5 - 20090812
52
-
53
- Started recording changes to the repository in to line up with git commit
54
- messages.
55
-
56
- * addition of License.txt and History.txt, though mostly empty
57
- * Initial gemspec was created. The daemon is now gemable