Class: Quebert::Backend::Beanstalk

Inherits:
Object
  • Object
show all
Defined in:
lib/quebert/backend/beanstalk.rb

Overview

Manage jobs on a Beanstalk queue out of process

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, tube_name) ⇒ Beanstalk

Returns a new instance of Beanstalk.



8
9
10
# File 'lib/quebert/backend/beanstalk.rb', line 8

def initialize(host, tube_name)
  @host, @tube_name = host, tube_name
end

Class Method Details

.configure(opts = {}) ⇒ Object



47
48
49
50
# File 'lib/quebert/backend/beanstalk.rb', line 47

def self.configure(opts={})
  opts[:host] ||= ['127.0.0.1:11300']
  new(opts[:host], opts[:tube])
end

Instance Method Details

#drain!Object

For testing purposes… I think there’s a better way to do this though.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/quebert/backend/beanstalk.rb', line 34

def drain!
  while peek(:ready) do
    reserve_without_controller.delete
  end
  while peek(:delayed) do
    reserve_without_controller.delete
  end
  while peek(:buried) do
    tube.kick
    reserve_without_controller.delete
  end
end

#peek(state) ⇒ Object



29
30
31
# File 'lib/quebert/backend/beanstalk.rb', line 29

def peek(state)
  tube.peek state
end

#put(job, *args) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/quebert/backend/beanstalk.rb', line 12

def put(job, *args)
  priority, delay, ttr = args
  opts = {}
  opts[:pri]   = priority unless priority.nil?
  opts[:delay] = delay    unless delay.nil?
  opts[:ttr]   = ttr      unless ttr.nil?
  tube.put job.to_json, opts
end

#reserve(timeout = nil) ⇒ Object



25
26
27
# File 'lib/quebert/backend/beanstalk.rb', line 25

def reserve(timeout=nil)
  Controller::Beanstalk.new reserve_without_controller(timeout), self
end

#reserve_without_controller(timeout = nil) ⇒ Object



21
22
23
# File 'lib/quebert/backend/beanstalk.rb', line 21

def reserve_without_controller(timeout=nil)
  tube.reserve timeout
end