Class: Quebert::Job

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/quebert/job.rb

Defined Under Namespace

Classes: Priority

Constant Summary collapse

DEFAULT_JOB_DELAY =

Delay a job for 0 seconds on the jobqueue

0
DEFAULT_JOB_TTR =

By default, the job should live for 10 seconds tops.

10
Action =

Exceptions are used for signaling job status… ewww. Yank this out and replace with a more well thought out controller.

Class.new(Exception)
Bury =
Class.new(Action)
Delete =
Class.new(Action)
Release =
Class.new(Action)
Timeout =
Class.new(Action)
Retry =
Class.new(Action)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) {|_self| ... } ⇒ Job

Returns a new instance of Job.

Yields:

  • (_self)

Yield Parameters:

  • _self (Quebert::Job)

    the object that the method was called on



33
34
35
36
37
38
39
40
# File 'lib/quebert/job.rb', line 33

def initialize(*args)
  @priority = Job::Priority::MEDIUM
  @delay    = DEFAULT_JOB_DELAY
  @ttr      = DEFAULT_JOB_TTR
  @args     = args.dup.freeze
  yield self if block_given?
  self
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



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

def args
  @args
end

#delayObject

Returns the value of attribute delay.



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

def delay
  @delay
end

#priorityObject

Returns the value of attribute priority.



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

def priority
  @priority
end

#queueObject

Returns the value of attribute queue.



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

def queue
  @queue
end

#ttrObject

Returns the value of attribute ttr.



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

def ttr
  @ttr
end

Class Method Details

.backendObject



88
89
90
# File 'lib/quebert/job.rb', line 88

def self.backend
  @backend || Quebert.configuration.backend
end

.backend=(backend) ⇒ Object



80
81
82
# File 'lib/quebert/job.rb', line 80

def self.backend=(backend)
  @backend = backend
end

.from_json(json) ⇒ Object

Read a JSON string and convert into a hash that Ruby can deal with.



74
75
76
77
78
# File 'lib/quebert/job.rb', line 74

def self.from_json(json)
  if hash = JSON.parse(json) and not hash.empty?
    Serializer::Job.deserialize(hash)
  end
end

Instance Method Details

#around_buryObject

Event hook that can be overridden



93
94
95
# File 'lib/quebert/job.rb', line 93

def around_bury
  yield
end

#around_deleteObject

Event hook that can be overridden



103
104
105
# File 'lib/quebert/job.rb', line 103

def around_delete
  yield
end

#around_releaseObject

Event hook that can be overridden



98
99
100
# File 'lib/quebert/job.rb', line 98

def around_release
  yield
end

#backendObject



84
85
86
# File 'lib/quebert/job.rb', line 84

def backend
  self.class.backend
end

#enqueue(override_opts = {}) ⇒ Object

Accepts arguments that override the job options and enqueu this stuff.



63
64
65
66
# File 'lib/quebert/job.rb', line 63

def enqueue(override_opts={})
  override_opts.each { |opt, val| self.send("#{opt}=", val) }
  backend.put(self)
end

#perform(*args) ⇒ Object

Raises:

  • (NotImplementedError)


42
43
44
# File 'lib/quebert/job.rb', line 42

def perform(*args)
  raise NotImplementedError
end

#perform!Object

Runs the perform method that somebody else should be implementing



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/quebert/job.rb', line 47

def perform!
  Quebert.config.before_job(self)
  Quebert.config.around_job(self)

  # Honor the timeout and kill the job in ruby-space. Beanstalk
  # should be cleaning up this job and returning it to the queue
  # as well.
  val = ::Timeout.timeout(ttr, Job::Timeout){ perform(*args) }

  Quebert.config.around_job(self)
  Quebert.config.after_job(self)

  val
end

#to_jsonObject

Serialize the job into a JSON string that we can put on the beandstalkd queue.



69
70
71
# File 'lib/quebert/job.rb', line 69

def to_json
  JSON.generate(Serializer::Job.serialize(self))
end