Sha256: b473689c7e9b0b6424751c1b2500660bd374a1b829822c4634c6771f0a03edea

Contents?: true

Size: 1.89 KB

Versions: 52

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true

module ActiveJob
  module QueueName
    extend ActiveSupport::Concern

    # Includes the ability to override the default queue name and prefix.
    module ClassMethods
      mattr_accessor :default_queue_name, default: "default"

      # Specifies the name of the queue to process the job on.
      #
      #   class PublishToFeedJob < ActiveJob::Base
      #     queue_as :feeds
      #
      #     def perform(post)
      #       post.to_feed!
      #     end
      #   end
      #
      # Can be given a block that will evaluate in the context of the job
      # so that a dynamic queue name can be applied:
      #
      #   class PublishToFeedJob < ApplicationJob
      #     queue_as do
      #       post = self.arguments.first
      #
      #       if post.paid?
      #         :paid_feeds
      #       else
      #         :feeds
      #       end
      #     end
      #
      #     def perform(post)
      #       post.to_feed!
      #     end
      #   end
      def queue_as(part_name = nil, &block)
        if block_given?
          self.queue_name = block
        else
          self.queue_name = queue_name_from_part(part_name)
        end
      end

      def queue_name_from_part(part_name) # :nodoc:
        queue_name = part_name || default_queue_name
        name_parts = [queue_name_prefix.presence, queue_name]
        -name_parts.compact.join(queue_name_delimiter)
      end
    end

    included do
      class_attribute :queue_name, instance_accessor: false, default: -> { self.class.default_queue_name }
      class_attribute :queue_name_delimiter, instance_accessor: false, default: "_"
      class_attribute :queue_name_prefix
    end

    # Returns the name of the queue the job will be run on.
    def queue_name
      if @queue_name.is_a?(Proc)
        @queue_name = self.class.queue_name_from_part(instance_exec(&@queue_name))
      end
      @queue_name
    end
  end
end

Version data entries

52 entries across 51 versions & 5 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/activejob-7.1.3.4/lib/active_job/queue_name.rb
activejob-8.0.1 lib/active_job/queue_name.rb
activejob-8.0.0.1 lib/active_job/queue_name.rb
activejob-7.2.2.1 lib/active_job/queue_name.rb
activejob-7.1.5.1 lib/active_job/queue_name.rb
activejob-7.0.8.7 lib/active_job/queue_name.rb
activejob-8.0.0 lib/active_job/queue_name.rb
activejob-7.2.2 lib/active_job/queue_name.rb
activejob-7.1.5 lib/active_job/queue_name.rb
activejob-8.0.0.rc2 lib/active_job/queue_name.rb
activejob-7.2.1.2 lib/active_job/queue_name.rb
activejob-7.1.4.2 lib/active_job/queue_name.rb
activejob-7.0.8.6 lib/active_job/queue_name.rb
activejob-8.0.0.rc1 lib/active_job/queue_name.rb
activejob-7.2.1.1 lib/active_job/queue_name.rb
activejob-7.1.4.1 lib/active_job/queue_name.rb
activejob-7.0.8.5 lib/active_job/queue_name.rb
activejob-8.0.0.beta1 lib/active_job/queue_name.rb
omg-activejob-8.0.0.alpha9 lib/active_job/queue_name.rb
omg-activejob-8.0.0.alpha8 lib/active_job/queue_name.rb