Class: RockQueue::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rock-queue.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter, *options) ⇒ Base

Initializes the whole thing and makes the connection to the queueing server using selected adapter (passed as lowercased symbol)



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rock-queue.rb', line 32

def initialize(adapter, *options)
  # Any better way to do this? :-)
  options = options.first
  if options.include?(:server) && options.include?(:port)
    case adapter
      when :beanstalkd
        @adapter = Beanstalkd.new(options)
      when :resque
        @adapter = ResqueQueue.new(options)
      when :delayed_job
        @adapter = DelayedJob.new(options)
    end
  else
    raise ArgumentError
  end

  # Initialize logger
  @@logger = Logger.new(options[:log].nil? ? STDOUT : options[:log])
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

Calling adapter method



74
75
76
# File 'lib/rock-queue.rb', line 74

def method_missing(sym, *args, &block)
  @adapter.send sym, *args, &block
end

Class Method Details

.loggerObject

Returns Rock Queue logger



79
80
81
# File 'lib/rock-queue.rb', line 79

def self.logger
  @@logger
end

Instance Method Details

#push(value, *args) ⇒ Object

Pushes the value (in our case it should always be an object) onto the queue using previously selected adapter



55
56
57
# File 'lib/rock-queue.rb', line 55

def push(value, *args)
  @adapter.push(value, args)
end

#receiveObject

Pulls the data off the queue. There are two ways to do so:

  • Call receive with no block (gets you a single item)

  • Pass a block to it (creates and endles loop that constantly pulls items from the queue as they become available)

All calls to the queueing server are made through the previosuly selected adaper.



64
65
66
67
68
69
70
71
# File 'lib/rock-queue.rb', line 64

def receive
  if block_given?
    obj, args = @adapter.pop
    yield QueueObject.new(obj, args) if obj
  else
    raise 'No block given'
  end
end