ryansch-resque-loner 1.0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown ADDED
@@ -0,0 +1,110 @@
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
+ If you want the same type of job in different queues, resque-loner lets you enqueue/dequeue to a queue of your choice:
72
+
73
+ >> Resque.enqueue_to :another_queue, CacheSweeper, 1
74
+ => "OK"
75
+ >> Resqueue.dequeue_from :another_queue, CacheSweeper, 1
76
+ => 1
77
+
78
+ How it works
79
+ --------
80
+
81
+ ### Keeping track of queued unique jobs
82
+
83
+ 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.
84
+
85
+ Here's how these keys are constructed:
86
+
87
+ resque:loners:queue:cache_sweeps:job:5ac5a005253450606aa9bc3b3d52ea5b
88
+ | | | |
89
+ | | | `---- Job's ID (#redis_key method)
90
+ | | `--------------------- Name of the queue
91
+ | `------------------------------ Prefix for this plugin
92
+ `----------------------------------------- Your redis namespace
93
+
94
+ 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:
95
+
96
+ { 'class': 'CacheSweeper', 'args': [1] }`
97
+
98
+ 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`).
99
+
100
+ You could also use the whole payload or anything else as a redis key, as long as you make sure these requirements are met:
101
+
102
+ 1. Two jobs of the same class with the same parameters/arguments/workload must produce the same redis_key
103
+ 2. Two jobs with either a different class or different parameters/arguments/workloads must not produce the same redis key
104
+ 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)
105
+
106
+ So when your job overwrites the #redis_key method, make sure these requirements are met. And all should be good.
107
+
108
+ ### Resque integration
109
+
110
+ 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.
@@ -0,0 +1,55 @@
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
+ job = create_without_loner(queue, klass, *args)
19
+ Resque::Plugins::Loner::Helpers.mark_loner_as_queued(queue, item)
20
+ job
21
+ end
22
+
23
+ #
24
+ # Overwriting original reserve method to mark an item as unqueued
25
+ #
26
+ def self.reserve_with_loner(queue)
27
+ item = reserve_without_loner(queue)
28
+ Resque::Plugins::Loner::Helpers.mark_loner_as_unqueued( queue, item ) if item && !Resque.inline?
29
+ item
30
+ end
31
+
32
+ #
33
+ # Overwriting original destroy method to mark all destroyed jobs as unqueued.
34
+ # Because the original method only returns the amount of jobs destroyed, but not
35
+ # the jobs themselves. Hence Resque::Plugins::Loner::Helpers.job_destroy looks almost
36
+ # as the original method Resque::Job.destroy. Couldn't make it any dry'er.
37
+ #
38
+ def self.destroy_with_loner(queue, klass, *args)
39
+ Resque::Plugins::Loner::Helpers.job_destroy(queue, klass, *args) unless Resque.inline?
40
+ destroy_without_loner(queue, klass, *args)
41
+ end
42
+
43
+ #
44
+ # Chain..
45
+ #
46
+ class << self
47
+ alias_method :create_without_loner, :create
48
+ alias_method :create, :create_with_loner
49
+ alias_method :reserve_without_loner, :reserve
50
+ alias_method :reserve, :reserve_with_loner
51
+ alias_method :destroy_without_loner, :destroy
52
+ alias_method :destroy, :destroy_with_loner
53
+ end
54
+ end
55
+ 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
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ require 'resque'
3
+ require 'resque-loner/unique_job'
4
+ require 'resque-loner/helpers'
5
+ require 'resque-ext/job'
6
+ require 'resque-ext/resque'
@@ -0,0 +1,61 @@
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
+ redis.set(unique_job_queue_key(queue, item), 1)
15
+ end
16
+
17
+ def self.mark_loner_as_unqueued(queue, job)
18
+ item = job.is_a?(Resque::Job) ? job.payload : job
19
+ return unless item_is_a_unique_job?(item)
20
+ redis.del(unique_job_queue_key(queue, item))
21
+ end
22
+
23
+ def self.unique_job_queue_key(queue, item)
24
+ job_key = constantize(item[:class] || item["class"]).redis_key(item)
25
+ "loners:queue:#{queue}:job:#{job_key}"
26
+ end
27
+
28
+ def self.item_is_a_unique_job?(item)
29
+ begin
30
+ klass = constantize(item[:class] || item["class"])
31
+ klass.included_modules.include?(::Resque::Plugins::UniqueJob)
32
+ rescue
33
+ false # Resque testsuite also submits strings as job classes while Resque.enqueue'ing,
34
+ end # so resque-loner should not start throwing up when that happens.
35
+ end
36
+
37
+ def self.job_destroy(queue, klass, *args)
38
+ klass = klass.to_s
39
+ redis_queue = "queue:#{queue}"
40
+
41
+ redis.lrange(redis_queue, 0, -1).each do |string|
42
+ json = decode(string)
43
+
44
+ match = json['class'] == klass
45
+ match &= json['args'] == args unless args.empty?
46
+
47
+ if match
48
+ Resque::Plugins::Loner::Helpers.mark_loner_as_unqueued( queue, json )
49
+ end
50
+ end
51
+ end
52
+
53
+ def self.cleanup_loners(queue)
54
+ keys = redis.keys("loners:queue:#{queue}:job:*")
55
+ redis.del(*keys) unless keys.empty?
56
+ end
57
+
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,59 @@
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(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
+ end # ClassMethods
37
+
38
+
39
+ end
40
+ end
41
+ end
42
+
43
+ module Resque
44
+ module Plugins
45
+ module Loner
46
+ class UniqueJob
47
+
48
+ include Resque::Plugins::UniqueJob
49
+
50
+ def self.inherited(host)
51
+ super(host)
52
+ return if @__unique_job_warned
53
+ warn "Inherit Resque::Plugins::Loner::UniqueJob is deprecated. Include Resque::Plugins::UniqueJob module instead."
54
+ @__unique_job_warned = true
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,7 @@
1
+ module Resque
2
+ module Plugins
3
+ module Loner
4
+ VERSION = "1.0.1.1"
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,169 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ryansch-resque-loner
3
+ version: !ruby/object:Gem::Version
4
+ hash: 89
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 1
10
+ - 1
11
+ version: 1.0.1.1
12
+ platform: ruby
13
+ authors:
14
+ - Jannis Hermanns
15
+ - Ryan Schlesinger
16
+ autorequire:
17
+ bindir: bin
18
+ cert_chain: []
19
+
20
+ date: 2011-11-04 00:00:00 -07:00
21
+ default_executable:
22
+ dependencies:
23
+ - !ruby/object:Gem::Dependency
24
+ name: resque
25
+ prerelease: false
26
+ requirement: &id001 !ruby/object:Gem::Requirement
27
+ none: false
28
+ requirements:
29
+ - - ~>
30
+ - !ruby/object:Gem::Version
31
+ hash: 15
32
+ segments:
33
+ - 1
34
+ - 0
35
+ version: "1.0"
36
+ type: :runtime
37
+ version_requirements: *id001
38
+ - !ruby/object:Gem::Dependency
39
+ name: rake
40
+ prerelease: false
41
+ requirement: &id002 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ hash: 49
47
+ segments:
48
+ - 0
49
+ - 8
50
+ - 7
51
+ version: 0.8.7
52
+ type: :development
53
+ version_requirements: *id002
54
+ - !ruby/object:Gem::Dependency
55
+ name: rspec
56
+ prerelease: false
57
+ requirement: &id003 !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ hash: 27
63
+ segments:
64
+ - 2
65
+ - 5
66
+ - 0
67
+ version: 2.5.0
68
+ type: :development
69
+ version_requirements: *id003
70
+ - !ruby/object:Gem::Dependency
71
+ name: bundler
72
+ prerelease: false
73
+ requirement: &id004 !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ~>
77
+ - !ruby/object:Gem::Version
78
+ hash: 23
79
+ segments:
80
+ - 1
81
+ - 0
82
+ - 0
83
+ version: 1.0.0
84
+ type: :development
85
+ version_requirements: *id004
86
+ - !ruby/object:Gem::Dependency
87
+ name: rack-test
88
+ prerelease: false
89
+ requirement: &id005 !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ~>
93
+ - !ruby/object:Gem::Version
94
+ hash: 5
95
+ segments:
96
+ - 0
97
+ - 5
98
+ - 7
99
+ version: 0.5.7
100
+ type: :development
101
+ version_requirements: *id005
102
+ description: |
103
+ Makes sure that for special jobs, there can be only one job with the same workload in one queue.
104
+
105
+ Example:
106
+ class CacheSweeper
107
+
108
+ include Resque::Plugins::UniqueJob
109
+
110
+ @queue = :cache_sweeps
111
+
112
+ def self.perform(article_id)
113
+ # Cache Me If You Can...
114
+ end
115
+ end
116
+
117
+ email:
118
+ - jannis@moviepilot.com
119
+ - ryan@instanceinc.com
120
+ executables: []
121
+
122
+ extensions: []
123
+
124
+ extra_rdoc_files: []
125
+
126
+ files:
127
+ - lib/resque-ext/job.rb
128
+ - lib/resque-ext/resque.rb
129
+ - lib/resque-loner/helpers.rb
130
+ - lib/resque-loner/unique_job.rb
131
+ - lib/resque-loner/version.rb
132
+ - lib/resque-loner.rb
133
+ - README.markdown
134
+ has_rdoc: true
135
+ homepage: http://github.com/ryansch/resque-loner
136
+ licenses: []
137
+
138
+ post_install_message:
139
+ rdoc_options: []
140
+
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ none: false
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ hash: 3
149
+ segments:
150
+ - 0
151
+ version: "0"
152
+ required_rubygems_version: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ hash: 3
158
+ segments:
159
+ - 0
160
+ version: "0"
161
+ requirements: []
162
+
163
+ rubyforge_project: resque-loner
164
+ rubygems_version: 1.6.2
165
+ signing_key:
166
+ specification_version: 3
167
+ summary: Adds unique jobs to resque
168
+ test_files: []
169
+