Module: Postburner::Callbacks::ClassMethods

Defined in:
app/concerns/postburner/callbacks.rb

Instance Method Summary collapse

Instance Method Details

#after_attempt(*filters, &blk) ⇒ Object

Defines a callback that will get called right after the job finishes and all metadata is persisted.

This isn’t called unless the attempt finishes without error, though this is subject to change in the future

class VideoProcessJob < Postburner::Base

  before_attempt do |job|
    $statsd.increment "attempt-complete-video-job.try"
  end

  def perform(video_id)
    Video.find(video_id).process
  end
end


96
97
98
# File 'app/concerns/postburner/callbacks.rb', line 96

def after_attempt(*filters, &blk)
  set_callback(:attempt, :after, *filters, &blk)
end

#after_enqueue(*filters, &blk) ⇒ Object

Defines a callback that will get called right after the job is enqueued.

class VideoProcessJob < Postburner::Base

  after_enqueue do |job|
    $statsd.increment "enqueue-video-job.success"
  end

  def perform(video_id)
    Video.find(video_id).process
  end
end


242
243
244
# File 'app/concerns/postburner/callbacks.rb', line 242

def after_enqueue(*filters, &blk)
  set_callback(:enqueue, :after, *filters, &blk)
end

#after_processed(*filters, &blk) ⇒ Object

Defines a callback that will get called right after the job’s processed_at timestamp has been persisted, marking the job as successfully completed, i.e. no errors raised.

This is rather similar to after_processing but all of the job’s metadata has been set.

class VideoProcessJob < Postburner::Base

  after_processed do |job|
    UserMailer.notify_video_processed(job.arguments.first)
  end

  def perform(video_id)
    Video.find(video_id).process
  end
end


206
207
208
# File 'app/concerns/postburner/callbacks.rb', line 206

def after_processed(*filters, &blk)
  set_callback(:processed, :after, *filters, &blk)
end

#after_processing(*filters, &blk) ⇒ Object

Defines a callback that will get called right after the job’s perform method has finished.

class VideoProcessJob < Postburner::Base

  after_processing do |job|
    UserMailer.notify_video_processed(job.arguments.first)
  end

  def perform(video_id)
    Video.find(video_id).process
  end
end


152
153
154
# File 'app/concerns/postburner/callbacks.rb', line 152

def after_processing(*filters, &blk)
  set_callback(:processing, :after, *filters, &blk)
end

#around_attempt(*filters, &blk) ⇒ Object

Defines a callback that will get called around the attempt of the job.

class VideoProcessJob < Postburner::Base

  around_enqueue do |job, block|
    $statsd.time "video-job.process" do
      block.call
    end
  end

  def perform(video_id)
    Video.find(video_id).process
  end
end


116
117
118
# File 'app/concerns/postburner/callbacks.rb', line 116

def around_attempt(*filters, &blk)
  set_callback(:attempt, :around, *filters, &blk)
end

#around_enqueue(*filters, &blk) ⇒ Object

Defines a callback that will get called around the enqueuing of the job.

class VideoProcessJob < Postburner::Base

  around_enqueue do |job, block|
    $statsd.time "video-job.process" do
      block.call
    end
  end

  def perform(video_id)
    Video.find(video_id).process
  end
end


262
263
264
# File 'app/concerns/postburner/callbacks.rb', line 262

def around_enqueue(*filters, &blk)
  set_callback(:enqueue, :around, *filters, &blk)
end

#around_processing(*filters, &blk) ⇒ Object

Defines a callback that will get called around the job’s perform method.

class VideoProcessJob < Postburner::Base

  around_processing do |job, block|
    UserMailer.notify_video_started_processing(job.arguments.first)
    block.call
    UserMailer.notify_video_processed(job.arguments.first)
  end

  def perform(video_id)
    Video.find(video_id).process
  end
end

You can access the return value of the job only if the execution wasn’t halted.

class VideoProcessJob < Postburner::Base
  around_processing do |job, block|
    value = block.call
    puts value # => "Hello World!"
  end

  def perform
    "Hello World!"
  end
end


184
185
186
# File 'app/concerns/postburner/callbacks.rb', line 184

def around_processing(*filters, &blk)
  set_callback(:processing, :around, *filters, &blk)
end

#before_attempt(*filters, &blk) ⇒ Object

Defines a callback that will get called right before the job starts and any metadata is persisted.

class VideoProcessJob < Postburner::Base

  before_attempt do |job|
    $statsd.increment "attempting-video-job.try"
  end

  def perform(video_id)
    Video.find(video_id).process
  end
end


75
76
77
# File 'app/concerns/postburner/callbacks.rb', line 75

def before_attempt(*filters, &blk)
  set_callback(:attempt, :before, *filters, &blk)
end

#before_enqueue(*filters, &blk) ⇒ Object

Defines a callback that will get called right before the job is enqueued.

class VideoProcessJob < Postburner::Base

  before_enqueue do |job|
    $statsd.increment "enqueue-video-job.try"
  end

  def perform(video_id)
    Video.find(video_id).process
  end
end


224
225
226
# File 'app/concerns/postburner/callbacks.rb', line 224

def before_enqueue(*filters, &blk)
  set_callback(:enqueue, :before, *filters, &blk)
end

#before_processing(*filters, &blk) ⇒ Object

Defines a callback that will get called right before the job’s perform method is executed.

class VideoProcessJob < Postburner::Base

  before_processing do |job|
    UserMailer.notify_video_started_processing(job.arguments.first)
  end

  def perform(video_id)
    Video.find(video_id).process
  end
end


134
135
136
# File 'app/concerns/postburner/callbacks.rb', line 134

def before_processing(*filters, &blk)
  set_callback(:processing, :before, *filters, &blk)
end

#inherited(klass) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/concerns/postburner/callbacks.rb', line 45

def inherited(klass)
  klass.get_callbacks(:enqueue).
    config[:skip_after_callbacks_if_terminated] =
      skip_after_callbacks_if_terminated
  klass.get_callbacks(:processing).
    config[:skip_after_callbacks_if_terminated] =
      skip_after_callbacks_if_terminated
  klass.get_callbacks(:attempt).
    config[:skip_after_callbacks_if_terminated] =
      skip_after_callbacks_if_terminated
  klass.get_callbacks(:processed).
    config[:skip_after_callbacks_if_terminated] =
      skip_after_callbacks_if_terminated
  super
end