Created
August 25, 2016 17:47
-
-
Save jonstorer/55b5758a42fbafc6906856f1210ca31c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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