kbaum-resque-priority 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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.
@@ -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.
@@ -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,46 @@
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
+ priority ||= :normal
5
+ if [:high, :low].include?(priority.to_sym)
6
+ queue = "#{queue}_#{priority}".to_sym
7
+ end
8
+ klass.priority = priority
9
+ Resque::Job.create(queue, klass, *args)
10
+ end
11
+
12
+ module Plugins
13
+ module Priority
14
+ def priority=(p)
15
+ @queue_without_priority ||= @queue
16
+ if [:high, :low].include?(p.to_sym)
17
+ @queue = "#{@queue}_#{p}".to_sym
18
+ end
19
+ @priority = p.to_sym
20
+ end
21
+
22
+ def priority_key(*args)
23
+ "priority:#{name}-#{args.to_s}"
24
+ end
25
+
26
+ def after_enqueue_set_priority(*args)
27
+ Resque.redis.setnx(priority_key(*args), @priority)
28
+ end
29
+
30
+ def around_perform_retrieve_priority(*args)
31
+ key = priority_key(*args)
32
+ priority = Resque.redis.get(key)
33
+ self.priority = priority && priority.empty? ? :normal : priority
34
+
35
+ begin
36
+ yield
37
+ ensure
38
+ Resque.redis.del(key)
39
+ @queue = @queue_without_priority
40
+ @priority = nil
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+
@@ -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{kbaum-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,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kbaum-resque-priority
3
+ version: !ruby/object:Gem::Version
4
+ hash: 9
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ version: "0.1"
10
+ platform: ruby
11
+ authors:
12
+ - Brett Buddin
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-10-01 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: " Provides Resque with three levels of (named) priority for a single queue.\n"
22
+ email: brett@intraspirit.net
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - LICENSE
29
+ - README.md
30
+ files:
31
+ - LICENSE
32
+ - README.md
33
+ - Rakefile
34
+ - lib/resque/plugins/priority.rb
35
+ - resque-priority.gemspec
36
+ has_rdoc: true
37
+ homepage: http://github.com/brettbuddin/resque-priority
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options:
42
+ - --charset=UTF-8
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ hash: 3
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.3.7
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Resque plugin that provides priority levels
70
+ test_files: []
71
+