phantom 0.0.3.pre → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +18 -4
- data/lib/phantom/base.rb +41 -5
- data/lib/phantom/status.rb +2 -0
- data/lib/phantom/version.rb +1 -1
- data/spec/base_spec.rb +63 -5
- data/spec/phantom_spec.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1d30637c752f12133d0987930aa8be435bfae5cd
|
4
|
+
data.tar.gz: 4fb139973bbdf6c990ee41f1cf736072fa91ac56
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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
|
-
##
|
61
|
-
|
62
|
-
|
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
|
|
data/lib/phantom/base.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
module Phantom
|
2
2
|
class Base
|
3
|
-
attr_reader :pid
|
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(:
|
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
|
data/lib/phantom/status.rb
CHANGED
data/lib/phantom/version.rb
CHANGED
data/spec/base_spec.rb
CHANGED
@@ -1,14 +1,72 @@
|
|
1
1
|
require 'phantom'
|
2
2
|
|
3
3
|
describe Phantom::Base do
|
4
|
-
|
5
|
-
|
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
|
-
|
25
|
+
File.new(count_file, 'w').close
|
9
26
|
end
|
10
|
-
phantom.
|
27
|
+
phantom.terminate
|
11
28
|
sleep 5
|
12
|
-
|
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
|
data/spec/phantom_spec.rb
CHANGED
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
|
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:
|
95
|
+
version: '0'
|
96
96
|
requirements: []
|
97
97
|
rubyforge_project:
|
98
98
|
rubygems_version: 2.1.9
|