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) ⇒ Beanstalk

Returns a new instance of Beanstalk.



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

def initialize(beanstalk_job)
  @beanstalk_job = beanstalk_job
  @job = Job.from_json(beanstalk_job.body)
rescue Job::Delete
  beanstalk_job.delete
  job_log "Deleted on initialization", :error
rescue Job::Release
  beanstalk_job.release job.priority, job.delay
  job_log "Released on initialization with priority: #{job.priority} and delay: #{job.delay}", :error
rescue Job::Bury
  beanstalk_job.bury
  job_log "Buried on initialization", :error
rescue => e
  beanstalk_job.bury
  job_log "Error caught on initialization. #{e.inspect}", :error
  raise
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

Instance Method Details

#performObject



33
34
35
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
# File 'lib/quebert/controller/beanstalk.rb', line 33

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

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

  job_log "Completed in #{(time*1000*1000).to_i/1000.to_f} ms\n"
  result
rescue Job::Delete
  job_log "Deleting job", :error
  beanstalk_job.delete
  job_log "Job deleted", :error
rescue Job::Release
  job_log "Releasing with priority: #{job.priority} and delay: #{job.delay}", :error
  beanstalk_job.release :pri => job.priority, :delay => job.delay
  job_log "Job released", :error
rescue Job::Bury
  job_log "Burrying job", :error
  beanstalk_job.bury
  job_log "Job burried", :error
rescue Job::Timeout => e
  job_log "Job timed out. Retrying with delay. #{e.inspect} #{e.backtrace.join("\n")}", :error
  retry_with_delay
  raise
rescue Job::Retry
  # The difference between the Retry and Timeout class is that
  # Retry does not job_log an exception where as Timeout does
  job_log "Manually retrying with delay"
  retry_with_delay
rescue => e
  job_log "Error caught on perform. Burying job. #{e.inspect} #{e.backtrace.join("\n")}", :error
  beanstalk_job.bury
  job_log "Job buried", :error
  raise
end