runaway 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +20 -0
- data/README.md +39 -0
- data/Rakefile +36 -0
- data/lib/runaway.rb +95 -0
- data/runaway.gemspec +59 -0
- data/spec/runaway_spec.rb +54 -0
- data/spec/spec_helper.rb +9 -0
- metadata +117 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f2394ae85e1bcf9ff2046d3892f33ece782e2e22
|
4
|
+
data.tar.gz: 4838053519c1500f34bef114eb374a2b05af9833
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 50c6402a8d00358139c9e93e37a02a4d34819e63ea6c7f00b27e6b544107af71d1a60b14671c2115c6d04565fe0d3a4f0310c2c79a3f100c453436f61b2e0a24
|
7
|
+
data.tar.gz: 8555a4a081a964299e1866ee00bb0728984a87303add7f89b708a26429006f28721064d21bbe7e95dfd5f0a4106590de288a491e68e0796eb5170f87bad65f9a
|
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2016 WeTransfer
|
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.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# runaway
|
2
|
+
|
3
|
+
Spin off work into child processes and terminate it on time
|
4
|
+
|
5
|
+
To simply ensure the process keeps responding (the Ruby runtime still can do context-switching
|
6
|
+
and can respond to Unix signals):
|
7
|
+
|
8
|
+
Runaway.spin do
|
9
|
+
ActiveRAMGobbler.load_many_things(num_things: 10_000_000)
|
10
|
+
end
|
11
|
+
|
12
|
+
If you need the process to be done within a certain time period (and if it takes longer
|
13
|
+
it probably is hanging):
|
14
|
+
|
15
|
+
Runaway.spin(must_quite_within: 15) do # ensures termination within 15 seconds
|
16
|
+
`/bin/proprietary_render_server/bin/render --put-server-on-fire=yes`
|
17
|
+
end
|
18
|
+
|
19
|
+
If the child process quits with the exit code of 0 it is considered a "clean" termination.
|
20
|
+
Anything else (any non-zero exit status) is considered a failure in the child process and
|
21
|
+
will cause an exception to be raised in the master.
|
22
|
+
|
23
|
+
The `spin` method in the master blocks until the child process terminates.
|
24
|
+
|
25
|
+
## Contributing to runaway
|
26
|
+
|
27
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
28
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
29
|
+
* Fork the project.
|
30
|
+
* Start a feature/bugfix branch.
|
31
|
+
* Commit and push until you are happy with your contribution.
|
32
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
33
|
+
* 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.
|
34
|
+
|
35
|
+
## Copyright
|
36
|
+
|
37
|
+
Copyright (c) 2016 Julik Tarkhanov. See LICENSE.txt for
|
38
|
+
further details.
|
39
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,36 @@
|
|
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 'jeweler'
|
15
|
+
require_relative 'lib/runaway'
|
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 = Runaway::VERSION
|
19
|
+
gem.name = "runaway"
|
20
|
+
gem.homepage = "https://github.com/WeTransfer/runaway"
|
21
|
+
gem.license = "MIT"
|
22
|
+
gem.description = %Q{Spin off blocks in child processes and make sure they terminate on time}
|
23
|
+
gem.summary = %Q{Strict control over child processes}
|
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
|
+
task :default => :spec
|
data/lib/runaway.rb
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'securerandom'
|
2
|
+
|
3
|
+
module Runaway
|
4
|
+
VERSION = '1.0.0'
|
5
|
+
|
6
|
+
UncleanExit = Class.new(StandardError)
|
7
|
+
Child = Class.new(StandardError)
|
8
|
+
HeartbeatTimeout = Class.new(Child)
|
9
|
+
RuntimeExceeded = Class.new(Child)
|
10
|
+
|
11
|
+
DEFAULT_HEARTBEAT_INTERVAL = 2
|
12
|
+
TERM = 'TERM'.freeze
|
13
|
+
KILL = 'KILL'.freeze
|
14
|
+
USR2 = 'USR2'.freeze
|
15
|
+
DEFAULT = "DEFAULT".freeze
|
16
|
+
INF = (1.0 / 0.0)
|
17
|
+
|
18
|
+
# Acts as a substitute for a Logger
|
19
|
+
module NullLogger; def self.warn(*); end; end
|
20
|
+
|
21
|
+
def self.spin(must_quit_within: INF, heartbeat_interval: DEFAULT_HEARTBEAT_INTERVAL,
|
22
|
+
logger: NullLogger, &block_to_run_in_child)
|
23
|
+
cookie = SecureRandom.hex(1)
|
24
|
+
r, w = IO.pipe
|
25
|
+
child_pid = fork do
|
26
|
+
r.close_read
|
27
|
+
# Remove anything that was there from the parent
|
28
|
+
[USR2, TERM, KILL].each { |reset_sig| trap(reset_sig, DEFAULT) }
|
29
|
+
|
30
|
+
# When the parent asks us for a heartbeat, send the cookie back
|
31
|
+
trap(USR2) { w.write(cookie); w.flush }
|
32
|
+
block_to_run_in_child.call
|
33
|
+
end
|
34
|
+
w.close_write
|
35
|
+
|
36
|
+
started_at = Time.now
|
37
|
+
|
38
|
+
has_quit = false
|
39
|
+
unclean_exit_error = nil
|
40
|
+
waiter_t = Thread.new do
|
41
|
+
has_quit, status = Process.wait2(child_pid)
|
42
|
+
unless status.exitstatus && status.exitstatus.zero?
|
43
|
+
unclean_exit_error = UncleanExit.new("#{child_pid} exited uncleanly: #{status.inspect}")
|
44
|
+
end
|
45
|
+
end
|
46
|
+
waiter_t.abort_on_exception = true
|
47
|
+
|
48
|
+
soft_signal = ->(sig) {
|
49
|
+
(Process.kill(sig, child_pid) rescue Errno::ESRCH) if !has_quit
|
50
|
+
}
|
51
|
+
|
52
|
+
begin
|
53
|
+
loop do
|
54
|
+
sleep heartbeat_interval
|
55
|
+
|
56
|
+
# First check if it has exceeded it's wall clock time allowance
|
57
|
+
running_for = Time.now - started_at
|
58
|
+
if running_for > must_quit_within
|
59
|
+
raise RuntimeExceeded.new('%d did not terminate after %d secs (limited to %d secs)' % [
|
60
|
+
child_pid, running_for, must_quit_within])
|
61
|
+
end
|
62
|
+
|
63
|
+
# Then send it the USR2 as a "ping", and expect a "pong" in
|
64
|
+
# the form of a pipe write. If the pipe is still not readable
|
65
|
+
# after a certain time, we assume the process has hung.
|
66
|
+
Process.kill(USR2, child_pid)
|
67
|
+
select_timeout = (heartbeat_interval * 2)
|
68
|
+
ready_read = IO.select([r], [], [], select_timeout)
|
69
|
+
if ready_read.nil?
|
70
|
+
raise HeartbeatTimeout.new('%d did not reply to heartbeat after %d secs' % [child_pid, select_timeout])
|
71
|
+
end
|
72
|
+
r.read(cookie.bytesize)
|
73
|
+
end
|
74
|
+
rescue Runaway => terminating_error
|
75
|
+
logger.error "Terminating %d - %s: %s" % [child_pid, terminating_error.class, terminating_error.message]
|
76
|
+
soft_signal[TERM]
|
77
|
+
sleep 5
|
78
|
+
soft_signal[KILL]
|
79
|
+
|
80
|
+
raise terminating_error
|
81
|
+
rescue Errno::EBADF, Errno::ESRCH, Errno::EPIPE
|
82
|
+
# Could not read from the pipe - the child quit, or could not send signal
|
83
|
+
end
|
84
|
+
|
85
|
+
# If the loop has terminated the process has certainly quit, and we can join the waiter thread
|
86
|
+
waiter_t.join
|
87
|
+
|
88
|
+
# If the process exited uncleanly (not with status 0) raise an exception in the parent
|
89
|
+
raise unclean_exit_error if unclean_exit_error
|
90
|
+
|
91
|
+
:done
|
92
|
+
ensure
|
93
|
+
r.close_read
|
94
|
+
end
|
95
|
+
end
|
data/runaway.gemspec
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
# stub: runaway 1.0.0 ruby lib
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "runaway"
|
9
|
+
s.version = "1.0.0"
|
10
|
+
|
11
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
|
+
s.require_paths = ["lib"]
|
13
|
+
s.authors = ["Julik Tarkhanov"]
|
14
|
+
s.date = "2016-02-26"
|
15
|
+
s.description = "Spin off blocks in child processes and make sure they terminate on time"
|
16
|
+
s.email = "me@julik.nl"
|
17
|
+
s.extra_rdoc_files = [
|
18
|
+
"LICENSE.txt",
|
19
|
+
"README.md"
|
20
|
+
]
|
21
|
+
s.files = [
|
22
|
+
".document",
|
23
|
+
".rspec",
|
24
|
+
"Gemfile",
|
25
|
+
"LICENSE.txt",
|
26
|
+
"README.md",
|
27
|
+
"Rakefile",
|
28
|
+
"lib/runaway.rb",
|
29
|
+
"runaway.gemspec",
|
30
|
+
"spec/runaway_spec.rb",
|
31
|
+
"spec/spec_helper.rb"
|
32
|
+
]
|
33
|
+
s.homepage = "https://github.com/WeTransfer/runaway"
|
34
|
+
s.licenses = ["MIT"]
|
35
|
+
s.rubygems_version = "2.2.2"
|
36
|
+
s.summary = "Strict control over child processes"
|
37
|
+
|
38
|
+
if s.respond_to? :specification_version then
|
39
|
+
s.specification_version = 4
|
40
|
+
|
41
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
42
|
+
s.add_development_dependency(%q<rspec>, ["< 3.3", "~> 3.2"])
|
43
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0"])
|
44
|
+
s.add_development_dependency(%q<jeweler>, ["~> 2.0.1"])
|
45
|
+
s.add_development_dependency(%q<simplecov>, [">= 0"])
|
46
|
+
else
|
47
|
+
s.add_dependency(%q<rspec>, ["< 3.3", "~> 3.2"])
|
48
|
+
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
49
|
+
s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
|
50
|
+
s.add_dependency(%q<simplecov>, [">= 0"])
|
51
|
+
end
|
52
|
+
else
|
53
|
+
s.add_dependency(%q<rspec>, ["< 3.3", "~> 3.2"])
|
54
|
+
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
55
|
+
s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
|
56
|
+
s.add_dependency(%q<simplecov>, [">= 0"])
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
require 'tempfile'
|
3
|
+
|
4
|
+
describe "Runaway" do
|
5
|
+
|
6
|
+
it 'supports all the options' do
|
7
|
+
require 'logger'
|
8
|
+
Runaway.spin(must_quit_within: 2, heartbeat_interval: 0.3, logger: Logger.new($stdout)) {} # just do nothing
|
9
|
+
end
|
10
|
+
|
11
|
+
context 'with a process that quits cleanly' do
|
12
|
+
it 'executes the block' do
|
13
|
+
tf = Tempfile.new('signal')
|
14
|
+
Runaway.spin { sleep(3); tf << 'written' }
|
15
|
+
tf.rewind
|
16
|
+
expect(tf.read).to eq('written')
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'with a process that sleeps for too long' do
|
21
|
+
it 'terminates the process' do
|
22
|
+
expect {
|
23
|
+
Runaway.spin(must_quit_within: 3) do
|
24
|
+
sleep 4
|
25
|
+
end
|
26
|
+
}.to raise_error {|err|
|
27
|
+
expect(err).to be_kind_of(Runaway::RuntimeExceeded)
|
28
|
+
expect(err.message).to match(/\d+ did not terminate after 4 secs \(limited to 3 secs\)/)
|
29
|
+
}
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'with a process that exits with a non-0 status' do
|
34
|
+
it 'raises an unclean termination error' do
|
35
|
+
expect {
|
36
|
+
Runaway.spin { exit 1 }
|
37
|
+
}.to raise_error {|err|
|
38
|
+
expect(err).to be_kind_of(Runaway::UncleanExit)
|
39
|
+
expect(err.message).to match(/\d+ exited uncleanly/)
|
40
|
+
}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'when a process stops responding to heartbeats' do
|
45
|
+
it 'raises an error' do
|
46
|
+
expect {
|
47
|
+
Runaway.spin { trap('USR2', 'DEFAULT'); trap('USR2') {}; sleep 7 }
|
48
|
+
}.to raise_error {|err|
|
49
|
+
expect(err).to be_kind_of(Runaway::HeartbeatTimeout)
|
50
|
+
expect(err.message).to match(/\d+ did not reply to heartbeat after 4 secs/)
|
51
|
+
}
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: runaway
|
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: 2016-02-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: '3.3'
|
20
|
+
- - "~>"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '3.2'
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "<"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.3'
|
30
|
+
- - "~>"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '3.2'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: bundler
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '1.0'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '1.0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: jeweler
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 2.0.1
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 2.0.1
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: simplecov
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
description: Spin off blocks in child processes and make sure they terminate on time
|
76
|
+
email: me@julik.nl
|
77
|
+
executables: []
|
78
|
+
extensions: []
|
79
|
+
extra_rdoc_files:
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
files:
|
83
|
+
- ".document"
|
84
|
+
- ".rspec"
|
85
|
+
- Gemfile
|
86
|
+
- LICENSE.txt
|
87
|
+
- README.md
|
88
|
+
- Rakefile
|
89
|
+
- lib/runaway.rb
|
90
|
+
- runaway.gemspec
|
91
|
+
- spec/runaway_spec.rb
|
92
|
+
- spec/spec_helper.rb
|
93
|
+
homepage: https://github.com/WeTransfer/runaway
|
94
|
+
licenses:
|
95
|
+
- MIT
|
96
|
+
metadata: {}
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubyforge_project:
|
113
|
+
rubygems_version: 2.2.2
|
114
|
+
signing_key:
|
115
|
+
specification_version: 4
|
116
|
+
summary: Strict control over child processes
|
117
|
+
test_files: []
|