Class: Delayed::MessageSending::DelayProxy

Inherits:
BasicObject
Defined in:
lib/delayed/message_sending.rb

Instance Method Summary collapse

Constructor Details

#initialize(object, synchronous: false, sender: nil, **enqueue_args) ⇒ DelayProxy

Returns a new instance of DelayProxy.



8
9
10
11
12
13
# File 'lib/delayed/message_sending.rb', line 8

def initialize(object, synchronous: false, sender: nil, **enqueue_args)
  @object = object
  @enqueue_args = enqueue_args
  @synchronous = synchronous
  @sender = sender
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, **kwargs) ⇒ Object

rubocop:disable Style/MissingRespondToMissing



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/delayed/message_sending.rb', line 15

def method_missing(method, *args, **kwargs) # rubocop:disable Style/MissingRespondToMissing
  # method doesn't exist? must be method_missing; assume private access
  @sender = nil if !@sender.nil? &&
                   @object.methods.exclude?(method) &&
                   @object.protected_methods.exclude?(method) &&
                   @object.private_methods.exclude?(method)

  sender_is_object = @sender.equal?(@object)
  sender_is_class = @sender.is_a?(@object.class)

  # even if the call is async, if the call is _going_ to generate an error, we make it synchronous
  # so that the error is generated immediately, instead of waiting for it to fail in a job,
  # which might go unnoticed
  if !@sender.nil? && !@synchronous
    @synchronous = true if !sender_is_object && @object.private_methods.include?(method)
    @synchronous = true if !sender_is_class && @object.protected_methods.include?(method)
  end

  if @synchronous
    if @sender.nil? || sender_is_object || (sender_is_class && @object.protected_methods.include?(method))
      return @object.send(method, *args) if kwargs.empty?

      return @object.send(method, *args, **kwargs)
    end

    return @object.public_send(method, *args) if kwargs.empty?

    return @object.public_send(method, *args, **kwargs)
  end

  ignore_transaction = @enqueue_args.delete(:ignore_transaction)
  on_failure = @enqueue_args.delete(:on_failure)
  on_permanent_failure = @enqueue_args.delete(:on_permanent_failure)
  unless ignore_transaction
    # delay queuing up the job in another database until the results of the current
    # transaction are visible
    connection = @object.class.connection if @object.class.respond_to?(:connection)
    connection ||= @object.connection if @object.respond_to?(:connection)
    connection ||= ::ActiveRecord::Base.connection

    if ::Delayed::Job != ::Delayed::Backend::ActiveRecord::Job || connection != ::Delayed::Job.connection
      connection.after_transaction_commit do
        ::Delayed::Job.enqueue(::Delayed::PerformableMethod.new(@object,
                                                                method,
                                                                args: args,
                                                                kwargs: kwargs,
                                                                on_failure: on_failure,
                                                                on_permanent_failure: on_permanent_failure,
                                                                sender: @sender),
                               **@enqueue_args)
      end
      return nil
    end
  end

  result = ::Delayed::Job.enqueue(::Delayed::PerformableMethod.new(@object,
                                                                   method,
                                                                   args: args,
                                                                   kwargs: kwargs,
                                                                   on_failure: on_failure,
                                                                   on_permanent_failure: on_permanent_failure,
                                                                   sender: @sender),
                                  **@enqueue_args)
  result = nil unless ignore_transaction
  result
end