resque-picky_worker 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source :rubygems
2
+
3
+ # Specify your gem's dependencies in pp-crawler.gemspec
4
+ gemspec
5
+
6
+ gem "SystemTimer"
7
+
8
+ group :test do
9
+ gem "rspec"
10
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,49 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ resque-picky_worker (0.0.1)
5
+ resque
6
+ resqueue-metadata
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ SystemTimer (1.2.3)
12
+ diff-lcs (1.1.2)
13
+ json (1.5.1)
14
+ rack (1.3.0)
15
+ redis (2.2.2)
16
+ redis-namespace (1.0.3)
17
+ redis (< 3.0.0)
18
+ redis-objects (0.5.1)
19
+ redis (>= 2.1.1)
20
+ resque (1.17.1)
21
+ json (< 1.6, >= 1.4.6)
22
+ redis-namespace (~> 1.0.2)
23
+ sinatra (>= 0.9.2)
24
+ vegas (~> 0.1.2)
25
+ resqueue-metadata (0.0.1)
26
+ redis-objects
27
+ resque
28
+ rspec (2.6.0)
29
+ rspec-core (~> 2.6.0)
30
+ rspec-expectations (~> 2.6.0)
31
+ rspec-mocks (~> 2.6.0)
32
+ rspec-core (2.6.4)
33
+ rspec-expectations (2.6.0)
34
+ diff-lcs (~> 1.1.2)
35
+ rspec-mocks (2.6.0)
36
+ sinatra (1.2.6)
37
+ rack (~> 1.1)
38
+ tilt (< 2.0, >= 1.2.2)
39
+ tilt (1.3.2)
40
+ vegas (0.1.8)
41
+ rack (>= 1.0.0)
42
+
43
+ PLATFORMS
44
+ ruby
45
+
46
+ DEPENDENCIES
47
+ SystemTimer
48
+ resque-picky_worker!
49
+ rspec
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ begin
5
+ require "spec"
6
+ rescue LoadError => e
7
+ require "rubygems"
8
+ require "spec"
9
+ end
10
+
11
+ begin
12
+ require 'spec/rake/spectask'
13
+
14
+ desc "Run all specs"
15
+ Spec::Rake::SpecTask.new('spec') do |t|
16
+ t.spec_files = FileList['spec/**/*.rb']
17
+ end
18
+ rescue LoadError => e
19
+ puts "Warning: RSpec not found. spec task won't work until you `gem install rspec`"
20
+ end
data/example.rb ADDED
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+ require "rubygems"
3
+ require "bundler/setup"
4
+ $:.unshift File.expand_path("lib", File.dirname(__FILE__))
5
+
6
+ require "resque/picky_worker/override"
7
+
8
+ # Try not to trample on other things in redis
9
+ Resque.redis.namespace = "example:pickyworker"
10
+
11
+ # Make sure we've got some queues to choose from
12
+ Resque.redis.del "queues"
13
+ %w(1 2 3 4).each do |x|
14
+ Resque.redis.sadd "queues", x
15
+ end
16
+
17
+ # Define how to choose a queue, we'll go for the max integer one
18
+ Resque::Worker.chooser = lambda do |queues|
19
+ queues.map(&:to_i).select {|i| i % 2 == 0 }.max.to_s
20
+ end
21
+
22
+ Resque::Worker.pick_queue # => "4"
@@ -0,0 +1,8 @@
1
+ unless defined?(Resque::PickyWorker)
2
+ require "resque/picky_worker"
3
+ end
4
+
5
+ # Swap this stuff out without generating a warning
6
+ original_verbosity, $VERBOSE = $VERBOSE, nil
7
+ Resque::Worker = Resque::PickyWorker
8
+ $VERBOSE, original_verbosity = original_verbosity, nil
@@ -0,0 +1,41 @@
1
+ require "resque"
2
+ require "resqueue-metadata"
3
+
4
+ module Resque
5
+ class PickyWorker < Worker
6
+ Error = Class.new(StandardError)
7
+
8
+ class << self
9
+ attr_accessor :default_max_workers
10
+ # Something that responds to #call (eg. Proc) and takes a single argument - array of queue names
11
+ # Should return a string which is a valid queue name
12
+ attr_accessor :chooser
13
+ end
14
+ self.default_max_workers ||= 5
15
+
16
+ def self.chooser
17
+ @chooser || raise(Error, "chooser must be specified or I cannot choose a queue to listen to")
18
+ end
19
+
20
+ # Returns a string (name of a queue)
21
+ def self.pick_queue
22
+ cue = chooser.call(Resque.queues)
23
+ raise(Error, "chooser returned an invalid queue name") unless Resque.queues.include?(cue)
24
+ cue
25
+ end
26
+
27
+ def self.queue_max name
28
+ (Resque::Queue::Metadata.new(name)["max_workers"] || default_max_workers).to_i
29
+ end
30
+
31
+ def self.queue_current name
32
+ all.select {|worker| worker.queues.include?(name) }.size
33
+ end
34
+
35
+ # Invoke the worker class with a randomly picked queue
36
+ def initialize
37
+ super(PickyWorker.pick_queue)
38
+ end
39
+
40
+ end
41
+ end
data/readme.md ADDED
@@ -0,0 +1,34 @@
1
+ # Resque::PickyWorker
2
+
3
+ Picky worker is a very opinionated worker that will just not be told what work to do. It prefers to pick and choose from the available queues, slotting in where it's needed to help others. Also kills itself quietly if left with no work to do.
4
+
5
+ ## Installation
6
+
7
+ Install with rubygems (might need to run this as root, depending on your setup)
8
+
9
+ gem install resque-picky_worker
10
+
11
+ Or in your Gemfile
12
+
13
+ gem "resque-picky_worker"
14
+
15
+ ## Usage
16
+
17
+ You'll need to require the picky worker, usually with `require "resque/picky_worker/override"` to make it the default Worker class in resque. (Means it *Just Works*™.)
18
+
19
+ Then you need to define how you want a queue to be chosen. Lets say we have a bunch of queues named after numbers and we want to choose the highest number available at the time the worker starts:
20
+
21
+ Resque::PickyWorker.chooser = lambda { | queues | queues.map(&:to_i).max.to_s }
22
+
23
+ Then just fire up your resque workers as normal and they'll choose the highest queue available to listen to!
24
+
25
+ ## MIT Licence
26
+
27
+ Copyright (c) 2011 PizzaPowered LLP <caius@pizzapowered.com>
28
+
29
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
30
+
31
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
32
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33
+
34
+ See http://pizzapowered.com/ for contact details.
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "resque-picky_worker"
5
+ s.version = "0.0.1"
6
+ s.platform = Gem::Platform::RUBY
7
+ s.authors = ["Caius Durling"]
8
+ s.email = ["caius@pizzapowered.com"]
9
+ s.homepage = "http://pizzapowered.com/"
10
+ s.summary = %q{Resque worker, but picky about what queues it listens to}
11
+ s.description = %q{Resque worker that evaluates queues and chooses which one to pull jobs from}
12
+
13
+ s.files = `git ls-files`.split("\n")
14
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
+ s.require_paths = ["lib"]
17
+ s.bindir = "bin"
18
+
19
+ s.add_dependency "resque", "~> 2.2"
20
+ s.add_dependency "resqueue-metadata", "~> 0.0.1"
21
+ end
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rubygems"
4
+ require "bundler/setup"
5
+ require File.expand_path("../lib/resque_picky_worker", File.dirname(__FILE__))
6
+
7
+ Resque.redis = "localhost:6379"
8
+
9
+ require "irb"
10
+ IRB.start(__FILE__)
@@ -0,0 +1,47 @@
1
+ require "spec_helper"
2
+
3
+ describe Resque::PickyWorker do
4
+ before do
5
+ @odd_queues = %w(1 3)
6
+ @even_queues = %w(2 4 6)
7
+ @queues = @odd_queues | @even_queues
8
+ Resque.stub!(:queues).and_return(@queues)
9
+
10
+ @chooser = lambda do |queues|
11
+ # Choose the highest even numbered queue we can find
12
+ queues.map(&:to_i).select {|i| i % 2 == 0 }.max.to_s
13
+ end
14
+ Resque::PickyWorker.chooser = @chooser
15
+ end
16
+
17
+ describe ".pick_queue" do
18
+ before do
19
+ @queue = Resque::PickyWorker.pick_queue
20
+ end
21
+ it "returns a string" do
22
+ @queue.should be_a(String)
23
+ end
24
+ it "should be a crawl queue" do
25
+ @even_queues.should include(@queue)
26
+ end
27
+ end
28
+
29
+ describe ".chooser" do
30
+ it "should be required" do
31
+ Resque::PickyWorker.chooser = nil
32
+
33
+ lambda do
34
+ Resque::PickyWorker.chooser
35
+ end.should raise_error(Resque::PickyWorker::Error, "chooser must be specified or I cannot choose a queue to listen to")
36
+ end
37
+ end
38
+
39
+ describe ".queue_max" do
40
+ context "with a max count set" do
41
+ it "should return it" do
42
+ Resque::Queue::Metadata.new("testing-queue-name-here")["max_workers"]
43
+ end
44
+ end
45
+ end
46
+
47
+ end
@@ -0,0 +1,4 @@
1
+ require File.expand_path("../lib/resque/picky_worker", File.dirname(__FILE__))
2
+
3
+ Resque.redis = "localhost:6379"
4
+ Resque.redis.namespace = "resque-picky-worker-spec"
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resque-picky_worker
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Caius Durling
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-08-23 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: resque
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 2
32
+ - 2
33
+ version: "2.2"
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: resqueue-metadata
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 29
45
+ segments:
46
+ - 0
47
+ - 0
48
+ - 1
49
+ version: 0.0.1
50
+ type: :runtime
51
+ version_requirements: *id002
52
+ description: Resque worker that evaluates queues and chooses which one to pull jobs from
53
+ email:
54
+ - caius@pizzapowered.com
55
+ executables: []
56
+
57
+ extensions: []
58
+
59
+ extra_rdoc_files: []
60
+
61
+ files:
62
+ - .rspec
63
+ - Gemfile
64
+ - Gemfile.lock
65
+ - Rakefile
66
+ - example.rb
67
+ - lib/resque/picky_worker.rb
68
+ - lib/resque/picky_worker/override.rb
69
+ - readme.md
70
+ - resque-picky_worker.gemspec
71
+ - script/console
72
+ - spec/resque/picky_worker_spec.rb
73
+ - spec/spec_helper.rb
74
+ - vendor/cache/SystemTimer-1.2.3.gem
75
+ - vendor/cache/diff-lcs-1.1.2.gem
76
+ - vendor/cache/json-1.5.1.gem
77
+ - vendor/cache/rack-1.3.0.gem
78
+ - vendor/cache/redis-2.2.2.gem
79
+ - vendor/cache/redis-namespace-1.0.3.gem
80
+ - vendor/cache/redis-objects-0.5.1.gem
81
+ - vendor/cache/resque-1.17.1.gem
82
+ - vendor/cache/resqueue-metadata-0.0.1.gem
83
+ - vendor/cache/rspec-2.6.0.gem
84
+ - vendor/cache/rspec-core-2.6.4.gem
85
+ - vendor/cache/rspec-expectations-2.6.0.gem
86
+ - vendor/cache/rspec-mocks-2.6.0.gem
87
+ - vendor/cache/sinatra-1.2.6.gem
88
+ - vendor/cache/tilt-1.3.2.gem
89
+ - vendor/cache/vegas-0.1.8.gem
90
+ has_rdoc: true
91
+ homepage: http://pizzapowered.com/
92
+ licenses: []
93
+
94
+ post_install_message:
95
+ rdoc_options: []
96
+
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ hash: 3
105
+ segments:
106
+ - 0
107
+ version: "0"
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ hash: 3
114
+ segments:
115
+ - 0
116
+ version: "0"
117
+ requirements: []
118
+
119
+ rubyforge_project:
120
+ rubygems_version: 1.6.2
121
+ signing_key:
122
+ specification_version: 3
123
+ summary: Resque worker, but picky about what queues it listens to
124
+ test_files:
125
+ - spec/resque/picky_worker_spec.rb
126
+ - spec/spec_helper.rb