pid_lock 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 77923305bc78cafa8695276ddf0c62ceffb09a1f
4
- data.tar.gz: d15114f2cfabc6717d639dd7cb34f5286e7a51b4
2
+ SHA256:
3
+ metadata.gz: 4bdb79fac62a06bf4139aaddacee881160ad977e69e361ea7c8486769bf3a1ee
4
+ data.tar.gz: e7a8b13bef7732a94ef6816f77efe3f25483001780e476807c34565897e5acbc
5
5
  SHA512:
6
- metadata.gz: 97a1780aff8976f8f3afbfcc00fcd6ba56efae10f45be45b25f653062784506818444cb1cefb628bc0f694d58f8179044b5b9fd3b92d48fd7770acd07a7adf99
7
- data.tar.gz: dec5e2d93cacd36eba2ec45cfe4105b7eee7b06cb0e9d9e70355655786c19d1185595a6a030f7cfa210ec65811b11b42f2a1f68a21f5a80719b8172230b25c58
6
+ metadata.gz: 5f3592e81e367de369cb4cbb74a8abc0329eae88b2df32d36f3f5ab54316d5b1dadc8507ac7cf723621414d5dab1cdc7dcb6e380bccb4b20076016671ea00a8f
7
+ data.tar.gz: 2521c04322aa72042a351fd822ddfc86809713deec2c1c5581ee254337b655cb17831acc0ed89f1b2342d16bb353a420d89ebd2c57216fd45b35bad4fffa5938
@@ -3,37 +3,44 @@ require 'fileutils'
3
3
 
4
4
  module PidLock
5
5
  class << self
6
- @@pid_path = nil
7
-
8
- def pid_path=(path)
9
- @@pid_path = path
6
+ attr_writer :pid_path
7
+ def pid_path
8
+ @pid_path || "tmp/pid_locks"
10
9
  end
11
10
 
12
- def pid_path
13
- @@pid_path || "tmp/pid_locks"
11
+ def ensure_single_running(pid_name)
12
+ return if locked?(pid_name)
13
+ PidLock.lock(pid_name)
14
+ begin
15
+ yield
16
+ ensure
17
+ PidLock.unlock(pid_name)
18
+ end
14
19
  end
15
20
 
16
21
  def lock(pid_name)
17
- Dir.exists?(pid_path) or FileUtils.mkdir_p(pid_path)
18
- File.write(File.join(pid_path, "#{pid_name}.pid"), $$)
22
+ Dir.exist?(pid_path) || FileUtils.mkdir_p(pid_path)
23
+ File.write(File.join(pid_path, "#{pid_name}.pid"), $PID)
19
24
  end
20
25
 
21
26
  def locked?(pid_name)
22
- return false unless File.exists?(File.join(pid_path, "#{pid_name}.pid"))
27
+ return false unless File.exist?(File.join(pid_path, "#{pid_name}.pid"))
23
28
  begin
24
29
  Process.kill(0, pid(pid_name))
25
30
  return true
26
- rescue
31
+ rescue StandardError
27
32
  self.unlock(pid_name)
28
33
  return false
29
34
  end
30
35
  end
31
36
 
32
37
  def stop(pid_name)
33
- return unless File.exists?(File.join(pid_path, "#{pid_name}.pid"))
38
+ return unless File.exist?(File.join(pid_path, "#{pid_name}.pid"))
34
39
  pid = File.read(File.join(pid_path, "#{pid_name}.pid"))
35
- unless pid.to_s.empty?
36
- Process.kill("TERM", pid.to_i) rescue nil
40
+ return if pid.to_s.empty?
41
+ begin
42
+ Process.kill("TERM", pid.to_i)
43
+ rescue StandardError
37
44
  end
38
45
  unlock(pid_name)
39
46
  end
@@ -43,7 +50,7 @@ module PidLock
43
50
  end
44
51
 
45
52
  def unlock(pid_name)
46
- if File.exists?(File.join(pid_path, "#{pid_name}.pid"))
53
+ if File.exist?(File.join(pid_path, "#{pid_name}.pid"))
47
54
  File.delete(File.join(pid_path, "#{pid_name}.pid"))
48
55
  end
49
56
  end
@@ -1,3 +1,3 @@
1
1
  module PidLock
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_development_dependency "bundler", "~> 1.6"
22
- spec.add_development_dependency "rake", "~> 10.4"
21
+ spec.add_development_dependency "bundler", "~> 2.1"
22
+ spec.add_development_dependency "rake", ">= 12.3.3"
23
23
  spec.add_development_dependency "test-unit", "~> 2.5"
24
24
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pid_lock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - xuxiangyang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-21 00:00:00.000000000 Z
11
+ date: 2020-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,28 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.6'
19
+ version: '2.1'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.6'
26
+ version: '2.1'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '10.4'
33
+ version: 12.3.3
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '10.4'
40
+ version: 12.3.3
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: test-unit
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -87,8 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
87
  - !ruby/object:Gem::Version
88
88
  version: '0'
89
89
  requirements: []
90
- rubyforge_project:
91
- rubygems_version: 2.4.8
90
+ rubygems_version: 3.0.8
92
91
  signing_key:
93
92
  specification_version: 4
94
93
  summary: ruby pid lock file