clicker 0.1.3 → 0.1.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6945c99ea0c8744bad04db4a506b1461e0ab34b7
4
- data.tar.gz: fc07e55227a5f9f865e173594d963ec8df8c7719
3
+ metadata.gz: cb7b0b66b2bf146b1b763b91f765bea5678537a3
4
+ data.tar.gz: a5f69f12bd7e07c0d3c6036b26e67f81cd6e9555
5
5
  SHA512:
6
- metadata.gz: 14e7d7745abe1aea2752a5258cb2f2ff326c8b84ee597eb5cd8017b1ba694cc3a7ee1ee038e1bf1b924ea9a1fca01c4860e57f9c60822945a88107e748b321a5
7
- data.tar.gz: 84c1358571a8483ec92503cc68a7901d2eb5f10004273bc001ffa486c08862f935b2695d7ab2e6358f51ed84011e45085f3807a88e59e83b33c247ca266890f6
6
+ metadata.gz: efaeaf6b461a72879356cba31f016ef75fb2f56fbc11dd61cd5dbd3664642385269e7da09950b6aed7f51cf7bed34d474cc5dc697fb299daf8ef6f775bb5daec
7
+ data.tar.gz: 00c984fa279a3df5c0be6c68fed7adb6b09bda147eafa754ad0ee00732828611ea3e9fad0881d83fb5e68aff45f1ee77dc6a554d6d278634a00f479bfda985e9
@@ -12,28 +12,45 @@ class LaunchLock
12
12
  end
13
13
 
14
14
  def try_lock
15
- File.open(@lock_file_path, 'w') do |f|
16
- f.puts Process.pid
15
+ File.open(@lock_file_path, 'r+') do |f|
16
+ success = f.flock(File::LOCK_EX | File::LOCK_NB)
17
+ if success
18
+ f.puts Process.pid
19
+ f.flush
20
+ f.truncate(f.pos)
21
+ yield
22
+ return true
23
+ else
24
+ return false
25
+ end
17
26
  end
18
- yield
19
- return true
20
- rescue
21
- return false
22
- ensure
23
- FileUtils.rm(@lock_file_path)
24
27
  end
25
28
 
26
29
  def locked?
27
- File.exist?(@lock_file_path)
30
+ File.open(@lock_file_path, 'r') do |f|
31
+ rv = f.flock(File::LOCK_EX | File::LOCK_NB)
32
+ case rv
33
+ when 0
34
+ return false
35
+ when false
36
+ return true
37
+ end
38
+ end
39
+ rescue Errno::ENOENT
40
+ return false
28
41
  end
29
42
 
30
43
  def owner
31
- pid = nil
32
44
  File.open(@lock_file_path) do |f|
33
- pid = f.gets.to_i
45
+ success = f.flock(File::LOCK_EX | File::LOCK_NB)
46
+ if success
47
+ return nil # owner is not alive...
48
+ else
49
+ pid = f.gets.to_i
50
+ return pid
51
+ end
34
52
  end
35
- return pid
36
- rescue
53
+ rescue Errno::ENOENT
37
54
  return nil
38
55
  end
39
56
  end
@@ -30,26 +30,21 @@ class Program
30
30
 
31
31
  unless success
32
32
  STDERR.puts("Error: another instance of clicker is running. (PID #{@lock.owner})")
33
- STDERR.puts("the lock file is at #{@lock.lock_file_path}.")
34
33
  exit 1
35
34
  end
36
35
  when :start
37
- if !@lock.locked?
36
+ if @lock.locked?
37
+ STDERR.puts("Error: another instance of clicker is running. (PID #{@lock.owner})")
38
+ exit 1
39
+ else
38
40
  if fork == nil
39
41
  if fork == nil
40
- success = @lock.try_lock do
42
+ @lock.try_lock do
41
43
  do_run
42
44
  end
43
- unless success
44
- # ここでは端末から切り離されているので出来ることはない。
45
- exit 1
46
- end
45
+ # ここでは端末から切り離されているのでロックに失敗しても出来ることはない。
47
46
  end
48
47
  end
49
- else
50
- STDERR.puts("Error: another instance of clicker seems to be running. (PID #{@lock.owner})")
51
- STDERR.puts("the lock file is at #{@lock.lock_file_path}.")
52
- exit 1
53
48
  end
54
49
  when :stop
55
50
  if @lock.locked?
@@ -59,8 +54,6 @@ class Program
59
54
  STDERR.puts("SIGTERM has been sent to PID #{owner}.")
60
55
  rescue Errno::ESRCH
61
56
  STDERR.puts("Warning: Process (#{owner}) not found.")
62
- STDERR.puts("Deleting the lock file.")
63
- FileUtils.rm(@lock.lock_file_path)
64
57
  end
65
58
  else
66
59
  STDERR.puts("no instance is running.")
@@ -1,3 +1,3 @@
1
1
  module Clicker
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clicker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yoteichi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-06-30 00:00:00.000000000 Z
11
+ date: 2016-07-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubysdl