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