Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/ruby30/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
VERSION = "0.1.1"
[25] Fix | Delete
[26] Fix | Delete
# Raised by Timeout.timeout when the block times out.
[27] Fix | Delete
class Error < RuntimeError
[28] Fix | Delete
attr_reader :thread
[29] Fix | Delete
[30] Fix | Delete
def self.catch(*args)
[31] Fix | Delete
exc = new(*args)
[32] Fix | Delete
exc.instance_variable_set(:@thread, Thread.current)
[33] Fix | Delete
::Kernel.catch(exc) {yield exc}
[34] Fix | Delete
end
[35] Fix | Delete
[36] Fix | Delete
def exception(*)
[37] Fix | Delete
# TODO: use Fiber.current to see if self can be thrown
[38] Fix | Delete
if self.thread == Thread.current
[39] Fix | Delete
bt = caller
[40] Fix | Delete
begin
[41] Fix | Delete
throw(self, bt)
[42] Fix | Delete
rescue UncaughtThrowError
[43] Fix | Delete
end
[44] Fix | Delete
end
[45] Fix | Delete
self
[46] Fix | Delete
end
[47] Fix | Delete
end
[48] Fix | Delete
[49] Fix | Delete
# :stopdoc:
[50] Fix | Delete
THIS_FILE = /\A#{Regexp.quote(__FILE__)}:/o
[51] Fix | Delete
CALLER_OFFSET = ((c = caller[0]) && THIS_FILE =~ c) ? 1 : 0
[52] Fix | Delete
private_constant :THIS_FILE, :CALLER_OFFSET
[53] Fix | Delete
# :startdoc:
[54] Fix | Delete
[55] Fix | Delete
# Perform an operation in a block, raising an error if it takes longer than
[56] Fix | Delete
# +sec+ seconds to complete.
[57] Fix | Delete
#
[58] Fix | Delete
# +sec+:: Number of seconds to wait for the block to terminate. Any number
[59] Fix | Delete
# may be used, including Floats to specify fractional seconds. A
[60] Fix | Delete
# value of 0 or +nil+ will execute the block without any timeout.
[61] Fix | Delete
# +klass+:: Exception Class to raise if the block fails to terminate
[62] Fix | Delete
# in +sec+ seconds. Omitting will use the default, Timeout::Error
[63] Fix | Delete
# +message+:: Error message to raise with Exception Class.
[64] Fix | Delete
# Omitting will use the default, "execution expired"
[65] Fix | Delete
#
[66] Fix | Delete
# Returns the result of the block *if* the block completed before
[67] Fix | Delete
# +sec+ seconds, otherwise throws an exception, based on the value of +klass+.
[68] Fix | Delete
#
[69] Fix | Delete
# The exception thrown to terminate the given block cannot be rescued inside
[70] Fix | Delete
# the block unless +klass+ is given explicitly. However, the block can use
[71] Fix | Delete
# ensure to prevent the handling of the exception. For that reason, this
[72] Fix | Delete
# method cannot be relied on to enforce timeouts for untrusted blocks.
[73] Fix | Delete
#
[74] Fix | Delete
# Note that this is both a method of module Timeout, so you can <tt>include
[75] Fix | Delete
# Timeout</tt> into your classes so they have a #timeout method, as well as
[76] Fix | Delete
# a module method, so you can call it directly as Timeout.timeout().
[77] Fix | Delete
def timeout(sec, klass = nil, message = nil) #:yield: +sec+
[78] Fix | Delete
return yield(sec) if sec == nil or sec.zero?
[79] Fix | Delete
message ||= "execution expired".freeze
[80] Fix | Delete
from = "from #{caller_locations(1, 1)[0]}" if $DEBUG
[81] Fix | Delete
e = Error
[82] Fix | Delete
bl = proc do |exception|
[83] Fix | Delete
begin
[84] Fix | Delete
x = Thread.current
[85] Fix | Delete
y = Thread.start {
[86] Fix | Delete
Thread.current.name = from
[87] Fix | Delete
begin
[88] Fix | Delete
sleep sec
[89] Fix | Delete
rescue => e
[90] Fix | Delete
x.raise e
[91] Fix | Delete
else
[92] Fix | Delete
x.raise exception, message
[93] Fix | Delete
end
[94] Fix | Delete
}
[95] Fix | Delete
return yield(sec)
[96] Fix | Delete
ensure
[97] Fix | Delete
if y
[98] Fix | Delete
y.kill
[99] Fix | Delete
y.join # make sure y is dead.
[100] Fix | Delete
end
[101] Fix | Delete
end
[102] Fix | Delete
end
[103] Fix | Delete
if klass
[104] Fix | Delete
begin
[105] Fix | Delete
bl.call(klass)
[106] Fix | Delete
rescue klass => e
[107] Fix | Delete
bt = e.backtrace
[108] Fix | Delete
end
[109] Fix | Delete
else
[110] Fix | Delete
bt = Error.catch(message, &bl)
[111] Fix | Delete
end
[112] Fix | Delete
level = -caller(CALLER_OFFSET).size-2
[113] Fix | Delete
while THIS_FILE =~ bt[level]
[114] Fix | Delete
bt.delete_at(level)
[115] Fix | Delete
end
[116] Fix | Delete
raise(e, message, bt)
[117] Fix | Delete
end
[118] Fix | Delete
[119] Fix | Delete
module_function :timeout
[120] Fix | Delete
end
[121] Fix | Delete
[122] Fix | Delete
def timeout(*args, &block)
[123] Fix | Delete
warn "Object##{__method__} is deprecated, use Timeout.timeout instead.", uplevel: 1
[124] Fix | Delete
Timeout.timeout(*args, &block)
[125] Fix | Delete
end
[126] Fix | Delete
[127] Fix | Delete
# Another name for Timeout::Error, defined for backwards compatibility with
[128] Fix | Delete
# earlier versions of timeout.rb.
[129] Fix | Delete
TimeoutError = Timeout::Error
[130] Fix | Delete
class Object
[131] Fix | Delete
deprecate_constant :TimeoutError
[132] Fix | Delete
end
[133] Fix | Delete
[134] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function