Module: Timeout
- Defined in:
- lib/convenient_service/debug/timeout.rb
Class Method Summary collapse
-
.timeout(sec, klass = nil, message = nil) ⇒ Object
:yield: +sec+.
Class Method Details
.timeout(sec, klass = nil, message = nil) ⇒ Object
:yield: +sec+
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/convenient_service/debug/timeout.rb', line 23 def timeout(sec, klass = nil, = nil) #:yield: +sec+ return yield(sec) if sec == nil or sec.zero? ||= "execution expired".freeze from = "from #{caller_locations(1, 1)[0]}" if $DEBUG e = Error bl = proc do |exception| begin x = Thread.current y = Thread.start { puts "[ConvenientService][Timeout]: Start timeout thread `#{Thread.current.object_id}`." Thread.current.name = from begin sleep sec rescue => e puts "[ConvenientService][Timeout]: End timeout thread `#{Thread.current.object_id}` by rescue." x.raise e else puts "[ConvenientService][Timeout]: End timeout thread `#{Thread.current.object_id}` by else." x.raise exception, end } return yield(sec) ensure if y puts "[ConvenientService][Timeout]: End timeout thread `#{y.object_id}` by kill." y.kill y.join # make sure y is dead. end end end if klass begin bl.call(klass) rescue klass => e bt = e.backtrace end else bt = Error.catch(, &bl) end level = -caller(CALLER_OFFSET).size-2 while THIS_FILE =~ bt[level] bt.delete_at(level) end raise(e, , bt) end |