phantom 0.0.4 → 0.1.0

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: d23e54afd7936bddad11560c655724526ade837c
4
- data.tar.gz: 596f32e098dae1b9dc5f2fa386f8887a91257784
3
+ metadata.gz: 3843d810f05c3f9159e41ff4f91bdc64a88efd7d
4
+ data.tar.gz: 688af5a139288b798118b40ae342dca6e6aa1f5a
5
5
  SHA512:
6
- metadata.gz: 005197660c0b984076ece07a3b4176e2f2ca028f2d923ba316025b1a09048b93a6eac1f857c93704eea0d05136ffe8b60aee316271a92390dc8d72771b299a51
7
- data.tar.gz: 260787a370717231b31ec7d1c113e63eed032038ad03aa556459eaeab43277afaeba6abd97ccfd5671a23a2da3379e55695567ed85235f7d86ab97a823f89505
6
+ metadata.gz: 062578b6840e9f76f90cdafe086bdf9c03b3656e233b6a95a10d35021c4209b5f69c072fd32bb09a6414ba9fc6315c50fa48a03c3ea66e616ffcf8d4494ca21d
7
+ data.tar.gz: c9c3d8563f204aed151bdf3e3d79fc9204da82318b6d889e1b122fabeeca22c5f5fe1a854af9be7d28f31f0136a78094bb54249bd92aefea049a6bcf3bc1041b
@@ -17,15 +17,18 @@ module Phantom
17
17
  at_exit do
18
18
  o.flush
19
19
  o.close
20
- File.delete pid_file if pid_file
20
+ if pid_file and File.exists? pid_file
21
+ File.delete pid_file if Process.pid == File.read(pid_file).to_i
22
+ end
21
23
  end
22
24
 
23
25
  i.close
24
26
  begin
25
27
  block.call if block_given?
26
28
  Marshal.dump(Status::OK, o)
27
- rescue StandardError => e
28
- Marshal.dump(e, o)
29
+ rescue Exception => e
30
+ status = e.is_a?(SignalException) ? Status::OK : e
31
+ Marshal.dump(status, o)
29
32
  end
30
33
  end
31
34
 
@@ -1,3 +1,3 @@
1
1
  module Phantom
2
- VERSION = "0.0.4"
2
+ VERSION = '0.1.0'
3
3
  end
@@ -6,6 +6,7 @@ describe Phantom::Base do
6
6
 
7
7
  after(:each) do
8
8
  File.delete count_file if File.exists? count_file
9
+ @error = nil
9
10
  end
10
11
 
11
12
  it 'should kill the process when called on abort' do
@@ -19,8 +20,8 @@ describe Phantom::Base do
19
20
  phantom.status.should == Phantom::Status::DEAD
20
21
  end
21
22
 
22
- it 'should be kill the process when called on terminate' do
23
- phantom = Phantom.run do
23
+ it 'should kill the process when called on terminate' do
24
+ phantom = Phantom.run(on_error: lambda{|e| @error = e}) do
24
25
  sleep 3
25
26
  File.new(count_file, 'w').close
26
27
  end
@@ -28,6 +29,7 @@ describe Phantom::Base do
28
29
  sleep 5
29
30
  File.should_not exist(count_file)
30
31
  phantom.status.should == Phantom::Status::DEAD
32
+ @error.should be_nil
31
33
  end
32
34
 
33
35
  it 'should be able to pause and to resume the process' do
@@ -2,9 +2,13 @@ require 'phantom'
2
2
 
3
3
  describe Phantom do
4
4
  pid_file = 'tmp/phantom.pid'
5
+ sub_pid_file = 'tmp/sub.pid'
5
6
 
6
7
  after(:each) do
7
- File.delete pid_file if File.exist? pid_file
8
+ [pid_file, sub_pid_file].each do |file|
9
+ File.delete file if File.exist? file
10
+ end
11
+ @error = nil
8
12
  end
9
13
 
10
14
  it 'should remove the pid file when normally ends.' do
@@ -26,26 +30,23 @@ describe Phantom do
26
30
 
27
31
  it 'should call on_ok when sub processed normally ends' do
28
32
  i = 0
29
- err = nil
30
- Phantom.run(pid_file: pid_file, on_ok: lambda{i += 1}, on_error: lambda{|e| err = e}) do
33
+ Phantom.run(pid_file: pid_file, on_ok: lambda{i += 1}, on_error: lambda{|e| @error = e}) do
31
34
  end
32
35
  sleep 3
33
36
  i.should == 1
34
- err.should == nil
37
+ @error.should be_nil
35
38
  end
36
39
 
37
40
  it 'should call on_error when sub process raises an unhandled error' do
38
- err = nil
39
41
  i = 0
40
- Phantom.run(pid_file: pid_file, on_ok: lambda{i += 1}, on_error: lambda{|e| err = e}) do
41
- raise 'Wa ha ha!'
42
+ Phantom.run(pid_file: pid_file, on_ok: lambda{i += 1}, on_error: lambda{|e| @error = e}) do
43
+ raise SyntaxError
42
44
  i += 1
43
45
  end
44
46
 
45
47
  sleep 3
46
48
 
47
- err.should be_an(StandardError)
48
- err.message.should == 'Wa ha ha!'
49
+ @error.should be_a(SyntaxError)
49
50
  i.should == 0
50
51
  end
51
52
 
@@ -55,11 +56,31 @@ describe Phantom do
55
56
 
56
57
  it 'should return a Phantom::Base instance when run' do
57
58
  phantom = Phantom.run do end
58
- phantom.should be_an(Phantom::Base)
59
+ phantom.should be_a(Phantom::Base)
59
60
  end
60
61
 
61
62
  it 'should do nothing if block is not given' do
62
63
  phantom = Phantom.run
63
64
  phantom.pid.should be_nil
64
65
  end
66
+
67
+ it 'should not call on_error if sub process calls exit' do
68
+ Phantom.run(on_error: lambda{|e| @error = e}) do
69
+ exit
70
+ end
71
+ @error.should be_nil
72
+ end
73
+
74
+ it 'should be able to fork sub-sub processes' do
75
+ Phantom.run(pid_file: pid_file, on_error: lambda{|e| @error = e}) do
76
+ Phantom.run(pid_file: sub_pid_file, on_error: lambda{|e| raise e}) do end
77
+ sleep 5
78
+ end
79
+ @error.should be_nil
80
+ sleep 1
81
+ File.should exist(pid_file)
82
+ File.should_not exist(sub_pid_file)
83
+ sleep 5
84
+ File.should_not exist(pid_file)
85
+ end
65
86
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phantom
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aetherus
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-26 00:00:00.000000000 Z
11
+ date: 2013-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler