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


15
16
17
18
19
20
21
22
# File 'lib/quebert/controller/beanstalk.rb', line 15

def initialize(beanstalk_job)
  @beanstalk_job = beanstalk_job
  @job = Job.from_json(beanstalk_job.body)
rescue => e
  beanstalk_job.bury
  logger.error "Error caught on initialization. #{e.inspect}"
  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


24
25
26
27
28
29
30
31
32
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
# File 'lib/quebert/controller/beanstalk.rb', line 24

def perform
  logger.error(job) { "Performing #{job.class.name}." }
  logger.error(job) { "Beanstalk Job Stats: #{beanstalk_job.stats.inspect}" }

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

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