Class: Franz::Output::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/franz/output/http.rb

Overview

HTTP output for Franz.

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ HTTP

Start a new output in the background. We’ll consume from the input queue and ship events via HTTP.

Parameters:

  • opts (Hash) (defaults to: {})

    options for the output

Options Hash (opts):

  • :input (Queue) — default: []

    “input” queue

  • :output (Queue) — default: []

    “output” configuration



20
21
22
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
72
73
74
# File 'lib/franz/output/http.rb', line 20

def initialize opts={}
  opts = {
    logger: Logger.new(STDOUT),
    tags: [],
    input: [],
    output: {
      server: 'http://localhost:3000',
      flush_size: 500,
      flush_interval: 10
    }
  }.deep_merge!(opts)

  @statz = opts[:statz] || Franz::Stats.new
  @statz.create :num_output, 0

  @logger = opts[:logger]

  @stop = false
  @foreground = opts[:foreground]

  server = opts[:output].delete :server
  @uri   = URI(server)
  open_uri

  @flush_size = opts[:output][:flush_size]
  @flush_interval = opts[:output][:flush_interval]
  @lock = Mutex.new
  @messages = []

  Thread.new do
    until @stop
      @lock.synchronize do
        flush_messages true
      end
      sleep @flush_interval
    end
  end

  @thread = Thread.new do
    until @stop
      event = JSON::generate(opts[:input].shift)
      @lock.synchronize do
        enqueue event
      end

      log.trace \
        event: 'publish',
        raw: event
    end
  end

  log.info event: 'output started'

  @thread.join if @foreground
end

Instance Method Details

#joinObject

Join the Output thread. Effectively only once.



78
79
80
81
82
# File 'lib/franz/output/http.rb', line 78

def join
  return if @foreground
  @foreground = true
  @thread.join
end

#stopObject

Stop the Output thread. Effectively only once.



86
87
88
89
90
91
# File 'lib/franz/output/http.rb', line 86

def stop
  return if @foreground
  @foreground = true
  @thread.kill
  log.info event: 'output stopped'
end