nogara-resque-loner 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/CHANGELOG.markdown +27 -0
- data/Gemfile +8 -0
- data/LICENSE +19 -0
- data/README.markdown +103 -0
- data/Rakefile +83 -0
- data/init.rb +1 -0
- data/lib/resque-ext/job.rb +57 -0
- data/lib/resque-ext/resque.rb +26 -0
- data/lib/resque-loner.rb +6 -0
- data/lib/resque-loner/helpers.rb +73 -0
- data/lib/resque-loner/unique_job.rb +75 -0
- data/lib/resque-loner/version.rb +7 -0
- data/rails/init.rb +1 -0
- data/resque-loner.gemspec +50 -0
- data/spec/loner_spec.rb +165 -0
- data/spec/redis-test.conf +115 -0
- data/spec/spec_helper.rb +14 -0
- data/tasks/redis.rake +159 -0
- data/tasks/resque.rake +2 -0
- data/test/hoptoad_test.rb +25 -0
- data/test/job_hooks_test.rb +323 -0
- data/test/job_plugins_test.rb +230 -0
- data/test/plugin_test.rb +116 -0
- data/test/redis-test.conf +115 -0
- data/test/resque-web_test.rb +53 -0
- data/test/resque_test.rb +259 -0
- data/test/test_helper.rb +148 -0
- data/test/worker_test.rb +332 -0
- metadata +185 -0
data/.gitignore
ADDED
data/CHANGELOG.markdown
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
1.2.1
|
2
|
+
--------------------------------
|
3
|
+
Merged @aerodynamik's pull request. Enqueuing and marking as
|
4
|
+
enqueued as an atomic operation.
|
5
|
+
|
6
|
+
1.2.0
|
7
|
+
--------------------------------
|
8
|
+
Thanks @unclebilly for your pull request. Resque-loner now supports
|
9
|
+
a maximum time for which a job should be unique. Just define @loner_ttl
|
10
|
+
in your job (or leave it at -1 to never expire) and after @loner_ttl
|
11
|
+
seconds your job can be enqueued again, even if an older one is still
|
12
|
+
marked as running.
|
13
|
+
|
14
|
+
1.1.0
|
15
|
+
--------------------------------
|
16
|
+
Merged in @ryansch's pull requests to clean up things a bit.
|
17
|
+
This removed the `enqueue_to` and `dequeue_from` methods from
|
18
|
+
resque-loner because it caused troubles with resque's own
|
19
|
+
`enqueue_to`.
|
20
|
+
|
21
|
+
1.0.1
|
22
|
+
--------------------------------
|
23
|
+
Pulled in #8 and #9 so that removing empty queues
|
24
|
+
does not fail
|
25
|
+
|
26
|
+
1.0
|
27
|
+
---------------------------------
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2010 Moviepilot GmbH http://moviepilot.com
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
Resque-Loner
|
2
|
+
======
|
3
|
+
|
4
|
+
Resque-Loner is a plugin for defunkt/resque which adds unique jobs to resque: Only one job with the same payload per queue.
|
5
|
+
|
6
|
+
|
7
|
+
Installation
|
8
|
+
-------------
|
9
|
+
|
10
|
+
First install the gem:
|
11
|
+
|
12
|
+
$ gem install resque-loner
|
13
|
+
|
14
|
+
Then include it in your app:
|
15
|
+
|
16
|
+
require 'resque-loner'
|
17
|
+
|
18
|
+
|
19
|
+
Tests
|
20
|
+
-----------
|
21
|
+
To make sure this plugin works on your installation, you should run the tests. resque-loner is tested in RSpec, but it also includes resque's original testsuite. You can run all tests specific to resque-loner with `rake spec`.
|
22
|
+
|
23
|
+
To make sure the plugin did not break resque, you can run `rake test` (the standard resque test suite). This runs all tests from the 1.10.0 version of resque, so make sure you have that version of resque installed, when you run the resque-tests.
|
24
|
+
|
25
|
+
Example
|
26
|
+
--------
|
27
|
+
|
28
|
+
Unique jobs can be useful in situations where running the same job multiple times issues the same results. Let's say you have a job called CacheSweeper that refreshes some cache. A user has edited some_article, so you put a job on the queue to refresh the cache for that article.
|
29
|
+
|
30
|
+
>> Resque.enqueue CacheSweeper, some_article.id
|
31
|
+
=> "OK"
|
32
|
+
|
33
|
+
Your queue is really full, so the job does not get executed right away. But the user editing the article has noticed another error, and updates the article again, and your app kindly queues another job to update that article's cache.
|
34
|
+
|
35
|
+
>> Resque.enqueue CacheSweeper, some_article.id
|
36
|
+
=> "OK"
|
37
|
+
|
38
|
+
At this point you will have two jobs in the queue, the second of which has no effect: You don't have to run it, once the cache has been updated for the first time. This is where resque-loner's UniqueJobs come in. If you define CacheSweeper like this:
|
39
|
+
|
40
|
+
class CacheSweeper
|
41
|
+
include Resque::Plugins::UniqueJob
|
42
|
+
@queue = :cache_sweeps
|
43
|
+
|
44
|
+
def self.perform(article_id)
|
45
|
+
# Cache Me If You Can...
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
Just like that you've assured that on the :cache_sweeps queue, there can only be one CacheSweeper job for each article. Let's see what happens when you try to enqueue a couple of these jobs now:
|
50
|
+
|
51
|
+
>> Resque.enqueue CacheSweeper, 1
|
52
|
+
=> "OK"
|
53
|
+
>> Resque.enqueue CacheSweeper, 1
|
54
|
+
=> "EXISTED"
|
55
|
+
>> Resque.enqueue CacheSweeper, 1
|
56
|
+
=> "EXISTED"
|
57
|
+
>> Resque.size :cache_sweeps
|
58
|
+
=> 1
|
59
|
+
|
60
|
+
Since resque-loner keeps track of which jobs are queued in a way that allows for finding jobs very quickly, you can also query if a job is currently in a queue:
|
61
|
+
|
62
|
+
>> Resque.enqueue CacheSweeper, 1
|
63
|
+
=> "OK"
|
64
|
+
>> Resque.enqueued? CacheSweeper, 1
|
65
|
+
=> true
|
66
|
+
>> Resque.enqueued? CacheSweeper, 2
|
67
|
+
=> false
|
68
|
+
>> Resque.enqueued_in? :another_queue, CacheSweeper, 1
|
69
|
+
=> false
|
70
|
+
|
71
|
+
How it works
|
72
|
+
--------
|
73
|
+
|
74
|
+
### Keeping track of queued unique jobs
|
75
|
+
|
76
|
+
For each created UniqueJob, resque-loner sets a redis key to 1. This key remains set until the job has either been fetched from the queue or destroyed through the Resque::Job.destroy method. As long as the key is set, the job is considered queued and consequent queue adds are being rejected.
|
77
|
+
|
78
|
+
Here's how these keys are constructed:
|
79
|
+
|
80
|
+
resque:loners:queue:cache_sweeps:job:5ac5a005253450606aa9bc3b3d52ea5b
|
81
|
+
| | | |
|
82
|
+
| | | `---- Job's ID (#redis_key method)
|
83
|
+
| | `--------------------- Name of the queue
|
84
|
+
| `------------------------------ Prefix for this plugin
|
85
|
+
`----------------------------------------- Your redis namespace
|
86
|
+
|
87
|
+
The last part of this key is the job's ID, which is pretty much your queue item's payload. For our CacheSweeper job, the payload would be:
|
88
|
+
|
89
|
+
{ 'class': 'CacheSweeper', 'args': [1] }`
|
90
|
+
|
91
|
+
The default method to create a job ID from these parameters is to do some normalization on the payload and then md5'ing it (defined in `Resque::Plugins::UniqueJob#redis_key`).
|
92
|
+
|
93
|
+
You could also use the whole payload or anything else as a redis key, as long as you make sure these requirements are met:
|
94
|
+
|
95
|
+
1. Two jobs of the same class with the same parameters/arguments/workload must produce the same redis_key
|
96
|
+
2. Two jobs with either a different class or different parameters/arguments/workloads must not produce the same redis key
|
97
|
+
3. The key must not be binary, because this restriction applies to redis keys: *Keys are not binary safe strings in Redis, but just strings not containing a space or a newline character. For instance "foo" or "123456789" or "foo_bar" are valid keys, while "hello world" or "hello\n" are not.* (see http://code.google.com/p/redis/wiki/IntroductionToRedisDataTypes)
|
98
|
+
|
99
|
+
So when your job overwrites the #redis_key method, make sure these requirements are met. And all should be good.
|
100
|
+
|
101
|
+
### Resque integration
|
102
|
+
|
103
|
+
Unfortunately not everything could be done as a plugin, so I overwrote three methods of Resque::Job: create, reserve and destroy (I found no hooks for these events). All the logic is in `module Resque::Plugins::Loner` though, so it should be fairly easy to make this a *pure* plugin once the hooks are known.
|
data/Rakefile
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
#
|
2
|
+
# Setup
|
3
|
+
#
|
4
|
+
|
5
|
+
$LOAD_PATH.unshift 'lib'
|
6
|
+
|
7
|
+
require "rubygems"
|
8
|
+
require "bundler"
|
9
|
+
Bundler.setup
|
10
|
+
|
11
|
+
require 'rspec/core/rake_task'
|
12
|
+
|
13
|
+
load 'tasks/redis.rake'
|
14
|
+
require 'rake/testtask'
|
15
|
+
|
16
|
+
require 'resque/tasks'
|
17
|
+
|
18
|
+
require 'bundler/gem_tasks'
|
19
|
+
|
20
|
+
def command?(command)
|
21
|
+
system("type #{command} > /dev/null 2>&1")
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
#
|
26
|
+
# Tests
|
27
|
+
#
|
28
|
+
|
29
|
+
task :default => :spec
|
30
|
+
|
31
|
+
desc "Run specs for resque-loner"
|
32
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
33
|
+
t.pattern = "spec/**/*_spec.rb"
|
34
|
+
t.rspec_opts = %w(-fd -c)
|
35
|
+
end
|
36
|
+
|
37
|
+
# desc "Run resque's test suite to make sure we did not break anything"
|
38
|
+
# task :test do
|
39
|
+
# rg = command?(:rg)
|
40
|
+
# Dir['test/**/*_test.rb'].each do |f|
|
41
|
+
# rg ? sh("rg #{f}") : ruby(f)
|
42
|
+
# end
|
43
|
+
# end
|
44
|
+
|
45
|
+
if command?(:rg)
|
46
|
+
desc "Run the test suite with rg"
|
47
|
+
task :test do
|
48
|
+
Dir['test/**/*_test.rb'].each do |f|
|
49
|
+
sh("rg #{f}")
|
50
|
+
end
|
51
|
+
end
|
52
|
+
else
|
53
|
+
Rake::TestTask.new do |test|
|
54
|
+
test.libs << "test"
|
55
|
+
test.test_files = FileList['test/**/*_test.rb']
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
if command? :kicker
|
60
|
+
desc "Launch Kicker (like autotest)"
|
61
|
+
task :kicker do
|
62
|
+
puts "Kicking... (ctrl+c to cancel)"
|
63
|
+
exec "kicker -e rake test lib examples"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
#
|
69
|
+
# Install
|
70
|
+
#
|
71
|
+
|
72
|
+
task :install => [ 'redis:install', 'dtach:install' ]
|
73
|
+
|
74
|
+
|
75
|
+
#
|
76
|
+
# Documentation
|
77
|
+
#
|
78
|
+
|
79
|
+
begin
|
80
|
+
require 'sdoc_helpers'
|
81
|
+
rescue LoadError
|
82
|
+
end
|
83
|
+
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'rails/init.rb'
|
@@ -0,0 +1,57 @@
|
|
1
|
+
#
|
2
|
+
# Since there were not enough hooks to hook into, I have to overwrite
|
3
|
+
# 3 methods of Resque::Job - the rest of the implementation is in the
|
4
|
+
# proper Plugin namespace.
|
5
|
+
#
|
6
|
+
module Resque
|
7
|
+
class Job
|
8
|
+
|
9
|
+
|
10
|
+
#
|
11
|
+
# Overwriting original create method to mark an item as queued
|
12
|
+
# after Resque::Job.create has called Resque.push
|
13
|
+
#
|
14
|
+
def self.create_with_loner(queue, klass, *args)
|
15
|
+
return create_without_loner(queue, klass, *args) if Resque.inline?
|
16
|
+
item = { :class => klass.to_s, :args => args }
|
17
|
+
return "EXISTED" if Resque::Plugins::Loner::Helpers.loner_queued?(queue, item)
|
18
|
+
# multi block returns array of keys
|
19
|
+
Resque.redis.multi do
|
20
|
+
create_without_loner(queue, klass, *args)
|
21
|
+
Resque::Plugins::Loner::Helpers.mark_loner_as_queued(queue, item)
|
22
|
+
end.first
|
23
|
+
end
|
24
|
+
|
25
|
+
#
|
26
|
+
# Overwriting original reserve method to mark an item as unqueued
|
27
|
+
#
|
28
|
+
def self.reserve_with_loner(queue)
|
29
|
+
item = reserve_without_loner(queue)
|
30
|
+
Resque::Plugins::Loner::Helpers.mark_loner_as_unqueued( queue, item ) if item && !Resque.inline?
|
31
|
+
item
|
32
|
+
end
|
33
|
+
|
34
|
+
#
|
35
|
+
# Overwriting original destroy method to mark all destroyed jobs as unqueued.
|
36
|
+
# Because the original method only returns the amount of jobs destroyed, but not
|
37
|
+
# the jobs themselves. Hence Resque::Plugins::Loner::Helpers.job_destroy looks almost
|
38
|
+
# as the original method Resque::Job.destroy. Couldn't make it any dry'er.
|
39
|
+
#
|
40
|
+
def self.destroy_with_loner(queue, klass, *args)
|
41
|
+
Resque::Plugins::Loner::Helpers.job_destroy(queue, klass, *args) unless Resque.inline?
|
42
|
+
destroy_without_loner(queue, klass, *args)
|
43
|
+
end
|
44
|
+
|
45
|
+
#
|
46
|
+
# Chain..
|
47
|
+
#
|
48
|
+
class << self
|
49
|
+
alias_method :create_without_loner, :create
|
50
|
+
alias_method :create, :create_with_loner
|
51
|
+
alias_method :reserve_without_loner, :reserve
|
52
|
+
alias_method :reserve, :reserve_with_loner
|
53
|
+
alias_method :destroy_without_loner, :destroy
|
54
|
+
alias_method :destroy, :destroy_with_loner
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Resque
|
2
|
+
|
3
|
+
def self.enqueued?( klass, *args)
|
4
|
+
enqueued_in?(queue_from_class(klass), klass, *args )
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.enqueued_in?(queue, klass, *args)
|
8
|
+
item = { :class => klass.to_s, :args => args }
|
9
|
+
return nil unless Resque::Plugins::Loner::Helpers.item_is_a_unique_job?(item)
|
10
|
+
Resque::Plugins::Loner::Helpers.loner_queued?(queue, item)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.remove_queue_with_loner_cleanup(queue)
|
14
|
+
self.remove_queue_without_loner_cleanup(queue)
|
15
|
+
Resque::Plugins::Loner::Helpers.cleanup_loners(queue)
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
class << self
|
20
|
+
|
21
|
+
alias_method :remove_queue_without_loner_cleanup, :remove_queue
|
22
|
+
alias_method :remove_queue, :remove_queue_with_loner_cleanup
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
data/lib/resque-loner.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
module Resque
|
2
|
+
module Plugins
|
3
|
+
module Loner
|
4
|
+
class Helpers
|
5
|
+
extend Resque::Helpers
|
6
|
+
|
7
|
+
def self.loner_queued?(queue, item)
|
8
|
+
return false unless item_is_a_unique_job?(item)
|
9
|
+
redis.get(unique_job_queue_key(queue, item)) == "1"
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.mark_loner_as_queued(queue, item)
|
13
|
+
return unless item_is_a_unique_job?(item)
|
14
|
+
key = unique_job_queue_key(queue, item)
|
15
|
+
redis.set(key, 1)
|
16
|
+
unless(ttl=item_ttl(item)) == -1 # no need to incur overhead for default value
|
17
|
+
redis.expire(key, ttl)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.mark_loner_as_unqueued(queue, job)
|
22
|
+
item = job.is_a?(Resque::Job) ? job.payload : job
|
23
|
+
return unless item_is_a_unique_job?(item)
|
24
|
+
redis.del(unique_job_queue_key(queue, item))
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.unique_job_queue_key(queue, item)
|
28
|
+
job_key = constantize(item[:class] || item["class"]).redis_key_loner(item)
|
29
|
+
"loners:queue:#{queue}:job:#{job_key}"
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.item_is_a_unique_job?(item)
|
33
|
+
begin
|
34
|
+
klass = constantize(item[:class] || item["class"])
|
35
|
+
klass.included_modules.include?(::Resque::Plugins::UniqueJob)
|
36
|
+
rescue
|
37
|
+
false # Resque testsuite also submits strings as job classes while Resque.enqueue'ing,
|
38
|
+
end # so resque-loner should not start throwing up when that happens.
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.item_ttl(item)
|
42
|
+
begin
|
43
|
+
constantize(item[:class] || item["class"]).loner_ttl
|
44
|
+
rescue
|
45
|
+
-1
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.job_destroy(queue, klass, *args)
|
50
|
+
klass = klass.to_s
|
51
|
+
redis_queue = "queue:#{queue}"
|
52
|
+
|
53
|
+
redis.lrange(redis_queue, 0, -1).each do |string|
|
54
|
+
json = decode(string)
|
55
|
+
|
56
|
+
match = json['class'] == klass
|
57
|
+
match &= json['args'] == args unless args.empty?
|
58
|
+
|
59
|
+
if match
|
60
|
+
Resque::Plugins::Loner::Helpers.mark_loner_as_unqueued( queue, json )
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.cleanup_loners(queue)
|
66
|
+
keys = redis.keys("loners:queue:#{queue}:job:*")
|
67
|
+
redis.del(*keys) unless keys.empty?
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'digest/md5'
|
2
|
+
|
3
|
+
#
|
4
|
+
# If you want your job to be unique, include this module in it. If you wish,
|
5
|
+
# you can overwrite this implementation of redis_key to fit your needs
|
6
|
+
#
|
7
|
+
module Resque
|
8
|
+
module Plugins
|
9
|
+
module UniqueJob
|
10
|
+
|
11
|
+
def self.included(base)
|
12
|
+
base.extend ClassMethods
|
13
|
+
base.class_eval do
|
14
|
+
base.send(:extend, Resque::Helpers)
|
15
|
+
end
|
16
|
+
end # self.included
|
17
|
+
|
18
|
+
module ClassMethods
|
19
|
+
|
20
|
+
|
21
|
+
#
|
22
|
+
# Payload is what Resque stored for this job along with the job's class name.
|
23
|
+
# On a Resque with no plugins installed, this is a hash containing :class and :args
|
24
|
+
#
|
25
|
+
def redis_key_loner(payload)
|
26
|
+
payload = decode(encode(payload)) # This is the cycle the data goes when being enqueued/dequeued
|
27
|
+
job = payload[:class] || payload["class"]
|
28
|
+
args = (payload[:args] || payload["args"])
|
29
|
+
args.map! do |arg|
|
30
|
+
arg.is_a?(Hash) ? arg.sort : arg
|
31
|
+
end
|
32
|
+
|
33
|
+
digest = Digest::MD5.hexdigest encode(:class => job, :args => args)
|
34
|
+
digest
|
35
|
+
end
|
36
|
+
|
37
|
+
#
|
38
|
+
# The default ttl of a locking key is -1, i.e. forever. If for some reason you only
|
39
|
+
# want the lock to be in place after a certain amount of time, just set a ttl for
|
40
|
+
# for your job. For example:
|
41
|
+
#
|
42
|
+
# class FooJob
|
43
|
+
# include Resque::Plugins::UniqueJob
|
44
|
+
# @loner_ttl = 40
|
45
|
+
# end
|
46
|
+
# end
|
47
|
+
#
|
48
|
+
def loner_ttl
|
49
|
+
@loner_ttl || -1
|
50
|
+
end
|
51
|
+
|
52
|
+
end # ClassMethods
|
53
|
+
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
module Resque
|
60
|
+
module Plugins
|
61
|
+
module Loner
|
62
|
+
class UniqueJob
|
63
|
+
|
64
|
+
include Resque::Plugins::UniqueJob
|
65
|
+
|
66
|
+
def self.inherited(host)
|
67
|
+
super(host)
|
68
|
+
return if @__unique_job_warned
|
69
|
+
warn "Inherit Resque::Plugins::Loner::UniqueJob is deprecated. Include Resque::Plugins::UniqueJob module instead."
|
70
|
+
@__unique_job_warned = true
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|