Class: Quebert::Controller::Beanstalk

Inherits:
Base
  • Object
show all
Includes:
Logging
Defined in:
lib/quebert/controller/beanstalk.rb

Overview

Handle interactions between a job and a Beanstalk queue.

Constant Summary collapse

MAX_TIMEOUT_RETRY_DELAY =
300
TIMEOUT_RETRY_DELAY_SEED =
2
TIMEOUT_RETRY_GROWTH_RATE =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(beanstalk_job, queue) ⇒ Beanstalk

Returns a new instance of Beanstalk.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/quebert/controller/beanstalk.rb', line 15

def initialize(beanstalk_job, queue)
  @beanstalk_job, @queue = beanstalk_job, queue

  begin
    @job = Job.from_json(beanstalk_job.body)
  rescue Job::Delete
    beanstalk_job.delete
    log "Deleted on initialization", :error
  rescue Job::Release
    beanstalk_job.release @job.priority, @job.delay
    log "Released on initialization with priority: #{@job.priority} and delay: #{@job.delay}", :error
  rescue Job::Bury
    beanstalk_job.bury
    log "Buried on initialization", :error
  rescue Exception => e
    beanstalk_job.bury
    log "Exception caught on initialization. #{e.inspect}", :error
    raise e  
  end
end

Instance Attribute Details

#beanstalk_jobObject (readonly)

Returns the value of attribute beanstalk_job.



9
10
11
# File 'lib/quebert/controller/beanstalk.rb', line 9

def beanstalk_job
  @beanstalk_job
end

#jobObject (readonly)

Returns the value of attribute job.



9
10
11
# File 'lib/quebert/controller/beanstalk.rb', line 9

def job
  @job
end

#queueObject (readonly)

Returns the value of attribute queue.



9
10
11
# File 'lib/quebert/controller/beanstalk.rb', line 9

def queue
  @queue
end

Instance Method Details

#performObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/quebert/controller/beanstalk.rb', line 36

def perform
  begin
    log "Performing with args #{job.args.inspect}"
    log "Beanstalk Job Stats: #{beanstalk_job.stats.inspect}"

    result = false
    time = Benchmark.realtime do
      result = job.perform!
      beanstalk_job.delete
    end

    log "Completed in #{(time*1000*1000).to_i/1000.to_f} ms\n"
    result
  rescue Job::Delete
    beanstalk_job.delete
    log "Deleted", :error
  rescue Job::Release
    beanstalk_job.release @job.priority, @job.delay
    log "Released with priority: #{@job.priority} and delay: #{@job.delay}", :error 
  rescue Job::Bury
    beanstalk_job.bury
    log "Burried", :error
  rescue Job::Timeout => e
    retry_with_delay
    raise e
  rescue Exception => e
    beanstalk_job.bury
    log "Exception caught on perform. Job buried. #{e.inspect}", :error
    raise e
  end
end