Module: Delayed::MessageSending

Defined in:
lib/delayed/message_sending.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#send_at(time, method, *args) ⇒ Object



45
46
47
48
# File 'lib/delayed/message_sending.rb', line 45

def send_at(time, method, *args)
  send_later_enqueue_args(method,
                      { :run_at => time }, *args)
end

#send_at_with_queue(time, method, queue, *args) ⇒ Object



50
51
52
53
54
# File 'lib/delayed/message_sending.rb', line 50

def send_at_with_queue(time, method, queue, *args)
  send_later_enqueue_args(method,
                      { :run_at => time, :queue => queue },
                      *args)
end

#send_later(method, *args) ⇒ Object



3
4
5
# File 'lib/delayed/message_sending.rb', line 3

def send_later(method, *args)
  send_later_enqueue_args(method, {}, *args)
end

#send_later_enqueue_args(method, enqueue_args = {}, *args) ⇒ Object



7
8
9
10
11
12
13
14
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
# File 'lib/delayed/message_sending.rb', line 7

def send_later_enqueue_args(method, enqueue_args = {}, *args)
  enqueue_args = enqueue_args.dup
  # support procs/methods as enqueue arguments
  enqueue_args.each do |k,v|
    if v.respond_to?(:call)
      enqueue_args[k] = v.call(self)
    end
  end

  no_delay = enqueue_args.delete(:no_delay)
  on_failure = enqueue_args.delete(:on_failure)
  on_permanent_failure = enqueue_args.delete(:on_permanent_failure)
  if !no_delay
    # delay queuing up the job in another database until the results of the current
    # transaction are visible
    connection = self.class.connection if self.class.respond_to?(:connection)
    connection ||= self.connection if 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(self, method.to_sym, args,
                                                            on_failure, on_permanent_failure), enqueue_args)
      end
      return nil
    end
  end

  result = Delayed::Job.enqueue(Delayed::PerformableMethod.new(self, method.to_sym, args,
                                                               on_failure, on_permanent_failure), enqueue_args)
  result = nil unless no_delay
  result
end

#send_later_if_production(*args) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/delayed/message_sending.rb', line 65

def send_later_if_production(*args)
  if Rails.env.production?
    send_later(*args)
  else
    send(*args)
  end
end

#send_later_if_production_enqueue_args(method, enqueue_args, *args) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/delayed/message_sending.rb', line 73

def send_later_if_production_enqueue_args(method, enqueue_args, *args)
  if Rails.env.production?
    send_later_enqueue_args(method, enqueue_args, *args)
  else
    send(method, *args)
  end
end

#send_later_unless_in_job(method, *args) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/delayed/message_sending.rb', line 56

def send_later_unless_in_job(method, *args)
  if Delayed::Job.in_delayed_job?
    send(method, *args)
  else
    send_later(method, *args)
  end
  nil # can't rely on the type of return value, so return nothing
end

#send_later_with_queue(method, queue, *args) ⇒ Object



41
42
43
# File 'lib/delayed/message_sending.rb', line 41

def send_later_with_queue(method, queue, *args)
  send_later_enqueue_args(method, { :queue => queue }, *args)
end

#send_now_or_later(_when, *args) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/delayed/message_sending.rb', line 81

def send_now_or_later(_when, *args)
  if _when == :now
    send(*args)
  else
    send_later(*args)
  end
end

#send_now_or_later_if_production(_when, *args) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/delayed/message_sending.rb', line 89

def send_now_or_later_if_production(_when, *args)
  if _when == :now
    send(*args)
  else
    send_later_if_production(*args)
  end
end