aboutsummaryrefslogtreecommitdiffstats
path: root/activejob/lib/active_job/queue_priority.rb
diff options
context:
space:
mode:
authorwvengen <dev-rails@willem.engen.nl>2015-03-18 10:48:26 +0100
committerwvengen <dev-rails@willem.engen.nl>2015-09-17 22:17:39 +0200
commit7059ab35f797c163cd8907abcd7d0830b31e56f7 (patch)
tree708ccbf2da4412030b305f79c629a6961eb487c1 /activejob/lib/active_job/queue_priority.rb
parent61f9e47feac75a2cf4ed9e092bac036294564168 (diff)
downloadrails-7059ab35f797c163cd8907abcd7d0830b31e56f7.tar.gz
rails-7059ab35f797c163cd8907abcd7d0830b31e56f7.tar.bz2
rails-7059ab35f797c163cd8907abcd7d0830b31e56f7.zip
Add job priorities to ActiveJob
Diffstat (limited to 'activejob/lib/active_job/queue_priority.rb')
-rw-r--r--activejob/lib/active_job/queue_priority.rb44
1 files changed, 44 insertions, 0 deletions
diff --git a/activejob/lib/active_job/queue_priority.rb b/activejob/lib/active_job/queue_priority.rb
new file mode 100644
index 0000000000..01d84910ff
--- /dev/null
+++ b/activejob/lib/active_job/queue_priority.rb
@@ -0,0 +1,44 @@
+module ActiveJob
+ module QueuePriority
+ extend ActiveSupport::Concern
+
+ # Includes the ability to override the default queue priority.
+ module ClassMethods
+ mattr_accessor(:default_priority)
+
+ # 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.
+ def queue_with_priority(priority=nil, &block)
+ if block_given?
+ self.priority = block
+ else
+ self.priority = priority
+ end
+ end
+ end
+
+ included do
+ class_attribute :priority, instance_accessor: false
+
+ self.priority = default_priority
+ 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