4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/active_bridge/execute.rb', line 4
def execute(opts = {}, &block)
queue = Queue.new
timeout_thr = if opts[:timeout]
Thread.new do
queue.push [:timeout, sleep(opts[:timeout])]
end
end
started_at = Time.now
main_thr = Thread.new do
queue.push [:ok, block.call]
end
op, value = queue.pop
took = Time.now - started_at
timeout_thr&.kill
main_thr.kill
main_thr.kill
main_thr.kill
case op
when :timeout
[:timeout, took, nil]
when :ok
[:ok, took, value]
else
raise "well, this is interesting"
end
end
|