Skip to content

Instantly share code, notes, and snippets.

@jonstorer
Created August 25, 2016 17:47
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 jonstorer/55b5758a42fbafc6906856f1210ca31c to your computer and use it in GitHub Desktop.
Save jonstorer/55b5758a42fbafc6906856f1210ca31c to your computer and use it in GitHub Desktop.
require 'active_support/concern'
module Asyncable
extend ActiveSupport::Concern
ASYNC_REGEX = Regexp.new(/^async_(.+)$/)
included do
include Sidekiq::Worker
def method_missing(method, *args)
if method.to_s =~ ASYNC_REGEX && respond_to?(method)
self.class.send(:define_method, method) do |*arguments|
Sidekiq::Client.enqueue(self.class, self._id, $1, *arguments)
end
self.send(method, *args)
else
super
end
end
def respond_to?(method, *args)
if method.to_s =~ ASYNC_REGEX
super($1, true)
else
super(method, *args)
end
end
def perform(id, *args)
begin
self.class.find(id).send(*args)
rescue Exception => e
Raygun.track_exception(e)
raise e
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment