resque-priority 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.md +39 -0
- data/Rakefile +24 -0
- data/lib/resque/plugins/priority.rb +45 -0
- data/resque-priority.gemspec +43 -0
- metadata +68 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Brett Buddin
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
Software), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
Resque Priority
|
2
|
+
================
|
3
|
+
|
4
|
+
Provides Resque with three levels of (named) priority for a single queue.
|
5
|
+
|
6
|
+
For example:
|
7
|
+
|
8
|
+
require 'resque/plugins/priority'
|
9
|
+
|
10
|
+
class Job
|
11
|
+
extend Resque::Plugins::Priority
|
12
|
+
|
13
|
+
@queue = :primary
|
14
|
+
|
15
|
+
def self.perform(record)
|
16
|
+
puts @queue #=> :primary_low
|
17
|
+
puts @priority #=> :low
|
18
|
+
other_stuff_and_junk
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Enqueuing the job
|
23
|
+
Resque.enqueue_with_priority(:low, Job, params)
|
24
|
+
|
25
|
+
The above code creates a new Job in the `:primary_low` queue. There are three variations on the queues:
|
26
|
+
|
27
|
+
* `:high`
|
28
|
+
* `:normal`
|
29
|
+
* `:low`
|
30
|
+
|
31
|
+
These three priorities produce queue names like, assuming `@queue` is set to "queuename", like:
|
32
|
+
|
33
|
+
* `:queuename_high`
|
34
|
+
* `:queuename`
|
35
|
+
* `:queuename_low`
|
36
|
+
|
37
|
+
A Resque worker would then use a `QUEUE` variable like: `QUEUE=queuename_high,queuename,queuename_low`
|
38
|
+
|
39
|
+
Your Job `self.perform` methods have have two instance variables available to them: `@queue` and `@priority`. `@queue` is nothing new, it's provided by Resque by default, but Priority has appended the current priority to the existing value; resulting in the actual queue the job originated from (e.g. ":queuename_high"). `@priority` provides you with the priority (e.g. ":high") of the current job execution.
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
begin
|
2
|
+
require "jeweler"
|
3
|
+
|
4
|
+
Jeweler::Tasks.new do |gem|
|
5
|
+
gem.name = "resque-priority"
|
6
|
+
gem.version = "0.1"
|
7
|
+
gem.summary = "Resque plugin that provides priority levels"
|
8
|
+
gem.description = <<-desc
|
9
|
+
Provides Resque with three levels of (named) priority for a single queue.
|
10
|
+
desc
|
11
|
+
gem.email = "brett@intraspirit.net"
|
12
|
+
gem.homepage = "http://github.com/brettbuddin/resque-priority"
|
13
|
+
gem.date = Time.now.strftime('%Y-%m-%d')
|
14
|
+
gem.authors = ["Brett Buddin"]
|
15
|
+
gem.files = %w( README.md Rakefile LICENSE )
|
16
|
+
gem.files += Dir["*", "{lib}/**/*"]
|
17
|
+
|
18
|
+
gem.has_rdoc = false
|
19
|
+
end
|
20
|
+
|
21
|
+
Jeweler::GemcutterTasks.new
|
22
|
+
rescue LoadError
|
23
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
24
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Resque
|
2
|
+
def self.enqueue_with_priority(priority, klass, *args)
|
3
|
+
queue = klass.instance_variable_get(:@queue) || (klass.respond_to?(:queue) and klass.queue)
|
4
|
+
|
5
|
+
if [:high, :low].include?(priority.to_sym)
|
6
|
+
queue = "#{queue}_#{priority}".to_sym
|
7
|
+
end
|
8
|
+
|
9
|
+
klass.priority = priority
|
10
|
+
Resque::Job.create(queue, klass, *args)
|
11
|
+
end
|
12
|
+
|
13
|
+
module Plugins
|
14
|
+
module Priority
|
15
|
+
def priority=(p)
|
16
|
+
@priority = p.to_sym
|
17
|
+
end
|
18
|
+
|
19
|
+
def priority_identifier(*args)
|
20
|
+
args.join('-')
|
21
|
+
end
|
22
|
+
|
23
|
+
def priority_key(*args)
|
24
|
+
['priority', 'name', priority_identifier(*args)].compact.join(':')
|
25
|
+
end
|
26
|
+
|
27
|
+
def after_enqueue_set_priority(*args)
|
28
|
+
Resque.redis.setnx(priority_key(*args), @priority)
|
29
|
+
end
|
30
|
+
|
31
|
+
def around_perform_retrieve_priority(*args)
|
32
|
+
key = priority_key(*args)
|
33
|
+
priority = Resque.redis.get(key)
|
34
|
+
self.priority = priority && priority.empty? ? :normal : priority
|
35
|
+
|
36
|
+
begin
|
37
|
+
yield
|
38
|
+
ensure
|
39
|
+
Resque.redis.del(key)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{resque-priority}
|
8
|
+
s.version = "0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Brett Buddin"]
|
12
|
+
s.date = %q{2010-10-01}
|
13
|
+
s.description = %q{ Provides Resque with three levels of (named) priority for a single queue.
|
14
|
+
}
|
15
|
+
s.email = %q{brett@intraspirit.net}
|
16
|
+
s.extra_rdoc_files = [
|
17
|
+
"LICENSE",
|
18
|
+
"README.md"
|
19
|
+
]
|
20
|
+
s.files = [
|
21
|
+
"LICENSE",
|
22
|
+
"README.md",
|
23
|
+
"Rakefile",
|
24
|
+
"lib/resque/plugins/priority.rb",
|
25
|
+
"resque-priority.gemspec"
|
26
|
+
]
|
27
|
+
s.homepage = %q{http://github.com/brettbuddin/resque-priority}
|
28
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
29
|
+
s.require_paths = ["lib"]
|
30
|
+
s.rubygems_version = %q{1.3.7}
|
31
|
+
s.summary = %q{Resque plugin that provides priority levels}
|
32
|
+
|
33
|
+
if s.respond_to? :specification_version then
|
34
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
35
|
+
s.specification_version = 3
|
36
|
+
|
37
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
38
|
+
else
|
39
|
+
end
|
40
|
+
else
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: resque-priority
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
version: "0.1"
|
9
|
+
platform: ruby
|
10
|
+
authors:
|
11
|
+
- Brett Buddin
|
12
|
+
autorequire:
|
13
|
+
bindir: bin
|
14
|
+
cert_chain: []
|
15
|
+
|
16
|
+
date: 2010-10-01 00:00:00 -04:00
|
17
|
+
default_executable:
|
18
|
+
dependencies: []
|
19
|
+
|
20
|
+
description: " Provides Resque with three levels of (named) priority for a single queue.\n"
|
21
|
+
email: brett@intraspirit.net
|
22
|
+
executables: []
|
23
|
+
|
24
|
+
extensions: []
|
25
|
+
|
26
|
+
extra_rdoc_files:
|
27
|
+
- LICENSE
|
28
|
+
- README.md
|
29
|
+
files:
|
30
|
+
- LICENSE
|
31
|
+
- README.md
|
32
|
+
- Rakefile
|
33
|
+
- lib/resque/plugins/priority.rb
|
34
|
+
- resque-priority.gemspec
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: http://github.com/brettbuddin/resque-priority
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options:
|
41
|
+
- --charset=UTF-8
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
segments:
|
50
|
+
- 0
|
51
|
+
version: "0"
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
requirements: []
|
61
|
+
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 1.3.7
|
64
|
+
signing_key:
|
65
|
+
specification_version: 3
|
66
|
+
summary: Resque plugin that provides priority levels
|
67
|
+
test_files: []
|
68
|
+
|