Skip to content

Instantly share code, notes, and snippets.

@jgomo3
Created April 12, 2019 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgomo3/f29967f46bea2ac7758b8371ca3a450a to your computer and use it in GitHub Desktop.
Save jgomo3/f29967f46bea2ac7758b8371ca3a450a to your computer and use it in GitHub Desktop.
One way to wrap mundane things to a Sidekiq Worker perform method
module Notification
def self.publisher_worker_class(klass)
Class.new do
include(Sidekiq::Worker, Wisper::Publisher)
sidekiq_options(:queue => :report)
define_method :perform do |*args, **kwargs|
success = false
begin
context = klass.new.perform(*args, **kwargs)
broadcast(:deliveried_report_delivery, context)
success = true
rescue => e
broadcast(:failed_report_delivery, context)
ensure
raise e unless success
end
end
end
end
end
# Alternativally, I used inheritance, making the subclasses to define a new method (perform2 or something).
# The superclass is the only one implementing the "perform" method, wrapping the call to the "perform2" method.
# Another one: Decorator.
#
# The main porpuse of saving this, is to remember the metaprogramming part, more than the actual problem,
# which I actually resolved diferently.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment