Sha256: a8eaa4247174e5a34b20610779724877823b5e6a4cb1e47bcb34628eff233401

Contents?: true

Size: 777 Bytes

Versions: 4

Compression:

Stored size: 777 Bytes

Contents

# based on Timeout

module Dump
  # Timeout if does not finish or defer in requested time
  module ContiniousTimeout
    class TimeoutException < ::Exception; end

    class RestartException < ::Exception; end

    # Object with defer method
    class Deferer
      def initialize(thread)
        @thread = thread
      end

      def defer
        @thread.raise RestartException.new
      end
    end

    def self.timeout(sec)
      x = Thread.current
      y = Thread.start do
        1.times do
          begin
            sleep sec
          rescue RestartException
            retry
          end
        end
        x.raise TimeoutException, 'execution expired' if x.alive?
      end
      yield Deferer.new(y)
    ensure
      y.kill if y && y.alive?
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dump-1.1.0 lib/dump/continious_timeout.rb
dump-1.0.8 lib/dump/continious_timeout.rb
dump-1.0.7 lib/dump/continious_timeout.rb
dump-1.0.6 lib/dump/continious_timeout.rb