exceptional_fork 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 747654a03842e5bf513885263e928420649bc789
4
+ data.tar.gz: 209e893bffed33cf0610551a228f11c1509580f2
5
+ SHA512:
6
+ metadata.gz: 5266e24496d23f343040a1c7fb8820e4513f6162acea0fb1bc9a4fd8a15e3ce8b23d42741a9801e935ab96c320354f0380a369db91f3783569c14d6549eeb17c
7
+ data.tar.gz: 1cc0e5f91b46b08dccf7c7870e0551e30dad4eac681af03491e3368ce9484f8c757240061b4a3e52ac45fc587c9497f5aace93441f4ee29d4981121511a350df
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.14"
10
+ gem "rdoc", "~> 3.12"
11
+ gem "bundler", "~> 1.0"
12
+ gem "jeweler", "~> 2.0.1"
13
+ gem "simplecov", ">= 0"
14
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2014 Julik Tarkhanov
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,33 @@
1
+ = exceptional_fork
2
+
3
+ Fork with a block and wait until the forked child exits.
4
+ Any exceptions raised within the block will be re-raised from this
5
+ in the parent process (where you call it from).
6
+
7
+ ExceptionalFork.fork_and_wait { raise "Explosion! "} # raises a RuntimeError
8
+
9
+ or for something that runs longer:
10
+
11
+ ExceptionalFork.fork_and_wait do
12
+ perform_long_running_job! # this raises some EOFError or another
13
+ end
14
+ #=> EOFError... # more data and the backtrace.
15
+
16
+ It is not guaranteed that all the exception metadata will be reinstated due to
17
+ marshaling/unmarshaling mechanics, but it helps debugging nevertheless.
18
+
19
+ == Contributing to exceptional_fork
20
+
21
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
22
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
23
+ * Fork the project.
24
+ * Start a feature/bugfix branch.
25
+ * Commit and push until you are happy with your contribution.
26
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
27
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
28
+
29
+ == Copyright
30
+
31
+ Copyright (c) 2014 Julik Tarkhanov. See LICENSE.txt for
32
+ further details.
33
+
data/Rakefile ADDED
@@ -0,0 +1,52 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require File.dirname(__FILE__) + '/lib/exceptional_fork'
15
+ require 'jeweler'
16
+ Jeweler::Tasks.new do |gem|
17
+ # gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
18
+ gem.version = ExceptionalFork::VERSION
19
+ gem.name = "exceptional_fork"
20
+ gem.homepage = "http://github.com/julik/exceptional_fork"
21
+ gem.license = "MIT"
22
+ gem.summary = %Q{ Raise exceptions from the forked child process in the parent }
23
+ gem.description = %Q{ Uses pipes to re-raise exceptions. Something better than an exit code has to exist. }
24
+ gem.email = "me@julik.nl"
25
+ gem.authors = ["Julik Tarkhanov"]
26
+ # dependencies defined in Gemfile
27
+ end
28
+ Jeweler::RubygemsDotOrgTasks.new
29
+
30
+ require 'rspec/core'
31
+ require 'rspec/core/rake_task'
32
+ RSpec::Core::RakeTask.new(:spec) do |spec|
33
+ spec.pattern = FileList['spec/**/*_spec.rb']
34
+ end
35
+
36
+ desc "Code coverage detail"
37
+ task :simplecov do
38
+ ENV['COVERAGE'] = "true"
39
+ Rake::Task['spec'].execute
40
+ end
41
+
42
+ task :default => :spec
43
+
44
+ require 'rdoc/task'
45
+ Rake::RDocTask.new do |rdoc|
46
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
47
+
48
+ rdoc.rdoc_dir = 'rdoc'
49
+ rdoc.title = "exceptional_fork #{version}"
50
+ rdoc.rdoc_files.include('README*')
51
+ rdoc.rdoc_files.include('lib/**/*.rb')
52
+ end
@@ -0,0 +1,72 @@
1
+ module ExceptionalFork
2
+ VERSION = '1.0.0'
3
+
4
+ # Fork with a block and wait until the forked child exits.
5
+ # Any exceptions raised within the block will be re-raised from this
6
+ # in the parent process (where you call it from).
7
+ #
8
+ # ExceptionalFork.fork_and_wait { raise "Explosion! "} # raises a RuntimeError
9
+ #
10
+ # or for something that runs longer:
11
+ #
12
+ # ExceptionalFork.fork_and_wait do
13
+ # perform_long_running_job! # this raises some EOFError or another
14
+ # end
15
+ # #=> EOFError... # more data and the backtrace.
16
+ #
17
+ # It is not guaranteed that all the exception metadata will be reinstated due to
18
+ # marshaling/unmarshaling mechanics, but it helps debugging nevertheless.
19
+ def fork_and_wait
20
+ # Redirect the exceptions in the child to the pipe. When we get a non-zero
21
+ # exit code we can read from that pipe to obtain the exception.
22
+ reader, writer = IO.pipe
23
+
24
+ # Run the block in a forked child
25
+ pid = fork_with_error_output(writer) { yield }
26
+
27
+ # Wait for the forked process to exit
28
+ Process.wait(pid)
29
+
30
+ writer.close rescue IOError # Close the writer so that we can read from the reader
31
+ child_error = reader.read # Read the error output
32
+ reader.close rescue IOError # Do not leak pipes since the process might be long-lived
33
+
34
+ if $?.exitstatus != 0 # If the process exited uncleanly capture the error
35
+ unmarshaled_error, backtrace_in_child = Marshal.load(child_error)
36
+ # Pick up the exception
37
+ reconstructed_error = unmarshaled_error.exception
38
+
39
+ # Reconstruct the backtrace
40
+ if reconstructed_error.respond_to?(:set_backtrace)
41
+ reconstructed_error.set_backtrace(backtrace_in_child)
42
+ end
43
+
44
+ raise reconstructed_error # ..and re-raise it in this (parent) process
45
+ end
46
+ end
47
+
48
+ def fork_with_error_output(errors_pipe)
49
+ pid = Process.fork do
50
+ # Tracking if the op failed
51
+ success = false
52
+ begin
53
+ # Run the closure passed to the fork_with_error_output method
54
+ yield
55
+ success = true
56
+ rescue Exception => exception
57
+ # Write a YAML dump of the exception and the backtrace to the error
58
+ # pipe, so that we can re-raise it in the parent process ;-)
59
+ error_payload = [exception, exception.backtrace.to_a]
60
+ errors_pipe.puts(Marshal.dump(error_payload))
61
+ errors_pipe.flush
62
+ ensure
63
+ Process.exit! success # Exit maintaining the status code
64
+ end
65
+ end
66
+
67
+ # Return the PID of the forked child
68
+ pid
69
+ end
70
+
71
+ extend self
72
+ end
@@ -0,0 +1,19 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "ExceptionalFork" do
4
+ it "re-raises the exception from a subprocess" do
5
+ expect(Process).to receive(:fork).and_call_original
6
+
7
+ pid_of_parent = Process.pid
8
+ begin
9
+ ExceptionalFork.fork_and_wait do
10
+ raise "This is process #{Process.pid} calling"
11
+ end
12
+ expect(false).to eq(true), "This should never be reached"
13
+ rescue RuntimeError => e
14
+ matches = (e.message =~ /This is process (\d+) calling/)
15
+ expect(matches).not_to be_nil
16
+ expect($1).not_to eq(pid_of_parent.to_s)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,29 @@
1
+ require 'simplecov'
2
+
3
+ module SimpleCov::Configuration
4
+ def clean_filters
5
+ @filters = []
6
+ end
7
+ end
8
+
9
+ SimpleCov.configure do
10
+ clean_filters
11
+ load_profile 'test_frameworks'
12
+ end
13
+
14
+ ENV["COVERAGE"] && SimpleCov.start do
15
+ add_filter "/.rvm/"
16
+ end
17
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
18
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
19
+
20
+ require 'rspec'
21
+ require 'exceptional_fork'
22
+
23
+ # Requires supporting files with custom matchers and macros, etc,
24
+ # in ./support/ and its subdirectories.
25
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
26
+
27
+ RSpec.configure do |config|
28
+ config.mock_with :rspec
29
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: exceptional_fork
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Julik Tarkhanov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rdoc
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.12'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: jeweler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 2.0.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 2.0.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: " Uses pipes to re-raise exceptions. Something better than an exit code
84
+ has to exist. "
85
+ email: me@julik.nl
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files:
89
+ - LICENSE.txt
90
+ - README.rdoc
91
+ files:
92
+ - ".document"
93
+ - ".rspec"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.rdoc
97
+ - Rakefile
98
+ - lib/exceptional_fork.rb
99
+ - spec/exceptional_fork_spec.rb
100
+ - spec/spec_helper.rb
101
+ homepage: http://github.com/julik/exceptional_fork
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.2.2
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Raise exceptions from the forked child process in the parent
125
+ test_files: []