Module: ActiveBridge

Defined in:
lib/active_bridge/execute.rb,
lib/active_bridge/version.rb,
lib/active_bridge/throttle.rb

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Instance Method Details

#execute(opts = {}, &block) ⇒ Object



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

#throttle(started_at, max) ⇒ Object



4
5
6
7
8
9
# File 'lib/active_bridge/throttle.rb', line 4

def throttle(started_at, max)
  took = Time.now - started_at
  remaining = max - took
  sleep remaining if remaining > 0
  remaining
end