Skip to content

Instantly share code, notes, and snippets.

@dmcclory
Forked from brandonhilkert/sucker_punch.rb
Last active December 19, 2015 04:39
Show Gist options
  • Save dmcclory/5898473 to your computer and use it in GitHub Desktop.
Save dmcclory/5898473 to your computer and use it in GitHub Desktop.
use a method_added callback to realias the methods after the base class version of #perform is defined.
require 'celluloid'
require 'pry'
module SuckerPunch
module Job
def self.included(base)
base.send(:include, ::Celluloid)
base.class_eval do
# set up a callback to SuckerPunch::Job
# whenever a method is defined on the base class
def self.method_added(m)
SuckerPunch::Job.realias_methods(self) if m == :perform
end
end
end
def perform_with_pool_check(*args, &block)
define_celluloid_pool
perform_without_pool_check(*args, &block)
end
private
def self.realias_methods(base)
# the icky bit prevents an infinite loop
# otherwise aliasing :perform_with_pool_check to :perform
# counts will trigger another call to method_added
if !@icky_bit
@icky_bit = true
base.class_eval do
alias_method :perform_without_pool_check, :perform
alias_method :perform, :perform_with_pool_check
end
remove_instance_variable :@icky_bit
end
end
def define_celluloid_pool
unless SuckerPunch::Queues.registered?(queue_name)
Celluloid::Actor[queue_name] = self.class.send(:pool)
SuckerPunch::Queues.register(queue_name)
end
end
def queue_name
self.class.to_s.underscore.to_sym
end
end
end
class EmailJob
include SuckerPunch::Job
def perform(user_id)
UserMailer.welcome(user_id).deliver
end
binding.pry
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment