phantom 0.0.3.pre → 0.0.3

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: ccff1ad812d5dcc3fc8826446d1dc42de2c87ec5
4
- data.tar.gz: 245a70cba1b1e2310f6e440d184acadc328405e0
3
+ metadata.gz: 1d30637c752f12133d0987930aa8be435bfae5cd
4
+ data.tar.gz: 4fb139973bbdf6c990ee41f1cf736072fa91ac56
5
5
  SHA512:
6
- metadata.gz: 24e129fd455242d38337b962c71bd3bc603cfd101b762f9f83a6f2d313fe14d342ca4c27d849c06c1697803efcd9b59bcb0be97b4c10f7133e87aa8de2758669
7
- data.tar.gz: 99a371f657d7723f456fef096314d2a2d8ba6cae984e9b702902c807194e5ffd805768c1c3138aad96d1d879e993dd1215c6b9d1ffb11e49882b2e9869599466
6
+ metadata.gz: dc63afdd0131f85c9369ba4e5d68cee2609f8e7a3bb1d3e6297dba98acabae0d279cf22c94ae8c3aaeb2a28f533c8c8f6c8a6a20c71a71de28511fb512dde51c
7
+ data.tar.gz: 0749b36d7bf51dd79aaece8b255cd618a2aa86801c4e38af5d9247648656f40e3a50006c1f6069ba8b09d09142c012695a224e83cccd8824e3be3fe06f2fe236
data/README.md CHANGED
@@ -36,7 +36,21 @@ rescue Phantom::ForkError => e
36
36
  puts e.message #=> error message
37
37
  end
38
38
 
39
- phantom.pid #=> PID or nil if fork fails
39
+ phantom.pid #=> PID or nil if fork fails
40
+ phantom.status #=> 'Alive' | 'Dead' | 'Paused'
41
+ phantom.dead? #=> true if the sub process is dead (i.e. either ended normally or killed)
42
+ phantom.alive? #=> true if not dead.
43
+
44
+ phantom.stop #=> pause the sub process
45
+ phantom.pause #=> pause is an alias of stop
46
+ phantom.continue #=> resume the sub process
47
+ phantom.resume #=> resume is an alias of continue
48
+
49
+ phantom.kill(1) #=> send signal to the sub process
50
+ phantom.kill(:TERM) #=> you can as well use the POSIX signal names
51
+
52
+ phantom.terminate #=> terminate the sub process gracefully
53
+ phantom.abort! #=> force the sub process to end immediately
40
54
  ```
41
55
 
42
56
  The `on_ok` parameter can be any instances that respond to `call` taking no arguments.
@@ -57,9 +71,9 @@ If you want a global mutex (i.e. mutex between multiple processes), you can use
57
71
  end
58
72
  ```
59
73
 
60
- ## TODO
61
-
62
- Implement monitoring and intercepting mechanism
74
+ ## Caution
75
+ 1. The status is NOT completely retrieved from the sub process, so any manipulation out of the phantom instance,
76
+ except terminating the sub process, will not be recognized.
63
77
 
64
78
  ## Contributing
65
79
 
@@ -1,9 +1,9 @@
1
1
  module Phantom
2
2
  class Base
3
- attr_reader :pid, :status
3
+ attr_reader :pid
4
4
 
5
5
  def initialize(pid)
6
- @pid = pid
6
+ @pid = pid.nil? ? nil : pid.to_i
7
7
  end
8
8
 
9
9
  def kill(signal)
@@ -30,9 +30,45 @@ module Phantom
30
30
  @process_priority ||= Process.getpriority(Process::PRIO_PROCESS, @pid)
31
31
  end
32
32
 
33
- def abort
34
- kill(:TERM)
33
+ def abort!
34
+ kill(:ABRT) if alive?
35
+ end
36
+
37
+ def terminate
38
+ kill(:TERM) if alive?
39
+ end
40
+
41
+ def stop
42
+ if alive?
43
+ kill(:STOP)
44
+ @status = Phantom::Status::PAUSED
45
+ end
46
+ end
47
+ alias :pause :stop
48
+
49
+ def continue
50
+ if alive?
51
+ kill(:CONT)
52
+ @status = Phantom::Status::ALIVE
53
+ end
54
+ end
55
+ alias :resume :continue
56
+
57
+ def status
58
+ begin
59
+ Process.kill(0, @pid)
60
+ return @status ||= Phantom::Status::ALIVE
61
+ rescue Errno::ESRCH
62
+ return @status = Phantom::Status::DEAD
63
+ end
64
+ end
65
+
66
+ def alive?
67
+ !dead?
68
+ end
69
+
70
+ def dead?
71
+ status == Phantom::Status::DEAD
35
72
  end
