Class: SuperPoller::StarlingQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/super_poller/starling_queue.rb

Instance Method Summary collapse

Constructor Details

#initialize(queue_name, *args) ⇒ StarlingQueue

Returns a new instance of StarlingQueue.



5
6
7
8
# File 'lib/super_poller/starling_queue.rb', line 5

def initialize(queue_name, *args)
  @queue_name = queue_name.to_s
  @queue = Starling.new(*args)
end

Instance Method Details

#any?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/super_poller/starling_queue.rb', line 30

def any?
  !empty?
end

#coerce(message) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/super_poller/starling_queue.rb', line 55

def coerce(message)
  unless message.start_with?('{')
    message = MultiJson.dump(Marshal.load(message))
  end
  message = fix_encoding(message)
  MultiJson.decode(message)
end

#empty?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/super_poller/starling_queue.rb', line 26

def empty?
  length.zero?
end

#fetchObject



18
19
20
# File 'lib/super_poller/starling_queue.rb', line 18

def fetch
  coerce @queue.fetch(@queue_name, true)
end

#fix_encoding(v) ⇒ Object



46
47
48
# File 'lib/super_poller/starling_queue.rb', line 46

def fix_encoding(v)
  v.force_encoding('UTF-8')
end

#flushObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/super_poller/starling_queue.rb', line 34

def flush
  @queue.delete(@queue_name)
rescue MemCache::MemCacheError => e
  if e.message =~ /bad command line format/
    STDERR.puts "WARNING: Server could not handle delete command. Falling back to the MUCH slower, and quite unreliable flush method. Consider using Reevoo Starling (http://github.com/reevoo/starling)"
    @queue.flush(@queue_name)
  else
    raise
  end
end

#lengthObject



22
23
24
# File 'lib/super_poller/starling_queue.rb', line 22

def length
  @queue.sizeof(@queue_name)
end

#popObject



10
11
12
# File 'lib/super_poller/starling_queue.rb', line 10

def pop
  coerce @queue.get(@queue_name, true)
end

#push(v, raw = false) ⇒ Object



14
15
16
# File 'lib/super_poller/starling_queue.rb', line 14

def push(v, raw = false)
  @queue.set(@queue_name, v, 0, raw)
end