timeout 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/test.yml +24 -0
- data/.gitignore +9 -0
- data/Gemfile +9 -0
- data/LICENSE.txt +22 -0
- data/README.md +43 -0
- data/Rakefile +10 -0
- data/bin/console +7 -0
- data/bin/setup +6 -0
- data/lib/timeout.rb +132 -0
- data/lib/timeout/version.rb +3 -0
- data/timeout.gemspec +27 -0
- metadata +56 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6c2746096c21aa551558e072a40721124c2cecd54af3d4bc4b80905b36e0490e
|
4
|
+
data.tar.gz: d6a44634d0cc4defe129bb8877364a1b04e7581e300bfb88dd73a479f915baff
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4b0068d484ba661c40b51f8104823885aafe49be9ce1575439cc0d3b6788e240a17b649e085f0731120ae9ac83d3840f5db6aba73937edd90b68509c6759dd5e
|
7
|
+
data.tar.gz: 465d96e3e05988a772fd446030c3fc4e91d6e658023c8468154e3118f78651200cb7d6a995c8d55a99170ce44547132d2b7d123c455375f904861103a85cc5df
|
@@ -0,0 +1,24 @@
|
|
1
|
+
name: ubuntu
|
2
|
+
|
3
|
+
on: [push, pull_request]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
name: build (${{ matrix.ruby }} / ${{ matrix.os }})
|
8
|
+
strategy:
|
9
|
+
matrix:
|
10
|
+
ruby: [ 2.7, 2.6, 2.5, 2.4, head ]
|
11
|
+
os: [ ubuntu-latest, macos-latest ]
|
12
|
+
runs-on: ${{ matrix.os }}
|
13
|
+
steps:
|
14
|
+
- uses: actions/checkout@master
|
15
|
+
- name: Set up Ruby
|
16
|
+
uses: ruby/setup-ruby@v1
|
17
|
+
with:
|
18
|
+
ruby-version: ${{ matrix.ruby }}
|
19
|
+
- name: Install dependencies
|
20
|
+
run: |
|
21
|
+
gem install bundler --no-document
|
22
|
+
bundle install
|
23
|
+
- name: Run test
|
24
|
+
run: rake test
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (C) 1993-2013 Yukihiro Matsumoto. All rights reserved.
|
2
|
+
|
3
|
+
Redistribution and use in source and binary forms, with or without
|
4
|
+
modification, are permitted provided that the following conditions
|
5
|
+
are met:
|
6
|
+
1. Redistributions of source code must retain the above copyright
|
7
|
+
notice, this list of conditions and the following disclaimer.
|
8
|
+
2. Redistributions in binary form must reproduce the above copyright
|
9
|
+
notice, this list of conditions and the following disclaimer in the
|
10
|
+
documentation and/or other materials provided with the distribution.
|
11
|
+
|
12
|
+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
13
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
14
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
15
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
16
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
17
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
18
|
+
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
19
|
+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
20
|
+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
21
|
+
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
22
|
+
SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# Timeout
|
2
|
+
|
3
|
+
Timeout provides a way to auto-terminate a potentially long-running
|
4
|
+
operation if it hasn't finished in a fixed amount of time.
|
5
|
+
|
6
|
+
Previous versions didn't use a module for namespacing, however
|
7
|
+
#timeout is provided for backwards compatibility. You
|
8
|
+
should prefer Timeout.timeout instead.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'timeout'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install timeout
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
require 'timeout'
|
30
|
+
status = Timeout::timeout(5) {
|
31
|
+
# Something that should be interrupted if it takes more than 5 seconds...
|
32
|
+
}
|
33
|
+
```
|
34
|
+
|
35
|
+
## Development
|
36
|
+
|
37
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
38
|
+
|
39
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
40
|
+
|
41
|
+
## Contributing
|
42
|
+
|
43
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/timeout.
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
data/lib/timeout.rb
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
# Timeout long-running blocks
|
3
|
+
#
|
4
|
+
# == Synopsis
|
5
|
+
#
|
6
|
+
# require 'timeout'
|
7
|
+
# status = Timeout::timeout(5) {
|
8
|
+
# # Something that should be interrupted if it takes more than 5 seconds...
|
9
|
+
# }
|
10
|
+
#
|
11
|
+
# == Description
|
12
|
+
#
|
13
|
+
# Timeout provides a way to auto-terminate a potentially long-running
|
14
|
+
# operation if it hasn't finished in a fixed amount of time.
|
15
|
+
#
|
16
|
+
# Previous versions didn't use a module for namespacing, however
|
17
|
+
# #timeout is provided for backwards compatibility. You
|
18
|
+
# should prefer Timeout.timeout instead.
|
19
|
+
#
|
20
|
+
# == Copyright
|
21
|
+
#
|
22
|
+
# Copyright:: (C) 2000 Network Applied Communication Laboratory, Inc.
|
23
|
+
# Copyright:: (C) 2000 Information-technology Promotion Agency, Japan
|
24
|
+
|
25
|
+
module Timeout
|
26
|
+
# Raised by Timeout.timeout when the block times out.
|
27
|
+
class Error < RuntimeError
|
28
|
+
attr_reader :thread
|
29
|
+
|
30
|
+
def self.catch(*args)
|
31
|
+
exc = new(*args)
|
32
|
+
exc.instance_variable_set(:@thread, Thread.current)
|
33
|
+
::Kernel.catch(exc) {yield exc}
|
34
|
+
end
|
35
|
+
|
36
|
+
def exception(*)
|
37
|
+
# TODO: use Fiber.current to see if self can be thrown
|
38
|
+
if self.thread == Thread.current
|
39
|
+
bt = caller
|
40
|
+
begin
|
41
|
+
throw(self, bt)
|
42
|
+
rescue UncaughtThrowError
|
43
|
+
end
|
44
|
+
end
|
45
|
+
self
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# :stopdoc:
|
50
|
+
THIS_FILE = /\A#{Regexp.quote(__FILE__)}:/o
|
51
|
+
CALLER_OFFSET = ((c = caller[0]) && THIS_FILE =~ c) ? 1 : 0
|
52
|
+
private_constant :THIS_FILE, :CALLER_OFFSET
|
53
|
+
# :startdoc:
|
54
|
+
|
55
|
+
# Perform an operation in a block, raising an error if it takes longer than
|
56
|
+
# +sec+ seconds to complete.
|
57
|
+
#
|
58
|
+
# +sec+:: Number of seconds to wait for the block to terminate. Any number
|
59
|
+
# may be used, including Floats to specify fractional seconds. A
|
60
|
+
# value of 0 or +nil+ will execute the block without any timeout.
|
61
|
+
# +klass+:: Exception Class to raise if the block fails to terminate
|
62
|
+
# in +sec+ seconds. Omitting will use the default, Timeout::Error
|
63
|
+
# +message+:: Error message to raise with Exception Class.
|
64
|
+
# Omitting will use the default, "execution expired"
|
65
|
+
#
|
66
|
+
# Returns the result of the block *if* the block completed before
|
67
|
+
# +sec+ seconds, otherwise throws an exception, based on the value of +klass+.
|
68
|
+
#
|
69
|
+
# The exception thrown to terminate the given block cannot be rescued inside
|
70
|
+
# the block unless +klass+ is given explicitly. However, the block can use
|
71
|
+
# ensure to prevent the handling of the exception. For that reason, this
|
72
|
+
# method cannot be relied on to enforce timeouts for untrusted blocks.
|
73
|
+
#
|
74
|
+
# Note that this is both a method of module Timeout, so you can <tt>include
|
75
|
+
# Timeout</tt> into your classes so they have a #timeout method, as well as
|
76
|
+
# a module method, so you can call it directly as Timeout.timeout().
|
77
|
+
def timeout(sec, klass = nil, message = nil) #:yield: +sec+
|
78
|
+
return yield(sec) if sec == nil or sec.zero?
|
79
|
+
message ||= "execution expired".freeze
|
80
|
+
from = "from #{caller_locations(1, 1)[0]}" if $DEBUG
|
81
|
+
e = Error
|
82
|
+
bl = proc do |exception|
|
83
|
+
begin
|
84
|
+
x = Thread.current
|
85
|
+
y = Thread.start {
|
86
|
+
Thread.current.name = from
|
87
|
+
begin
|
88
|
+
sleep sec
|
89
|
+
rescue => e
|
90
|
+
x.raise e
|
91
|
+
else
|
92
|
+
x.raise exception, message
|
93
|
+
end
|
94
|
+
}
|
95
|
+
return yield(sec)
|
96
|
+
ensure
|
97
|
+
if y
|
98
|
+
y.kill
|
99
|
+
y.join # make sure y is dead.
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
if klass
|
104
|
+
begin
|
105
|
+
bl.call(klass)
|
106
|
+
rescue klass => e
|
107
|
+
bt = e.backtrace
|
108
|
+
end
|
109
|
+
else
|
110
|
+
bt = Error.catch(message, &bl)
|
111
|
+
end
|
112
|
+
level = -caller(CALLER_OFFSET).size-2
|
113
|
+
while THIS_FILE =~ bt[level]
|
114
|
+
bt.delete_at(level)
|
115
|
+
end
|
116
|
+
raise(e, message, bt)
|
117
|
+
end
|
118
|
+
|
119
|
+
module_function :timeout
|
120
|
+
end
|
121
|
+
|
122
|
+
def timeout(*args, &block)
|
123
|
+
warn "Object##{__method__} is deprecated, use Timeout.timeout instead.", uplevel: 1
|
124
|
+
Timeout.timeout(*args, &block)
|
125
|
+
end
|
126
|
+
|
127
|
+
# Another name for Timeout::Error, defined for backwards compatibility with
|
128
|
+
# earlier versions of timeout.rb.
|
129
|
+
TimeoutError = Timeout::Error
|
130
|
+
class Object
|
131
|
+
deprecate_constant :TimeoutError
|
132
|
+
end
|
data/timeout.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
begin
|
2
|
+
require_relative "lib/timeout/version"
|
3
|
+
rescue LoadError # Fallback to load version file in ruby core repository
|
4
|
+
require_relative "version"
|
5
|
+
end
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "timeout"
|
9
|
+
spec.version = Timeout::VERSION
|
10
|
+
spec.authors = ["Yukihiro Matsumoto"]
|
11
|
+
spec.email = ["matz@ruby-lang.org"]
|
12
|
+
|
13
|
+
spec.summary = %q{Auto-terminate potentially long-running operations in Ruby.}
|
14
|
+
spec.description = %q{Auto-terminate potentially long-running operations in Ruby.}
|
15
|
+
spec.homepage = "https://github.com/ruby/timeout"
|
16
|
+
spec.license = "BSD-2-Clause"
|
17
|
+
|
18
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
19
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
20
|
+
|
21
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
22
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
23
|
+
end
|
24
|
+
spec.bindir = "exe"
|
25
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
|
+
spec.require_paths = ["lib"]
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: timeout
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yukihiro Matsumoto
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-04-01 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Auto-terminate potentially long-running operations in Ruby.
|
14
|
+
email:
|
15
|
+
- matz@ruby-lang.org
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".github/workflows/test.yml"
|
21
|
+
- ".gitignore"
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE.txt
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- bin/console
|
27
|
+
- bin/setup
|
28
|
+
- lib/timeout.rb
|
29
|
+
- lib/timeout/version.rb
|
30
|
+
- timeout.gemspec
|
31
|
+
homepage: https://github.com/ruby/timeout
|
32
|
+
licenses:
|
33
|
+
- BSD-2-Clause
|
34
|
+
metadata:
|
35
|
+
homepage_uri: https://github.com/ruby/timeout
|
36
|
+
source_code_uri: https://github.com/ruby/timeout
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
requirements: []
|
52
|
+
rubygems_version: 3.2.0.pre1
|
53
|
+
signing_key:
|
54
|
+
specification_version: 4
|
55
|
+
summary: Auto-terminate potentially long-running operations in Ruby.
|
56
|
+
test_files: []
|