36
-
37
73
  end
38
74
  end
@@ -4,5 +4,7 @@ module Phantom
4
4
  ABORTED = 'Aborted'
5
5
  ERROR = 'Error'
6
6
  PAUSED = 'Paused'
7
+ ALIVE = 'Alive'
8
+ DEAD = 'Dead'
7
9
  end
8
10
  end
@@ -1,3 +1,3 @@
1
1
  module Phantom
2
- VERSION = "0.0.3.pre"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -1,14 +1,72 @@
1
1
  require 'phantom'
2
2
 
3
3
  describe Phantom::Base do
4
- it 'should be kill the process when called on abort' do
5
- i = 0
4
+
5
+ count_file = 'tmp/count.txt'
6
+
7
+ after(:each) do
8
+ File.delete count_file if File.exists? count_file
9
+ end
10
+
11
+ it 'should kill the process when called on abort' do
12
+ phantom = Phantom.run do
13
+ sleep 3
14
+ File.new(count_file, 'w').close
15
+ end
16
+ phantom.abort!
17
+ sleep 5
18
+ File.should_not exist(count_file)
19
+ phantom.status.should == Phantom::Status::DEAD
20
+ end
21
+
22
+ it 'should be kill the process when called on terminate' do
6
23
  phantom = Phantom.run do
7
24
  sleep 3
8
- i = 1
25
+ File.new(count_file, 'w').close
9
26
  end
10
- phantom.abort
27
+ phantom.terminate
11
28
  sleep 5
12
- i.should == 0
29
+ File.should_not exist(count_file)
30
+ phantom.status.should == Phantom::Status::DEAD
31
+ end
32
+
33
+ it 'should be able to pause and to resume the process' do
34
+ phantom = Phantom.run do
35
+ loop do
36
+ File.open(count_file, 'a') {|f| f.write 'P'}
37
+ end
38
+ sleep 0.5
39
+ end
40
+ sleep 3
41
+ phantom.pause
42
+ current = File.read(count_file)
43
+ sleep 3
44
+ File.read(count_file).should == current
45
+ phantom.status.should == Phantom::Status::PAUSED
46
+
47
+ phantom.resume
48
+ sleep 3
49
+ phantom.status.should == Phantom::Status::ALIVE
50
+
51
+ phantom.terminate
52
+ sleep 3
53
+ File.read(count_file).length.should > current.length
54
+ end
55
+
56
+ it 'should be able to identify if the process is alive' do
57
+ phantom = Phantom.run do
58
+ @i += 1
59
+ sleep 0.5
60
+ end
61
+ phantom.should be_alive
62
+ phantom.status.should == Phantom::Status::ALIVE
63
+
64
+ phantom.terminate
65
+
66
+ sleep 0.5
67
+
68
+ phantom.should be_dead
69
+ phantom.status.should == Phantom::Status::DEAD
13
70
  end
71
+
14
72
  end
@@ -3,7 +3,7 @@ require 'phantom'
3
3
  describe Phantom do
4
4
  pid_file = 'tmp/phantom.pid'
5
5
 
6
- after do
6
+ after(:each) do
7
7
  File.delete pid_file if File.exist? pid_file
8
8
  end
9
9
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phantom
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3.pre
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aetherus
@@ -90,9 +90,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
90
90
  version: '0'
91
91
  required_rubygems_version: !ruby/object:Gem::Requirement
92
92
  requirements:
93
- - - '>'
93
+ - - '>='
94
94
  - !ruby/object:Gem::Version
95
- version: 1.3.1
95
+ version: '0'
96
96
  requirements: []
97
97
  rubyforge_project:
98
98
  rubygems_version: 2.1.9