pid 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +22 -0
- data/README.rdoc +20 -0
- data/Rakefile +19 -0
- data/VERSION +1 -0
- data/lib/core_ext.rb +1 -0
- data/lib/core_ext/string.rb +25 -0
- data/lib/pid.rb +97 -0
- data/pid.gemspec +41 -0
- metadata +53 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1c74deaebd61e613000eafe5a657639f31aadfe5
|
4
|
+
data.tar.gz: 5adc0f986456ad6a4efd4a8694f0e14d32339739
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d0b2b6d7a85cce9cca1270a4fb51a5a5c732ce6ea7d6c2f7e383b82da7062493a2dc60ae30b6f524b93d34340346a2519b77cfe7eb33bdb830c8d4ab2c1e7a57
|
7
|
+
data.tar.gz: e58bbcd44d111c082d0c89a4c9f4d7636e8a91b3dd411a700a23ce9ec0a9a34cadea1be42e6d51d7eaead6cc8e062cbbe368766af311a2fd05b1ee68d1f1064f
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 debbbbie
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/README.rdoc
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
= pid
|
2
|
+
|
3
|
+
Library for managing pidfiles. It was extracted from Kenneth
|
4
|
+
Kalmer's excellent daemon-kit project (http://github.com/kennethkalmer/daemon-kit).
|
5
|
+
The daemon-kit framework is amazing for bootstrapping your daemons, but if
|
6
|
+
you need something more lightweight (e.g., a single file daemon), then you're
|
7
|
+
out of luck (for now). In the mean time, there weren't any obvious, simple
|
8
|
+
pid file management libraries that you could trust. This one works well and
|
9
|
+
has facilities to ensure the process the pid file is pointing to is still running,
|
10
|
+
so you can handle stale pid files easily.
|
11
|
+
|
12
|
+
= Usage
|
13
|
+
|
14
|
+
Some samples of the most common uses:
|
15
|
+
|
16
|
+
require 'rubygems'
|
17
|
+
require 'pid'
|
18
|
+
|
19
|
+
Pid.running?("/path/to/your.pid") # pid filepath
|
20
|
+
Pid.running?(pid) # the pid of process
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "pid"
|
8
|
+
gem.summary = %Q{Library for managing pidfiles.}
|
9
|
+
gem.description = %Q{Library for managing pidfiles.}
|
10
|
+
gem.email = "debbbbie@163.com"
|
11
|
+
gem.homepage = "http://github.com/debbbbie/pid_rb"
|
12
|
+
gem.authors = ["debbbbie"]
|
13
|
+
# gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
data/lib/core_ext.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/core_ext/string'
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# Extracted from Kenneth Kalmer's excellent daemon-kit project
|
2
|
+
# on GitHub: http://github.com/kennethkalmer/daemon-kit
|
3
|
+
|
4
|
+
require 'pathname'
|
5
|
+
|
6
|
+
class String
|
7
|
+
|
8
|
+
# Assuming the string is a file or path name, convert it into an
|
9
|
+
# absolute path.
|
10
|
+
def to_absolute_path
|
11
|
+
# Pathname is incompatible with Windows, but Windows doesn't have
|
12
|
+
# real symlinks so File.expand_path is safe.
|
13
|
+
if RUBY_PLATFORM =~ /(:?mswin|mingw)/
|
14
|
+
File.expand_path( self )
|
15
|
+
|
16
|
+
# Otherwise use Pathname#realpath which respects symlinks.
|
17
|
+
else
|
18
|
+
begin
|
19
|
+
File.expand_path( Pathname.new( self ).realpath.to_s )
|
20
|
+
rescue Errno::ENOENT
|
21
|
+
File.expand_path( Pathname.new( self ).cleanpath.to_s )
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/pid.rb
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
# Much of this was extracted from Kenneth Kalmer's excellent
|
2
|
+
# daemon-kit project on GitHub: http://github.com/kennethkalmer/daemon-kit
|
3
|
+
|
4
|
+
require File.dirname(__FILE__) + '/core_ext'
|
5
|
+
|
6
|
+
class Pid
|
7
|
+
def initialize(path)
|
8
|
+
@path = path.to_absolute_path
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.drop(path)
|
12
|
+
p = self.new(path)
|
13
|
+
if p.exists?
|
14
|
+
unless p.running?
|
15
|
+
p.cleanup
|
16
|
+
p.write!
|
17
|
+
end
|
18
|
+
else
|
19
|
+
p.write!
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.cleanup(path)
|
24
|
+
p = self.new(path)
|
25
|
+
if p.running?
|
26
|
+
return false
|
27
|
+
else
|
28
|
+
p.cleanup
|
29
|
+
return true
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.cleanup!(path)
|
34
|
+
p = self.new(path)
|
35
|
+
p.cleanup if p.exists?
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.running?(pid_or_filepath)
|
39
|
+
object = self.new(pid_or_filepath)
|
40
|
+
object.running?
|
41
|
+
end
|
42
|
+
|
43
|
+
def exists?
|
44
|
+
File.exists?(@path)
|
45
|
+
end
|
46
|
+
|
47
|
+
# Returns true if the process is running
|
48
|
+
def running?
|
49
|
+
return false unless self.exists?
|
50
|
+
|
51
|
+
# Check if process is in existence
|
52
|
+
# The simplest way to do this is to send signal '0'
|
53
|
+
# (which is a single system call) that doesn't actually
|
54
|
+
# send a signal
|
55
|
+
begin
|
56
|
+
Process.kill(0, self.pid)
|
57
|
+
return true
|
58
|
+
rescue Errno::ESRCH
|
59
|
+
return false
|
60
|
+
rescue ::Exception # for example on EPERM (process exists but does not belong to us)
|
61
|
+
return true
|
62
|
+
#rescue Errno::EPERM
|
63
|
+
# return false
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# Return the pid contained in the pidfile, or nil
|
68
|
+
def pid
|
69
|
+
return nil unless self.exists?
|
70
|
+
|
71
|
+
File.open( @path ) { |f| return f.gets.to_i }
|
72
|
+
end
|
73
|
+
|
74
|
+
def ensure_stopped!
|
75
|
+
if self.running?
|
76
|
+
puts "Process already running with id #{self.pid}"
|
77
|
+
exit 1
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def cleanup
|
82
|
+
begin
|
83
|
+
File.delete(@path)
|
84
|
+
rescue Errno::ENOENT
|
85
|
+
File.delete("/tmp/#{Pathname.new(@path).basename}")
|
86
|
+
end
|
87
|
+
end
|
88
|
+
alias zap cleanup
|
89
|
+
|
90
|
+
def write!
|
91
|
+
begin
|
92
|
+
File.open(@path, "w") { |f| f.puts Process.pid }
|
93
|
+
rescue Errno::ENOENT, Errno::EACCES
|
94
|
+
File.open("/tmp/#{Pathname.new(@path).basename}", "w") { |f| f.puts Process.pid }
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
data/pid.gemspec
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
Gem::Specification.new do |s|
|
3
|
+
s.name = %q{pid}
|
4
|
+
s.version = "1.0.0"
|
5
|
+
|
6
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
7
|
+
s.authors = ["debbbbie"]
|
8
|
+
s.date = %q{2015-08-12}
|
9
|
+
s.description = %q{Library for managing pidfiles.}
|
10
|
+
s.email = %q{debbbbie@163.com}
|
11
|
+
s.extra_rdoc_files = [
|
12
|
+
"LICENSE",
|
13
|
+
"README.rdoc"
|
14
|
+
]
|
15
|
+
s.files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc",
|
18
|
+
"Rakefile",
|
19
|
+
"VERSION",
|
20
|
+
"lib/core_ext.rb",
|
21
|
+
"lib/core_ext/string.rb",
|
22
|
+
"lib/pid.rb",
|
23
|
+
"pid.gemspec"
|
24
|
+
]
|
25
|
+
s.homepage = %q{http://github.com/debbbbie/pid_rb}
|
26
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
27
|
+
s.require_paths = ["lib"]
|
28
|
+
# s.rubygems_version = %q{1.3.7}
|
29
|
+
s.summary = %q{Library for managing pidfiles.}
|
30
|
+
|
31
|
+
if s.respond_to? :specification_version then
|
32
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
33
|
+
s.specification_version = 3
|
34
|
+
|
35
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
36
|
+
else
|
37
|
+
end
|
38
|
+
else
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
metadata
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- debbbbie
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-12 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Library for managing pidfiles.
|
14
|
+
email: debbbbie@163.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files:
|
18
|
+
- LICENSE
|
19
|
+
- README.rdoc
|
20
|
+
files:
|
21
|
+
- LICENSE
|
22
|
+
- README.rdoc
|
23
|
+
- Rakefile
|
24
|
+
- VERSION
|
25
|
+
- lib/core_ext.rb
|
26
|
+
- lib/core_ext/string.rb
|
27
|
+
- lib/pid.rb
|
28
|
+
- pid.gemspec
|
29
|
+
homepage: http://github.com/debbbbie/pid_rb
|
30
|
+
licenses: []
|
31
|
+
metadata: {}
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options:
|
34
|
+
- --charset=UTF-8
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
requirements: []
|
48
|
+
rubyforge_project:
|
49
|
+
rubygems_version: 2.4.5
|
50
|
+
signing_key:
|
51
|
+
specification_version: 3
|
52
|
+
summary: Library for managing pidfiles.
|
53
|
+
test_files: []
|