Module: Promiscuous::AMQP::File::Subscriber

Defined in:
lib/promiscuous/amqp/file.rb

Defined Under Namespace

Classes: Metadata

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#lockObject

Returns the value of attribute lock.



21
22
23
# File 'lib/promiscuous/amqp/file.rb', line 21

def lock
  @lock
end

#num_pendingObject

Returns the value of attribute num_pending.



21
22
23
# File 'lib/promiscuous/amqp/file.rb', line 21

def num_pending
  @num_pending
end

#prefetch_waitObject

Returns the value of attribute prefetch_wait.



21
22
23
# File 'lib/promiscuous/amqp/file.rb', line 21

def prefetch_wait
  @prefetch_wait
end

Instance Method Details

#disconnectObject



64
65
66
# File 'lib/promiscuous/amqp/file.rb', line 64

def disconnect
  @stop = true
end

#recoverObject



61
62
# File 'lib/promiscuous/amqp/file.rb', line 61

def recover
end

#subscribe(options = {}, &block) ⇒ Object



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
# File 'lib/promiscuous/amqp/file.rb', line 23

def subscribe(options={}, &block)
  file_name, worker_index, num_workers = Promiscuous::Config.subscriber_amqp_url.split(':')

  worker_index = worker_index.to_i
  num_workers = num_workers.to_i

  file = File.open(file_name, 'r')

  @prefetch = Promiscuous::Config.prefetch
  @num_pending = 0
  @lock = Mutex.new
  @prefetch_wait = ConditionVariable.new

  @thread = Thread.new do
    file.each_with_index do |line, i|
      if num_workers > 0
        next if ((i+worker_index) % num_workers) != 0
      end

      return if @stop

      @lock.synchronize do
        @prefetch_wait.wait(@lock) until @num_pending < @prefetch
        @num_pending += 1
      end

      block.call(Metadata.new(self), line.chomp)
    end

    @lock.synchronize do
      @prefetch_wait.wait(@lock) until @num_pending == 0
    end

    # will shutdown the CLI gracefully
    Process.kill("SIGTERM", Process.pid)
  end
end