Class: HireFire::Web

Inherits:
Object
  • Object
show all
Defined in:
lib/hirefire/web.rb

Defined Under Namespace

Classes: DispatchError

Instance Method Summary collapse

Constructor Details

#initializeWeb

Returns a new instance of Web.



10
11
12
13
14
15
16
17
# File 'lib/hirefire/web.rb', line 10

def initialize
  @buffer = {}
  @mutex = Mutex.new
  @dispatcher_running = false
  @dispatch_interval = 1
  @dispatch_timeout = 5
  @buffer_ttl = 60
end

Instance Method Details

#add_to_buffer(request_queue_time) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/hirefire/web.rb', line 57

def add_to_buffer(request_queue_time)
  @mutex.synchronize do
    timestamp = Time.now.to_i
    @buffer[timestamp] ||= []
    @buffer[timestamp] << request_queue_time
  end
end

#dispatcher_running?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/hirefire/web.rb', line 53

def dispatcher_running?
  @mutex.synchronize { @dispatcher_running }
end

#start_dispatcherObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/hirefire/web.rb', line 19

def start_dispatcher
  @mutex.synchronize do
    return false if @dispatcher_running
    @dispatcher_running = true
  end

  logger.info "[HireFire] Starting web metrics dispatcher."

  @dispatcher = Thread.new do
    while dispatcher_running?
      dispatch_buffer
      sleep @dispatch_interval
    end
  end

  true
end

#stop_dispatcherObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hirefire/web.rb', line 37

def stop_dispatcher
  @mutex.synchronize do
    return false unless @dispatcher_running
    @dispatcher_running = false
  end

  @dispatcher.join(@dispatch_timeout)
  @dispatcher = nil

  flush_buffer

  logger.info "[HireFire] Web metrics dispatcher stopped."

  true
end