resque-dedup 0.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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in resque-dedup.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ Resque Dedup
2
+ ============
3
+
4
+ A [Resque][rq] plugin. Until [Datagraph's][dg] patched version of Resque
5
+ is merged, you will need to use the `datagraph` branch of our [Resque
6
+ repository][dgrq]
7
+
8
+ If you want only one instance of a particular job to be enqueued at any
9
+ one time, extend it with this module.
10
+
11
+ For example:
12
+
13
+ require 'resque/plugins/dedup'
14
+
15
+ class DoSomeHeavyLifting
16
+ extend Resque::Plugins::Dedup
17
+
18
+ def self.perform(some_id)
19
+ heavy_lifting
20
+ end
21
+ end
22
+
23
+ Only one instance of this job will be enqueued at any one time. The job is
24
+ identified by a `lock` key which is a combination of its name and the arguments
25
+ supplied to it.
26
+
27
+ If you want to define this key yourself, you can override the `lock` class method
28
+ in your subclass, e.g.
29
+
30
+ class DoSomeHeavyLifting
31
+ extend Resque::Plugins::Lock
32
+
33
+ # Run only one at a time, regardless of some_id.
34
+ def self.lock(some_id)
35
+ "DoSomeHeavyLifting"
36
+ end
37
+
38
+ def self.perform(some_id)
39
+ heavy_lifting
40
+ end
41
+ end
42
+
43
+ The above modification will ensure only one job of class
44
+ DoSomeHeavyLifting is enqueued at a time, regardless of the
45
+ some_id.
46
+
47
+ [rq]: http://github.com/defunkt/resque
48
+ [dg]: http://datagraph.org
49
+ [dgrq]: http://github.com/datagraph/resque
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake/testtask'
5
+ task :default => :test
6
+
7
+ Rake::TestTask.new do |test|
8
+ test.libs << "test"
9
+ test.test_files = FileList['test/**/*_test.rb']
10
+ end
@@ -0,0 +1,35 @@
1
+ module Resque
2
+ module Plugins
3
+ module Dedup
4
+ VERSION = "0.0.1"
5
+
6
+ # Override in your job to control the lock key. It is
7
+ # passed the same arguments as `perform`, that is, your job's
8
+ # payload.
9
+ def lock(*args)
10
+ "lock:#{name}-#{args.to_s}"
11
+ end
12
+
13
+ # Convenience method, not used internally.
14
+ def locked?(*args)
15
+ Resque.redis.exists(lock(*args))
16
+ end
17
+
18
+ def before_enqueue_lock(*args)
19
+ return false unless Resque.redis.setnx(lock(*args), true)
20
+ true
21
+ end
22
+
23
+ # Cleanup the key after the job is done.
24
+ def around_perform_lock(*args)
25
+ begin
26
+ yield
27
+ ensure
28
+ # Always clear the lock when we're done, even if there is an
29
+ # error.
30
+ Resque.redis.del(lock(*args))
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "resque/plugins/dedup"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "resque-dedup"
7
+ s.version = Resque::Plugins::Dedup::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Josh Huckabee"]
10
+ s.email = ["joshhuckabee@gmail.com"]
11
+ s.homepage = "http://github.com/datagraph/resque-dedup"
12
+ s.summary = %q{A Resque plugin for ensuring that the same job does not get enqueued multiple times}
13
+ s.description = %q{A Resque plugin for ensuring that the same job does not get enqueued multiple times}
14
+
15
+ s.rubyforge_project = "resque-dedup"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,28 @@
1
+ require 'test/unit'
2
+ require 'resque'
3
+ require 'resque/plugins/dedup'
4
+
5
+ class Job
6
+ extend Resque::Plugins::Dedup
7
+ @queue = :test
8
+
9
+ def self.perform
10
+ sleep 1
11
+ end
12
+ end
13
+
14
+ class DedupTest < Test::Unit::TestCase
15
+ def test_lint
16
+ assert_nothing_raised do
17
+ Resque::Plugin.lint(Resque::Plugins::Dedup)
18
+ end
19
+ end
20
+
21
+ def test_dedup
22
+ 3.times { Resque.enqueue(Job) }
23
+ assert_equal 1, Resque.size(:test)
24
+ worker = Resque::Worker.new(:test)
25
+ worker.process
26
+ assert_equal 0, Resque.size(:test)
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resque-dedup
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Josh Huckabee
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-10 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: A Resque plugin for ensuring that the same job does not get enqueued multiple times
18
+ email:
19
+ - joshhuckabee@gmail.com
20
+ executables: []
21
+
22
+ extensions: []
23
+
24
+ extra_rdoc_files: []
25
+
26
+ files:
27
+ - Gemfile
28
+ - README.md
29
+ - Rakefile
30
+ - lib/resque/plugins/dedup.rb
31
+ - resque-dedup.gemspec
32
+ - test/dedup_test.rb
33
+ has_rdoc: true
34
+ homepage: http://github.com/datagraph/resque-dedup
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ requirements: []
55
+
56
+ rubyforge_project: resque-dedup
57
+ rubygems_version: 1.6.2
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: A Resque plugin for ensuring that the same job does not get enqueued multiple times
61
+ test_files: []
62
+