Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/ruby22/lib64/ruby/2.2.0
File: timeout.rb
# Timeout long-running blocks
[0] Fix | Delete
#
[1] Fix | Delete
# == Synopsis
[2] Fix | Delete
#
[3] Fix | Delete
# require 'timeout'
[4] Fix | Delete
# status = Timeout::timeout(5) {
[5] Fix | Delete
# # Something that should be interrupted if it takes more than 5 seconds...
[6] Fix | Delete
# }
[7] Fix | Delete
#
[8] Fix | Delete
# == Description
[9] Fix | Delete
#
[10] Fix | Delete
# Timeout provides a way to auto-terminate a potentially long-running
[11] Fix | Delete
# operation if it hasn't finished in a fixed amount of time.
[12] Fix | Delete
#
[13] Fix | Delete
# Previous versions didn't use a module for namespacing, however
[14] Fix | Delete
# #timeout is provided for backwards compatibility. You
[15] Fix | Delete
# should prefer Timeout#timeout instead.
[16] Fix | Delete
#
[17] Fix | Delete
# == Copyright
[18] Fix | Delete
#
[19] Fix | Delete
# Copyright:: (C) 2000 Network Applied Communication Laboratory, Inc.
[20] Fix | Delete
# Copyright:: (C) 2000 Information-technology Promotion Agency, Japan
[21] Fix | Delete
[22] Fix | Delete
module Timeout
[23] Fix | Delete
# Raised by Timeout#timeout when the block times out.
[24] Fix | Delete
class Error < RuntimeError
[25] Fix | Delete
attr_reader :thread
[26] Fix | Delete
[27] Fix | Delete
def self.catch(*args)
[28] Fix | Delete
exc = new(*args)
[29] Fix | Delete
exc.instance_variable_set(:@thread, Thread.current)
[30] Fix | Delete
::Kernel.catch(exc) {yield exc}
[31] Fix | Delete
end
[32] Fix | Delete
[33] Fix | Delete
def exception(*)
[34] Fix | Delete
# TODO: use Fiber.current to see if self can be thrown
[35] Fix | Delete
if self.thread == Thread.current
[36] Fix | Delete
bt = caller
[37] Fix | Delete
begin
[38] Fix | Delete
throw(self, bt)
[39] Fix | Delete
rescue UncaughtThrowError
[40] Fix | Delete
end
[41] Fix | Delete
end
[42] Fix | Delete
self
[43] Fix | Delete
end
[44] Fix | Delete
end
[45] Fix | Delete
ExitException = Error
[46] Fix | Delete
[47] Fix | Delete
# :stopdoc:
[48] Fix | Delete
THIS_FILE = /\A#{Regexp.quote(__FILE__)}:/o
[49] Fix | Delete
CALLER_OFFSET = ((c = caller[0]) && THIS_FILE =~ c) ? 1 : 0
[50] Fix | Delete
# :startdoc:
[51] Fix | Delete
[52] Fix | Delete
# Perform an operation in a block, raising an error if it takes longer than
[53] Fix | Delete
# +sec+ seconds to complete.
[54] Fix | Delete
#
[55] Fix | Delete
# +sec+:: Number of seconds to wait for the block to terminate. Any number
[56] Fix | Delete
# may be used, including Floats to specify fractional seconds. A
[57] Fix | Delete
# value of 0 or +nil+ will execute the block without any timeout.
[58] Fix | Delete
# +klass+:: Exception Class to raise if the block fails to terminate
[59] Fix | Delete
# in +sec+ seconds. Omitting will use the default, Timeout::Error
[60] Fix | Delete
#
[61] Fix | Delete
# Returns the result of the block *if* the block completed before
[62] Fix | Delete
# +sec+ seconds, otherwise throws an exception, based on the value of +klass+.
[63] Fix | Delete
#
[64] Fix | Delete
# The exception thrown to terminate the given block cannot be rescued inside
[65] Fix | Delete
# the block unless +klass+ is given explicitly.
[66] Fix | Delete
#
[67] Fix | Delete
# Note that this is both a method of module Timeout, so you can <tt>include
[68] Fix | Delete
# Timeout</tt> into your classes so they have a #timeout method, as well as
[69] Fix | Delete
# a module method, so you can call it directly as Timeout.timeout().
[70] Fix | Delete
def timeout(sec, klass = nil) #:yield: +sec+
[71] Fix | Delete
return yield(sec) if sec == nil or sec.zero?
[72] Fix | Delete
message = "execution expired"
[73] Fix | Delete
e = Error
[74] Fix | Delete
bl = proc do |exception|
[75] Fix | Delete
begin
[76] Fix | Delete
x = Thread.current
[77] Fix | Delete
y = Thread.start {
[78] Fix | Delete
begin
[79] Fix | Delete
sleep sec
[80] Fix | Delete
rescue => e
[81] Fix | Delete
x.raise e
[82] Fix | Delete
else
[83] Fix | Delete
x.raise exception, message
[84] Fix | Delete
end
[85] Fix | Delete
}
[86] Fix | Delete
return yield(sec)
[87] Fix | Delete
ensure
[88] Fix | Delete
if y
[89] Fix | Delete
y.kill
[90] Fix | Delete
y.join # make sure y is dead.
[91] Fix | Delete
end
[92] Fix | Delete
end
[93] Fix | Delete
end
[94] Fix | Delete
if klass
[95] Fix | Delete
begin
[96] Fix | Delete
bl.call(klass)
[97] Fix | Delete
rescue klass => e
[98] Fix | Delete
bt = e.backtrace
[99] Fix | Delete
end
[100] Fix | Delete
else
[101] Fix | Delete
bt = Error.catch(message, &bl)
[102] Fix | Delete
end
[103] Fix | Delete
rej = /\A#{Regexp.quote(__FILE__)}:#{__LINE__-4}\z/o
[104] Fix | Delete
bt.reject! {|m| rej =~ m}
[105] Fix | Delete
level = -caller(CALLER_OFFSET).size
[106] Fix | Delete
while THIS_FILE =~ bt[level]
[107] Fix | Delete
bt.delete_at(level)
[108] Fix | Delete
end
[109] Fix | Delete
raise(e, message, bt)
[110] Fix | Delete
end
[111] Fix | Delete
[112] Fix | Delete
module_function :timeout
[113] Fix | Delete
end
[114] Fix | Delete
[115] Fix | Delete
# Identical to:
[116] Fix | Delete
#
[117] Fix | Delete
# Timeout::timeout(n, e, &block).
[118] Fix | Delete
#
[119] Fix | Delete
# This method is deprecated and provided only for backwards compatibility.
[120] Fix | Delete
# You should use Timeout#timeout instead.
[121] Fix | Delete
def timeout(n, e = nil, &block)
[122] Fix | Delete
Timeout::timeout(n, e, &block)
[123] Fix | Delete
end
[124] Fix | Delete
[125] Fix | Delete
# Another name for Timeout::Error, defined for backwards compatibility with
[126] Fix | Delete
# earlier versions of timeout.rb.
[127] Fix | Delete
TimeoutError = Timeout::Error
[128] Fix | Delete
[129] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function