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



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
67
68
69
70
71
72
73
74
75
76
# 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
    log "Deleting job", :error
    beanstalk_job.delete
    log "Job deleted", :error
  rescue Job::Release
    log "Releasing with priority: #{@job.priority} and delay: #{@job.delay}", :error 
    beanstalk_job.release :pri => @job.priority, :delay => @job.delay
    log "Job released", :error 
  rescue Job::Bury
    log "Burrying job", :error
    beanstalk_job.bury
    log "Job burried", :error
  rescue Job::Timeout => e
    log "Job timed out!", :error
    retry_with_delay
    raise e
  rescue Job::Retry => e
    # The difference between the Retry and Timeout class is that
    # Retry does not log an exception where as Timeout does
    log "Manually retrying with delay"
    retry_with_delay
  rescue Exception => e
    log "Exception caught on perform. Burying job. #{e.inspect} #{e.backtrace.join("\n")}", :error
    beanstalk_job.bury
    log "Job buried", :error
    raise e
  end
end