Skip to content

Instantly share code, notes, and snippets.

@coreyhaines
Forked from brandonhilkert/sucker_punch.rb
Last active December 19, 2015 05:39
Show Gist options
  • Save coreyhaines/5905791 to your computer and use it in GitHub Desktop.
Save coreyhaines/5905791 to your computer and use it in GitHub Desktop.
class EmailJob
include SuckerPunch::Job
def perform(user_id)
UserMailer.welcome(user_id).deliver
end
end
module SuckerPunch
module Job
def self.included(base)
base.send(:include, ::Celluloid)
end
# we want perform_with_pool_check to define the pool before calling #perform
def perform_with_pool_check(*args, &block)
define_celluloid_pool
perform(*args, &block)
end
# we want perform_without_pool_check just be a pass-through to perform #perform
# this probably could be removed if the alias for it was just a convenience
def perform_without_pool_check(*args, &block)
perform(*args, &block)
end
# this is what the subclass is expected to define
# should always include this in the super class, so the subclass knows what the contract is
def perform(*args, &block)
raise "Implement #perform to define work needed by this job"
end
private
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment