aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_job/enqueuing.rb
blob: 43bce125b5de8c5a62626396ad7da1dcd229f051 (plain) (blame)
1
2
3
4
5
6
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
40
41
42
43
44
require 'active_job/parameters'

module ActiveJob
  module Enqueuing
    ##
    # Push a job onto the queue.  The arguments must be legal JSON types
    # (string, int, float, nil, true, false, hash or array) or
    # ActiveModel::GlobalIdentication instances.  Arbitrary Ruby objects
    # are not supported.
    #
    # The return value is adapter-specific and may change in a future
    # ActiveJob release.
    def enqueue(*args)
      ActiveSupport::Notifications.instrument "enqueue.active_job", adapter: queue_adapter, job: self, params: args
      queue_adapter.queue self, *Parameters.serialize(args)
    end

    ##
    # Enqueue a job to be performed at +interval+ from now.
    #
    #   enqueue_in(1.week, "mike")
    #
    # Returns truthy if a job was scheduled.
    def enqueue_in(interval, *args)
      enqueue_at(interval.from_now, *args)
    end

    ##
    # Enqueue a job to be performed at an explicit point in time.
    #
    #   enqueue_at(Date.tomorrow.midnight, "mike")
    #
    # Returns truthy if a job was scheduled.
    def enqueue_at(timestamp, *args)
      ts = timestamp.to_f
      ActiveSupport::Notifications.instrument "enqueue_at.active_job", adapter: queue_adapter, timestamp: ts, job: self, params: args
      if Time.now.to_f > ts
        queue_adapter.queue self, *Parameters.serialize(args)
      else
        queue_adapter.queue_at self, ts, *Parameters.serialize(args)
      end
    end
  end
end