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