2017-07-09 13:49:52 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:40:03 -04:00
|
|
|
|
2015-03-18 05:48:26 -04:00
|
|
|
module ActiveJob
|
|
|
|
module QueuePriority
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
# Includes the ability to override the default queue priority.
|
|
|
|
module ClassMethods
|
2017-05-31 05:16:20 -04:00
|
|
|
mattr_accessor :default_priority
|
2015-03-18 05:48:26 -04:00
|
|
|
|
|
|
|
# Specifies the priority of the queue to create the job with.
|
|
|
|
#
|
|
|
|
# class PublishToFeedJob < ActiveJob::Base
|
|
|
|
# queue_with_priority 50
|
|
|
|
#
|
|
|
|
# def perform(post)
|
|
|
|
# post.to_feed!
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# Specify either an argument or a block.
|
2016-10-28 23:05:58 -04:00
|
|
|
def queue_with_priority(priority = nil, &block)
|
2015-03-18 05:48:26 -04:00
|
|
|
if block_given?
|
|
|
|
self.priority = block
|
|
|
|
else
|
|
|
|
self.priority = priority
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
included do
|
2017-05-29 12:01:50 -04:00
|
|
|
class_attribute :priority, instance_accessor: false, default: default_priority
|
2015-03-18 05:48:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the priority that the job will be created with
|
|
|
|
def priority
|
|
|
|
if @priority.is_a?(Proc)
|
|
|
|
@priority = instance_exec(&@priority)
|
|
|
|
end
|
|
|
|
@priority
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|