phantom 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.idea/encodings.xml +5 -0
- data/.idea/misc.xml +5 -0
- data/.idea/modules.xml +9 -0
- data/.idea/phantom.iml +136 -0
- data/.idea/scopes/scope_settings.xml +5 -0
- data/.idea/vcs.xml +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +60 -0
- data/Rakefile +1 -0
- data/lib/phantom.rb +8 -0
- data/lib/phantom/base.rb +38 -0
- data/lib/phantom/class_methods.rb +56 -0
- data/lib/phantom/fork_error.rb +11 -0
- data/lib/phantom/status.rb +8 -0
- data/lib/phantom/version.rb +3 -0
- data/phantom.gemspec +24 -0
- data/spec/base_spec.rb +14 -0
- data/spec/phantom_spec.rb +60 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e5395b53c7f1561a2687b7df2cbafff5ee34c5e1
|
4
|
+
data.tar.gz: 74be7c0eb42b6e2fbc896677b8a1d514d9e582ee
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c371d61bdcd53237401c978780fdd98250ed368a09b3db89e91943af4cc700beb3d8d00c9b06bfce608fcf866af220fc2ce824067d22285c6f2410f24601456a
|
7
|
+
data.tar.gz: 914f9d6314cad126cc03e37dcee6872f986bec63d621f03b9ed04a2b21db0b3f28a07e50921e893f72c0a5e96b180d60a9cd4e156f123dbfb2d7d29dae614555
|
data/.gitignore
ADDED
data/.idea/encodings.xml
ADDED
data/.idea/misc.xml
ADDED
data/.idea/modules.xml
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<project version="4">
|
3
|
+
<component name="ProjectModuleManager">
|
4
|
+
<modules>
|
5
|
+
<module fileurl="file://$PROJECT_DIR$/.idea/phantom.iml" filepath="$PROJECT_DIR$/.idea/phantom.iml" />
|
6
|
+
</modules>
|
7
|
+
</component>
|
8
|
+
</project>
|
9
|
+
|
data/.idea/phantom.iml
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<module type="RUBY_MODULE" version="4">
|
3
|
+
<component name="FacetManager">
|
4
|
+
<facet type="gem" name="Gem">
|
5
|
+
<configuration>
|
6
|
+
<option name="GEM_APP_ROOT_PATH" value="$MODULE_DIR$" />
|
7
|
+
<option name="GEM_APP_TEST_PATH" value="$MODULE_DIR$/test" />
|
8
|
+
<option name="GEM_APP_LIB_PATH" value="$MODULE_DIR$/lib" />
|
9
|
+
</configuration>
|
10
|
+
</facet>
|
11
|
+
</component>
|
12
|
+
<component name="ModuleRunConfigurationManager">
|
13
|
+
<configuration default="false" name="Phantom: phantom" type="RSpecRunConfigurationType" factoryName="RSpec" temporary="true">
|
14
|
+
<predefined_log_file id="RUBY_RSPEC" enabled="true" />
|
15
|
+
<module name="phantom" />
|
16
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="RUBY_ARGS" VALUE="-e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift)" />
|
17
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="WORK DIR" VALUE="$MODULE_DIR$" />
|
18
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="SHOULD_USE_SDK" VALUE="false" />
|
19
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="ALTERN_SDK_NAME" VALUE="" />
|
20
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="myPassParentEnvs" VALUE="true" />
|
21
|
+
<envs>
|
22
|
+
<env name="JRUBY_OPTS" value="-X+O" />
|
23
|
+
</envs>
|
24
|
+
<EXTENSION ID="BundlerRunConfigurationExtension" bundleExecEnabled="false" />
|
25
|
+
<EXTENSION ID="JRubyRunConfigurationExtension" NailgunExecEnabled="false" />
|
26
|
+
<EXTENSION ID="RubyCoverageRunConfigurationExtension" enabled="false" sample_coverage="true" track_test_folders="true" runner="rcov">
|
27
|
+
<COVERAGE_PATTERN ENABLED="true">
|
28
|
+
<PATTERN REGEXPS="/.rvm/" INCLUDED="false" />
|
29
|
+
</COVERAGE_PATTERN>
|
30
|
+
</EXTENSION>
|
31
|
+
<EXTENSION ID="org.jetbrains.plugins.ruby.motion.run.MotionSimulatorRunExtension" />
|
32
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="TESTS_FOLDER_PATH" VALUE="" />
|
33
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="TEST_SCRIPT_PATH" VALUE="$MODULE_DIR$/spec/phantom_spec.rb" />
|
34
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="SPEC_RUNNER_PATH" VALUE="" />
|
35
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="TEST_FILE_MASK" VALUE="**/*_spec.rb" />
|
36
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="SPEC_EXAMPLE_NAME" VALUE="Phantom" />
|
37
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="TEST_TEST_TYPE" VALUE="TEST_SCRIPT" />
|
38
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="SPEC_ARGS" VALUE="" />
|
39
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="RUNNER_VERSION" VALUE="" />
|
40
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="USE_CUSTOM_SPEC_RUNNER" VALUE="false" />
|
41
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="DRB" VALUE="false" />
|
42
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="ZEUS" VALUE="false" />
|
43
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="FULL_BACKTRACE" VALUE="false" />
|
44
|
+
<RunnerSettings RunnerId="RubyRunner" />
|
45
|
+
<ConfigurationWrapper RunnerId="RubyRunner" />
|
46
|
+
<method />
|
47
|
+
</configuration>
|
48
|
+
<configuration default="false" name="phantom_spec: phantom" type="RSpecRunConfigurationType" factoryName="RSpec" temporary="true">
|
49
|
+
<predefined_log_file id="RUBY_RSPEC" enabled="true" />
|
50
|
+
<module name="phantom" />
|
51
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="RUBY_ARGS" VALUE="-e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift)" />
|
52
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="WORK DIR" VALUE="$MODULE_DIR$" />
|
53
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="SHOULD_USE_SDK" VALUE="false" />
|
54
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="ALTERN_SDK_NAME" VALUE="" />
|
55
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="myPassParentEnvs" VALUE="true" />
|
56
|
+
<envs>
|
57
|
+
<env name="JRUBY_OPTS" value="-X+O" />
|
58
|
+
</envs>
|
59
|
+
<EXTENSION ID="BundlerRunConfigurationExtension" bundleExecEnabled="false" />
|
60
|
+
<EXTENSION ID="JRubyRunConfigurationExtension" NailgunExecEnabled="false" />
|
61
|
+
<EXTENSION ID="RubyCoverageRunConfigurationExtension" enabled="false" sample_coverage="true" track_test_folders="true" runner="rcov">
|
62
|
+
<COVERAGE_PATTERN ENABLED="true">
|
63
|
+
<PATTERN REGEXPS="/.rvm/" INCLUDED="false" />
|
64
|
+
</COVERAGE_PATTERN>
|
65
|
+
</EXTENSION>
|
66
|
+
<EXTENSION ID="org.jetbrains.plugins.ruby.motion.run.MotionSimulatorRunExtension" />
|
67
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="TESTS_FOLDER_PATH" VALUE="" />
|
68
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="TEST_SCRIPT_PATH" VALUE="$MODULE_DIR$/spec/phantom_spec.rb" />
|
69
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="SPEC_RUNNER_PATH" VALUE="" />
|
70
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="TEST_FILE_MASK" VALUE="**/*_spec.rb" />
|
71
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="SPEC_EXAMPLE_NAME" VALUE="" />
|
72
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="TEST_TEST_TYPE" VALUE="TEST_SCRIPT" />
|
73
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="SPEC_ARGS" VALUE="" />
|
74
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="RUNNER_VERSION" VALUE="" />
|
75
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="USE_CUSTOM_SPEC_RUNNER" VALUE="false" />
|
76
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="DRB" VALUE="false" />
|
77
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="ZEUS" VALUE="false" />
|
78
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="FULL_BACKTRACE" VALUE="false" />
|
79
|
+
<RunnerSettings RunnerId="RubyCover" />
|
80
|
+
<RunnerSettings RunnerId="RubyRunner" />
|
81
|
+
<ConfigurationWrapper RunnerId="RubyCover" />
|
82
|
+
<ConfigurationWrapper RunnerId="RubyRunner" />
|
83
|
+
<method />
|
84
|
+
</configuration>
|
85
|
+
<configuration default="false" name="All specs in spec: phantom" type="RSpecRunConfigurationType" factoryName="RSpec" temporary="true">
|
86
|
+
<predefined_log_file id="RUBY_RSPEC" enabled="true" />
|
87
|
+
<module name="phantom" />
|
88
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="RUBY_ARGS" VALUE="-e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift)" />
|
89
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="WORK DIR" VALUE="$MODULE_DIR$" />
|
90
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="SHOULD_USE_SDK" VALUE="false" />
|
91
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="ALTERN_SDK_NAME" VALUE="" />
|
92
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="myPassParentEnvs" VALUE="true" />
|
93
|
+
<envs>
|
94
|
+
<env name="JRUBY_OPTS" value="-X+O" />
|
95
|
+
</envs>
|
96
|
+
<EXTENSION ID="BundlerRunConfigurationExtension" bundleExecEnabled="false" />
|
97
|
+
<EXTENSION ID="JRubyRunConfigurationExtension" NailgunExecEnabled="false" />
|
98
|
+
<EXTENSION ID="RubyCoverageRunConfigurationExtension" enabled="false" sample_coverage="true" track_test_folders="true" runner="rcov">
|
99
|
+
<COVERAGE_PATTERN ENABLED="true">
|
100
|
+
<PATTERN REGEXPS="/.rvm/" INCLUDED="false" />
|
101
|
+
</COVERAGE_PATTERN>
|
102
|
+
</EXTENSION>
|
103
|
+
<EXTENSION ID="org.jetbrains.plugins.ruby.motion.run.MotionSimulatorRunExtension" />
|
104
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="TESTS_FOLDER_PATH" VALUE="$MODULE_DIR$/spec" />
|
105
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="TEST_SCRIPT_PATH" VALUE="" />
|
106
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="SPEC_RUNNER_PATH" VALUE="" />
|
107
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="TEST_FILE_MASK" VALUE="**/*_spec.rb" />
|
108
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="SPEC_EXAMPLE_NAME" VALUE="" />
|
109
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="TEST_TEST_TYPE" VALUE="ALL_IN_FOLDER" />
|
110
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="SPEC_ARGS" VALUE="" />
|
111
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="RUNNER_VERSION" VALUE="" />
|
112
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="USE_CUSTOM_SPEC_RUNNER" VALUE="false" />
|
113
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="DRB" VALUE="false" />
|
114
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="ZEUS" VALUE="false" />
|
115
|
+
<RSPEC_RUN_CONFIG_SETTINGS_ID NAME="FULL_BACKTRACE" VALUE="false" />
|
116
|
+
<RunnerSettings RunnerId="RubyRunner" />
|
117
|
+
<ConfigurationWrapper RunnerId="RubyRunner" />
|
118
|
+
<method />
|
119
|
+
</configuration>
|
120
|
+
</component>
|
121
|
+
<component name="NewModuleRootManager">
|
122
|
+
<content url="file://$MODULE_DIR$">
|
123
|
+
<sourceFolder url="file://$MODULE_DIR$/spec" isTestSource="true" />
|
124
|
+
</content>
|
125
|
+
<orderEntry type="inheritedJdk" />
|
126
|
+
<orderEntry type="sourceFolder" forTests="false" />
|
127
|
+
<orderEntry type="library" scope="PROVIDED" name="bundler (v1.3.5, RVM: ruby-2.0.0-p247) [gem]" level="application" />
|
128
|
+
<orderEntry type="library" scope="PROVIDED" name="diff-lcs (v1.2.4, RVM: ruby-2.0.0-p247) [gem]" level="application" />
|
129
|
+
<orderEntry type="library" scope="PROVIDED" name="rake (v10.1.0, RVM: ruby-2.0.0-p247) [gem]" level="application" />
|
130
|
+
<orderEntry type="library" scope="PROVIDED" name="rspec (v2.14.1, RVM: ruby-2.0.0-p247) [gem]" level="application" />
|
131
|
+
<orderEntry type="library" scope="PROVIDED" name="rspec-core (v2.14.7, RVM: ruby-2.0.0-p247) [gem]" level="application" />
|
132
|
+
<orderEntry type="library" scope="PROVIDED" name="rspec-expectations (v2.14.4, RVM: ruby-2.0.0-p247) [gem]" level="application" />
|
133
|
+
<orderEntry type="library" scope="PROVIDED" name="rspec-mocks (v2.14.4, RVM: ruby-2.0.0-p247) [gem]" level="application" />
|
134
|
+
</component>
|
135
|
+
</module>
|
136
|
+
|
data/.idea/vcs.xml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 zhoumh
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# Phantom
|
2
|
+
|
3
|
+
This gem is made to implement the callback mechanism to sub processes.
|
4
|
+
|
5
|
+
## Requirements
|
6
|
+
|
7
|
+
Ruby >= 2.0.0
|
8
|
+
|
9
|
+
This gem uses `::fork` and therefore is not compatible with JRuby.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'phantom'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install phantom
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
begin
|
29
|
+
phantom = Phantom.run(pid_file: '/path/to/your.pid',
|
30
|
+
on_ok: method(:my_ok_callback),
|
31
|
+
on_error: method(:my_error_callback)) do
|
32
|
+
# Do your background job here
|
33
|
+
end
|
34
|
+
rescue Phantom::ForkError => e
|
35
|
+
puts e.pid_file #=> pid file path
|
36
|
+
puts e.message #=> error message
|
37
|
+
end
|
38
|
+
|
39
|
+
phantom.pid #=> PID or nil if fork fails
|
40
|
+
```
|
41
|
+
|
42
|
+
The `on_ok` parameter can be any instances that respond to `call` taking no arguments.
|
43
|
+
|
44
|
+
The `on_error` parameter can be any instances that respond to `call` taking 1 argument, the exception.
|
45
|
+
|
46
|
+
None of the parameters is required.
|
47
|
+
|
48
|
+
If `pid_file` is given, `Phantom.run` first check the existence of the file, if exits, raises a `Phantom::ForkError`.
|
49
|
+
|
50
|
+
## TODO
|
51
|
+
|
52
|
+
Implement monitoring and intercepting mechanism
|
53
|
+
|
54
|
+
## Contributing
|
55
|
+
|
56
|
+
1. Fork it
|
57
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
58
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
59
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
60
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/phantom.rb
ADDED
data/lib/phantom/base.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
module Phantom
|
2
|
+
class Base
|
3
|
+
attr_reader :pid, :status
|
4
|
+
|
5
|
+
def initialize(pid)
|
6
|
+
@pid = pid
|
7
|
+
end
|
8
|
+
|
9
|
+
def kill(signal)
|
10
|
+
Process.kill(signal, @pid) if @pid
|
11
|
+
end
|
12
|
+
|
13
|
+
def sid
|
14
|
+
@sid ||= Process.getsid(@pid)
|
15
|
+
end
|
16
|
+
|
17
|
+
def gid
|
18
|
+
@gid ||= Process.getpgid(@pid)
|
19
|
+
end
|
20
|
+
|
21
|
+
def user_priority
|
22
|
+
@user_priority ||= Process.getpriority(Process::PRIO_USER, @pid)
|
23
|
+
end
|
24
|
+
|
25
|
+
def group_priority
|
26
|
+
@group_priority ||= Process.getpriority(Process::PRIO_PGRP, @pid)
|
27
|
+
end
|
28
|
+
|
29
|
+
def process_priority
|
30
|
+
@process_priority ||= Process.getpriority(Process::PRIO_PROCESS, @pid)
|
31
|
+
end
|
32
|
+
|
33
|
+
def abort
|
34
|
+
kill(:TERM)
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module Phantom
|
2
|
+
|
3
|
+
class << self
|
4
|
+
def run(pid_file: nil, on_ok: nil, on_error: nil, &block)
|
5
|
+
return Phantom::Base.new(nil) unless block_given?
|
6
|
+
|
7
|
+
raise ForkError.new('Running process exists.', pid_file) if pid_file and File.exist?(pid_file)
|
8
|
+
|
9
|
+
i, o = IO.pipe
|
10
|
+
f = File.new(pid_file, 'w') if pid_file
|
11
|
+
|
12
|
+
pid = fork do
|
13
|
+
at_exit do
|
14
|
+
o.flush
|
15
|
+
o.close
|
16
|
+
File.delete pid_file if pid_file
|
17
|
+
end
|
18
|
+
|
19
|
+
trap(:TERM) do
|
20
|
+
Process.abort
|
21
|
+
end
|
22
|
+
|
23
|
+
i.close
|
24
|
+
begin
|
25
|
+
block.call if block_given?
|
26
|
+
Marshal.dump(Status::OK, o)
|
27
|
+
rescue StandardError => e
|
28
|
+
Marshal.dump(e, o)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
Process.detach(pid)
|
33
|
+
f.write(pid.to_s) if pid_file
|
34
|
+
f.close if pid_file
|
35
|
+
o.close
|
36
|
+
|
37
|
+
Thread.abort_on_exception = true
|
38
|
+
Thread.new do
|
39
|
+
begin
|
40
|
+
status = Marshal.load(i)
|
41
|
+
if status == Status::OK then
|
42
|
+
on_ok.call if on_ok
|
43
|
+
else
|
44
|
+
on_error.call(status) if on_error
|
45
|
+
end
|
46
|
+
rescue Errno::EPIPE, EOFError => e
|
47
|
+
on_error.call(e) if on_error
|
48
|
+
ensure
|
49
|
+
i.close
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
return Phantom::Base.new(pid)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/phantom.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'phantom/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "phantom"
|
8
|
+
spec.version = Phantom::VERSION
|
9
|
+
spec.authors = ["Aetherus"]
|
10
|
+
spec.email = ["sasabune0122@gmail.com"]
|
11
|
+
spec.description = %q{This gem is developed to implement callback mechanism to sub processes.}
|
12
|
+
spec.summary = %q{This gem is developed to implement callback mechanism to sub processes. Not production ready.}
|
13
|
+
spec.homepage = "https://github.com/Aetherus/phantom"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
data/spec/base_spec.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'phantom'
|
2
|
+
|
3
|
+
describe Phantom do
|
4
|
+
pid_file = 'tmp/phantom.pid'
|
5
|
+
|
6
|
+
after do
|
7
|
+
File.delete pid_file if File.exist? pid_file
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should remove the pid file when normally ends.' do
|
11
|
+
Phantom.run(pid_file: pid_file) {}
|
12
|
+
File.should_not exist(pid_file)
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should raise error if pre-exists pid file' do
|
16
|
+
File.new(pid_file, 'w').close
|
17
|
+
expect{
|
18
|
+
Phantom.run(pid_file: pid_file) do end
|
19
|
+
}.to raise_error(Phantom::ForkError)
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should call on_ok when sub processed normally ends' do
|
23
|
+
i = 0
|
24
|
+
err = nil
|
25
|
+
Phantom.run(pid_file: pid_file, on_ok: lambda{i += 1}, on_error: lambda{|e| err = e}) do
|
26
|
+
end
|
27
|
+
sleep 3
|
28
|
+
i.should == 1
|
29
|
+
err.should == nil
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should call on_error when sub process raises an unhandled error' do
|
33
|
+
err = nil
|
34
|
+
i = 0
|
35
|
+
Phantom.run(pid_file: pid_file, on_ok: lambda{i += 1}, on_error: lambda{|e| err = e}) do
|
36
|
+
raise 'Wa ha ha!'
|
37
|
+
i += 1
|
38
|
+
end
|
39
|
+
|
40
|
+
sleep 3
|
41
|
+
|
42
|
+
err.should be_an(StandardError)
|
43
|
+
err.message.should == 'Wa ha ha!'
|
44
|
+
i.should == 0
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should be OK if pid_file is not given' do
|
48
|
+
Phantom.run do end
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'should return a Phantom::Base instance when run' do
|
52
|
+
phantom = Phantom.run do end
|
53
|
+
phantom.should be_an(Phantom::Base)
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'should do nothing if block is not given' do
|
57
|
+
phantom = Phantom.run
|
58
|
+
phantom.pid.should be_nil
|
59
|
+
end
|
60
|
+
end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: phantom
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aetherus
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-11-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: This gem is developed to implement callback mechanism to sub processes.
|
56
|
+
email:
|
57
|
+
- sasabune0122@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- .idea/encodings.xml
|
64
|
+
- .idea/misc.xml
|
65
|
+
- .idea/modules.xml
|
66
|
+
- .idea/phantom.iml
|
67
|
+
- .idea/scopes/scope_settings.xml
|
68
|
+
- .idea/vcs.xml
|
69
|
+
- Gemfile
|
70
|
+
- LICENSE.txt
|
71
|
+
- README.md
|
72
|
+
- Rakefile
|
73
|
+
- lib/phantom.rb
|
74
|
+
- lib/phantom/base.rb
|
75
|
+
- lib/phantom/class_methods.rb
|
76
|
+
- lib/phantom/fork_error.rb
|
77
|
+
- lib/phantom/status.rb
|
78
|
+
- lib/phantom/version.rb
|
79
|
+
- phantom.gemspec
|
80
|
+
- spec/base_spec.rb
|
81
|
+
- spec/phantom_spec.rb
|
82
|
+
homepage: https://github.com/Aetherus/phantom
|
83
|
+
licenses:
|
84
|
+
- MIT
|
85
|
+
metadata: {}
|
86
|
+
post_install_message:
|
87
|
+
rdoc_options: []
|
88
|
+
require_paths:
|
89
|
+
- lib
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - '>='
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
requirements: []
|
101
|
+
rubyforge_project:
|
102
|
+
rubygems_version: 2.1.9
|
103
|
+
signing_key:
|
104
|
+
specification_version: 4
|
105
|
+
summary: This gem is developed to implement callback mechanism to sub processes. Not
|
106
|
+
production ready.
|
107
|
+
test_files:
|
108
|
+
- spec/base_spec.rb
|
109
|
+
- spec/phantom_spec.rb
|