Module: PerformLater::Aliasing

Defined in:
lib/perform_later/aliasing.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/perform_later/aliasing.rb', line 6

def self.extended(base)
  base.class_eval do
    # methods configured for execution
    # through an asyncronous bus.
    #
    # example:
    #   { :do_work => {after_deserialize: :setup_from_async}}
    #
    # note: sidekiq has implementation of class_attribute
    class_attribute :perform_later_configs
    self.perform_later_configs = {}

    include Delegation
  end
end

Instance Method Details

#perform_later(method, opts = {}) ⇒ Object

configre a method to be performed asyncronously

ex:

class Foo

  def do_work
  end

  perform_later :do_work, after_deserialize: :setup_from_async

  private

  def setup_from_async(arg1, arg2)
    @obj1 = Parser.parse(arg1)
    @obj2 = Lookup.lookup(arg2)
  end
end


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/perform_later/aliasing.rb', line 39

def perform_later(method, opts={})
  config = opts.clone
  aliases = Array(config.delete(:as){ ["#{method}_later", "#{method}_async"] })
  self.perform_later_configs[method.to_s] = config

  entry_point = aliases.delete_at(0)
  define_singleton_method entry_point, ->(*args) do
    args = call_before_serialize(config[:before_serialize], args)
    perform_async(method, *args).tap do |id|
      logger.debug(Messages::EnqueuedMessage.new(self, method, id))
    end
  end

  aliases.each do | entry_point_alias |
    singleton_class.send(:alias_method, entry_point_alias, entry_point)
  end
end