Class: BarnyardHarvester::GenericQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/barnyard_harvester/generic_queue.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ GenericQueue

Returns a new instance of GenericQueue.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/barnyard_harvester/generic_queue.rb', line 5

def initialize(args)
  @queueing = args.fetch(:queueing) { raise "You must provide :queueing" }

  case @queueing
    when :sqs
      require "aws-sdk"
      @sqs_settings = args.fetch(:sqs_settings) { raise "You must provide :sqs_settings" }
      @sqs = AWS::SQS.new(@sqs_settings)
    when :rabbitmq
      require "bunny"
      @rabbitmq_settings = args.fetch(:rabbitmq_settings) { raise "You must provide :rabbitmq_settings" }
      @rabbitmq_settings[:logging] = true if @debug
      @bunny = Bunny.new(@rabbitmq_settings)
      @bunny.start
    when :hash
      @queues = Hash.new
    else
      raise "Unknown queueing method. #{@queuing}"
  end

end

Instance Method Details

#empty(queue_name) ⇒ Object



66
67
68
69
# File 'lib/barnyard_harvester/generic_queue.rb', line 66

def empty(queue_name)
  while pop(queue_name)
  end
end

#pop(queue_name) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/barnyard_harvester/generic_queue.rb', line 41

def pop(queue_name)
  case @queueing
    when :sqs
      msg = @sqs.queues.create(queue_name).receive_message
      unless msg.nil?
        msg.delete
        msg.body
      else
        nil
      end

    when :rabbitmq
      msg = @bunny.queue(queue_name).pop[:payload]
      if msg == :queue_empty
        return nil
      else
        msg
      end
    when :hash
      msg = @queue.pop
      File.open("#{queue_name}.yml", "w") { |file| file.puts(@queues[queue_name].to_yaml) }
      msg
  end
end

#push(queue_name, message) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/barnyard_harvester/generic_queue.rb', line 27

def push(queue_name, message)
  case @queueing
    when :sqs
      queue = @sqs.queues.create(queue_name)
      queue.send_message(message)
    when :rabbitmq
      @bunny.queue(queue_name).publish(message)
    when :hash
      @queues[queue_name] = Array.new unless @queues.has_key?(queue_name)
      @queues[queue_name] << message
      File.open("#{queue_name}.yml", "w") { |file| file.puts(@queues[queue_name].to_yaml) }
  end
end