pidfile 0.2.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 (5) hide show
  1. data/LICENSE +21 -0
  2. data/README.rdoc +46 -0
  3. data/Rakefile +51 -0
  4. data/lib/pidfile.rb +124 -0
  5. metadata +65 -0
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2010 Samuel Mullen
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
@@ -0,0 +1,46 @@
1
+ = PidFile
2
+
3
+ http://github.com/samullen/pidfile
4
+
5
+ A basic library for creating lockfiles for processes
6
+
7
+ This library works with Ruby 1.8 and 1.9 and is licensed under the MIT License.
8
+
9
+ == Installation
10
+
11
+ The pidfile gem is hosted on RubyGems.org (http://rubygems.org). This
12
+ requires that you have http://rubygems.org in your gem sources.
13
+
14
+ Install the pidfile gem:
15
+
16
+ sudo gem install pidfile
17
+
18
+ == Gettings Started
19
+
20
+ The pidfile gem is easy to use and only requires the addition of a single line
21
+ of code; something like this:
22
+
23
+ pf = PidFile.new unless PidFile.running?
24
+
25
+ Of course there is other functionality, but if you are just wanting to keep a
26
+ process from running more than once, this is all you need.
27
+
28
+
29
+ === Arguments
30
+
31
+ PidFile creates file to store the process ID (PID). By default, process ID
32
+ (PID) files are created in /tmp upon instantiation and are given the same name
33
+ as the calling script with a .pid extension. Process ID files are removed when
34
+ the creating program exits.
35
+
36
+ Each argument, :pidfile and :piddir, can be changed upon instantiation of the
37
+ class.
38
+
39
+ ==== Example:
40
+
41
+ PidFile.new(:piddir => '/var/lock', :pidfile => "awesome.pid")
42
+
43
+ == Copyright
44
+
45
+ Copyright(c) 2010 Samuel Mullen (samullen). See LICENSE for details
46
+
@@ -0,0 +1,51 @@
1
+ $:.unshift File.expand_path("../lib", __FILE__)
2
+
3
+ require 'rubygems'
4
+ require 'lib/pidfile'
5
+ require 'rake/testtask'
6
+ require 'rake/gempackagetask'
7
+
8
+ lib_dir = File.expand_path('lib')
9
+ test_dir = File.expand_path('test')
10
+
11
+ gem_spec = Gem::Specification.new do |s|
12
+ s.name = "pidfile"
13
+ s.version = PidFile::VERSION
14
+ s.authors = ["Samuel Mullen"]
15
+ s.email = "samullen@gmail.com"
16
+ s.homepage = "http://github.com/samullen/pidfile"
17
+ s.summary = "A basic library for creating lockfiles for processes"
18
+ s.authors = ["Samuel Mullen"]
19
+ s.email = "samullen@gmail.com"
20
+ s.test_files = Dir['test/**/*.rb']
21
+ s.description = false
22
+ s.files = [
23
+ "LICENSE",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ # "examples/functional.rb",
27
+ # "examples/objectoriented.rb",
28
+ "lib/pidfile.rb",
29
+ ] + s.test_files
30
+ end
31
+
32
+ Rake::TestTask.new(:test) do |test|
33
+ test.libs = [lib_dir, test_dir]
34
+ test.pattern = 'test/**/*rb'
35
+ test.verbose = true
36
+ end
37
+
38
+ Rake::GemPackageTask.new(gem_spec) do |pkg|
39
+ pkg.need_zip = false
40
+ pkg.need_tar = false
41
+ end
42
+
43
+ desc "Install the gem locally"
44
+ task :install => [:test, :gem] do
45
+ sh %{gem install pkg/#{gem_spec.name}-#{gem_spec.version}}
46
+ end
47
+
48
+ desc "Remove the pkg directory and all of its contents."
49
+ task :clean => :clobber_package
50
+
51
+ task :default => [:test, :gem]
@@ -0,0 +1,124 @@
1
+ class PidFile
2
+ attr_accessor :pidfile, :piddir
3
+
4
+ VERSION = '0.2.0'
5
+
6
+ DEFAULT_OPTIONS = {
7
+ :pidfile => File.basename($0, File.extname($0)) + ".pid",
8
+ :piddir => '/tmp',
9
+ }
10
+
11
+ def initialize(*args)
12
+ opts = {}
13
+
14
+ case
15
+ when args.length == 0 then
16
+ when args.length == 1 && args[0].class == Hash then
17
+ arg = args.shift
18
+
19
+ if arg.class == Hash
20
+ opts = arg
21
+ end
22
+ else
23
+ raise ArgumentError, "new() expects hash or hashref as argument"
24
+ end
25
+
26
+ opts = DEFAULT_OPTIONS.merge opts
27
+
28
+ @piddir = opts[:piddir]
29
+ @pidfile = opts[:pidfile]
30
+ @fh = nil
31
+
32
+ create_pidfile
33
+
34
+ at_exit { release }
35
+ end
36
+
37
+ # Returns the fullpath to the file containing the process ID (PID)
38
+ def pidpath
39
+ File.join(@piddir, @pidfile)
40
+ end
41
+
42
+ # Returns the PID, if any, of the instantiating process
43
+ def pid
44
+ return @pid unless @pid.nil?
45
+
46
+ if self.pidfile_exists?
47
+ @pid = open(self.pidpath, 'r').read.to_i
48
+ else
49
+ @pid = nil
50
+ end
51
+ end
52
+
53
+ # Boolean stating whether this process is alive and running
54
+ def alive?
55
+ return false unless self.pid && (self.pid == Process.pid)
56
+
57
+ self.class.process_exists?(self.pid)
58
+ end
59
+
60
+ # does the pidfile exist?
61
+ def pidfile_exists?
62
+ self.class.pidfile_exists?(pidpath)
63
+ end
64
+
65
+ # unlock and remove the pidfile. Sets pid to nil
66
+ def release
67
+ unless @fh.nil?
68
+ @fh.flock(File::LOCK_UN)
69
+ remove_pidfile
70
+ end
71
+ @pid = nil
72
+ end
73
+
74
+ # returns the modification time of the pidfile
75
+ def locktime
76
+ File.mtime(self.pidpath)
77
+ end
78
+
79
+ # class method for determining the existence of pidfile
80
+ def self.pidfile_exists?(path=nil)
81
+ path ||= File.join(DEFAULT_OPTIONS[:piddir], DEFAULT_OPTIONS[:pidfile])
82
+
83
+ File.exists?(path)
84
+ end
85
+
86
+ # boolean stating whether the calling program is already running
87
+ def self.running?(path=nil)
88
+ path ||= File.join(DEFAULT_OPTIONS[:piddir], DEFAULT_OPTIONS[:pidfile])
89
+
90
+ if pidfile_exists?(path)
91
+ pid = open(path, 'r').read.to_i
92
+ else
93
+ pid = nil
94
+ end
95
+
96
+ return false unless pid && (pid != Process.pid)
97
+
98
+ process_exists?(pid)
99
+ end
100
+
101
+ private
102
+
103
+ # Writes the process ID to the pidfile and defines @pid as such
104
+ def create_pidfile
105
+ @fh = open(self.pidpath, "w")
106
+ @fh.flock(File::LOCK_EX | File::LOCK_NB) || raise
107
+ @pid = Process.pid
108
+ @fh.puts @pid
109
+ end
110
+
111
+ # removes the pidfile.
112
+ def remove_pidfile
113
+ File.unlink(self.pidpath) if self.pidfile_exists?
114
+ end
115
+
116
+ def self.process_exists?(process_id)
117
+ begin
118
+ Process.kill(0, process_id)
119
+ true
120
+ rescue Errno::ESRCH, TypeError # "PID is NOT running or is zombied
121
+ false
122
+ end
123
+ end
124
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pidfile
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
+ platform: ruby
11
+ authors:
12
+ - Samuel Mullen
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-06-14 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: "false"
22
+ email: samullen@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - LICENSE
31
+ - README.rdoc
32
+ - Rakefile
33
+ - lib/pidfile.rb
34
+ has_rdoc: true
35
+ homepage: http://github.com/samullen/pidfile
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ segments:
48
+ - 0
49
+ version: "0"
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.3.6
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: A basic library for creating lockfiles for processes
64
+ test_files: []
65
+