resque-duplicateless 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cc404ee81cf0ef2dfe4c9ae0defe27811529a5a9
4
+ data.tar.gz: 68ddd9acd5e4a57ce9a30c781b4e23ecc919c0ff
5
+ SHA512:
6
+ metadata.gz: 81dff25c9928f776e3bf773b181a38d284938be1149e78de9e044d4d0c0aa3df19df3f5a002bd0856d859430d9141953621b9a71b558ea8d1399b3a4d42aa3fc
7
+ data.tar.gz: 0e9136afb45892a53519ac51dbcd8c0d9fac1ef77d71bd2a9c36ab3b35d74ad381c66eb464337d655f014ff097baca0c569ea0bff34fbd04a7285d59d90e098e
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at deathbyjer@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in resque_unique_job.gemspec
6
+ gemspec
7
+
8
+ gem "redis"
9
+ gem "rspec"
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,67 @@
1
+ # ResqueUniqueJob
2
+
3
+ Resque is awesome. It's great at queuing up lots of jobs and processing them offline. It works making use of Redis, which is extraordinarily fast.
4
+
5
+ However, Resque makes use of Redis's lists, which means that the entries are not unique. And this is usually what we want. But sometimes it's not.
6
+
7
+ ResqueUniqueJob gives us the option of uniquely adding a job to a queue. This will ensure that the job will only be added to the queue if no other job matching its class and arguments already exists on the queue.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'resque_unique_job'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install resque_unique_job
24
+
25
+ ## Usage
26
+
27
+ Making a job unique is super easy.
28
+
29
+ In a normal Resque environment, we would enqueue a job by using the following method
30
+
31
+ Resque.enqueue FooClass, argument_1, argument_2, ...
32
+
33
+ In order to uniquely enqueue that class, we do the following.
34
+
35
+ Resque.enqueue_uniquely FooClass, argument_1, argument_2, ...
36
+
37
+ Also, a correlated method to `enqueue_to` exists in `enqueue_uniquely_to`
38
+
39
+
40
+ ## Development
41
+
42
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
43
+
44
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
45
+
46
+ Please make sure to run `bundle exec rspec` if you make any changes to make sure they are not breaking. Rspec expects that there is a Redis process set up on the machine and makes use of the keys `ruj:rspec:queue_1` and `ruj:rspec:queue_1`
47
+
48
+ *WARNING* - The Rspec will delete everything on the `ruj:rspec:queue_1` and `ruj:rspec:queue_2` queues. Please make sure you don't need them for anything before testing or change the redis database in the `spec/helpers/spec_init.rb` file.
49
+
50
+ ## Contributing
51
+
52
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/resque_unique_job. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
53
+
54
+ ## License
55
+
56
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
57
+
58
+ ## Code of Conduct
59
+
60
+ Everyone interacting in the ResqueUniqueJob project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/resque_unique_job/blob/master/CODE_OF_CONDUCT.md).
61
+
62
+
63
+ ### Authors
64
+
65
+ This gem was authored by the following:
66
+
67
+ Jeremy Linder (deathbyjer)
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "resque_unique_job"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,6 @@
1
+ require "resque-duplicateless/version"
2
+
3
+ Dir[File.dirname(__FILE__) + "/**/*.rb"].each {|f| require f}
4
+
5
+ module ResqueDuplicateless
6
+ end
@@ -0,0 +1,65 @@
1
+ # We are going to make a extension that kludges a couple of methods into the Resque main object
2
+ module ResqueDuplicateless
3
+ module Ext
4
+ module Resque
5
+ def unique_push(queue, item)
6
+ data_store.uniquely_push_to_queue queue, encode(item)
7
+ end
8
+
9
+ # This method is basically a copy of "enqueue", but it will make sure
10
+ # that the enqueue method happens uniquely using this plugin
11
+ def enqueue_uniquely(klass, *args)
12
+ enqueue_uniquely_to queue_from_class(klass), klass, *args
13
+ end
14
+
15
+ # We need to rewrite enqueue_to in order to use enqueue_with-options
16
+ def enqueue_to(queue, klass, *args)
17
+ enqueue_with_options :queue => queue, :class => klass, :args => args
18
+ end
19
+
20
+ # This is basically a copy of enqueue_to, but will make sure that the enqueue
21
+ # happens uniquely using this plugin
22
+ def enqueue_uniquely_to(queue, klass, *args)
23
+ enqueue_with_options :queue => queue, :class => klass, :args => args, :unique => true
24
+ end
25
+
26
+ # We are going to create a new "base" job creation method. This will allow us to send options
27
+ # into the enqueue function and, eventually, the Job.create function
28
+ def enqueue_with_options(options = {})
29
+ surround_enqueue(options) do
30
+ ::Resque::Job.create_with_options(options)
31
+ end
32
+ end
33
+
34
+ protected
35
+
36
+ # We are goung to do a surrounding enqueue function so that the hooks are called in a separate function,
37
+ # should we need to make changes to them separately for some reason. We could have done this in two functions,
38
+ # but because the before and after hooks should have the same basic options and form, we do it in one
39
+ #
40
+ # The code here is basically ripped straight from Resque (commit 0420094)
41
+ def surround_enqueue(options)
42
+ queue, klass, args = options[:queue], options[:class], options[:args]
43
+
44
+ # Perform before_enqueue hooks. Don't perform enqueue if any hook returns false
45
+ before_hooks = ::Resque::Plugin.before_enqueue_hooks(klass).collect do |hook|
46
+ klass.send(hook, *args)
47
+ end
48
+ return nil if before_hooks.any? { |result| result == false }
49
+
50
+ yield
51
+
52
+ ::Resque::Plugin.after_enqueue_hooks(klass).each do |hook|
53
+ klass.send(hook, *args)
54
+ end
55
+
56
+ return true
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ # Perform the actual kludge
63
+ module Resque
64
+ extend ResqueDuplicateless::Ext::Resque
65
+ end
@@ -0,0 +1,85 @@
1
+ # We need to kludge in the method to uniquely add an item into a queue
2
+ #
3
+ # This is a little bit complicated because of how Resque is built. It uses an internal object known
4
+ # as the QueueAccess object to actually interact with the queues. Because of this, we want to extend
5
+ # those objects to follow their practices
6
+ module ResqueDuplicateless
7
+ module Ext
8
+ module Resque
9
+ module DataStore
10
+
11
+ def uniquely_push_to_queue(queue, item)
12
+ @queue_access.uniquely_push_to_queue queue, item
13
+ end
14
+
15
+ # This is the extension for the QueueAccess Object
16
+ module QueueAccess
17
+
18
+ # This function calls our LUA script for evaluation.
19
+ def uniquely_push_to_queue(queue, item)
20
+ out = nil
21
+ # Ensure the script is loaded
22
+ @redis.redis.script(:load, uniquely_push_to_queue_script) unless @redis.redis.script(:exists, uniquely_push_to_queue_script_sha)
23
+
24
+ # Now pipeline the watch and addition calls
25
+ @redis.pipelined do
26
+ watch_queue queue
27
+ out = @redis.evalsha uniquely_push_to_queue_script_sha, [redis_key_for_queue_also(queue)], [item]
28
+ end
29
+ out.value
30
+ end
31
+
32
+ protected
33
+
34
+ # TODO: Resque queue names should really be protected, not private
35
+ def redis_key_for_queue_also(queue)
36
+ "queue:#{queue}"
37
+ end
38
+
39
+ # Create the SHA once and save it to avoid doing it repeatedly for no reason
40
+ def uniquely_push_to_queue_script_sha
41
+ @uniquely_push_to_queue_script_sha = Digest::SHA1.hexdigest uniquely_push_to_queue_script unless @uniquely_push_to_queue_script_sha
42
+ @uniquely_push_to_queue_script_sha
43
+ end
44
+
45
+ # The LUA script to uniquely add to queue
46
+ #
47
+ # It *always* runs O(n)
48
+ #
49
+ # It has two parts
50
+ # 1. Use RPOPLPUSH to cycle through the entire array.
51
+ # It must complete the cycle, even if it finds something, so that the list's order is maintained
52
+ # 1A. If the list is empty, then we don't need to do this. Non-existant lists are considered empty
53
+ #
54
+ # 2. If it does not find the item, then add it to the tail of the list
55
+ def uniquely_push_to_queue_script
56
+ <<-FOO
57
+ local list_size = redis.call('LLEN',KEYS[1])
58
+ local response = 0
59
+ if list_size > 0 then
60
+ for i=1,list_size
61
+ do
62
+ redis.call('RPOPLPUSH',KEYS[1],KEYS[1])
63
+ if ARGV[1] == redis.call('LINDEX',KEYS[1],0) then response = 1 end
64
+ end
65
+ end
66
+ if response == 0 then redis.call('RPUSH',KEYS[1],ARGV[1]) end
67
+ return 1
68
+ FOO
69
+ end
70
+
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+
77
+ # Now just kludge these methods in
78
+ module Resque
79
+ class DataStore
80
+ include ResqueDuplicateless::Ext::Resque::DataStore
81
+ class QueueAccess
82
+ include ResqueDuplicateless::Ext::Resque::DataStore::QueueAccess
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,40 @@
1
+ # We have to overload the Job function to manufacture a new "create_with_options" method
2
+ #
3
+ # This method will allow us to actually perform the correct type of queuing, IE whether
4
+ # we want to do a unique enqueue or not
5
+ module ResqueDuplicateless
6
+ module Ext
7
+ module Resque
8
+ module Job
9
+ def create(queue, klass, *args)
10
+ create_with_options :queue => queue, :class => klass, :args => args
11
+ end
12
+
13
+ def create_with_options(options = {})
14
+ queue, klass, args = options[:queue], options[:class], options[:args]
15
+ ::Resque.validate(klass, queue)
16
+
17
+ if ::Resque.inline?
18
+ # Instantiating a Resque::Job and calling perform on it so callbacks run
19
+ # decode(encode(args)) to ensure that args are normalized in the same manner as a non-inline job
20
+ new(:inline, {'class' => klass, 'args' => decode(encode(args))}).perform
21
+ # FINALLY, a unique push!
22
+ elsif options[:unique]
23
+ ::Resque.unique_push(queue, :class => klass.to_s, :args => args)
24
+ else
25
+ ::Resque.push(queue, :class => klass.to_s, :args => args)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+
34
+
35
+ # Now just kludge these methods in
36
+ module Resque
37
+ class Job
38
+ extend ResqueDuplicateless::Ext::Resque::Job
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module ResqueDuplicateless
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "resque-duplicateless/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "resque-duplicateless"
8
+ spec.version = ResqueDuplicateless::VERSION
9
+ spec.authors = ["Jeremy Linder"]
10
+ spec.email = ["jeremy@nomibeauty.com"]
11
+
12
+ spec.summary = %q{Allow for unique Resque Jobs on the queue}
13
+ spec.description = %q{Resque allows a single queue to have multiple jobs of the same time. This is because it uses a redis list, which does not have the concept of a unique entry. Sometimes, however, a particular value is only needed if it isn't already on the queue.}
14
+ spec.homepage = "https://www.nomibeauty.com"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_dependency "resque", "~> 1.x"
34
+
35
+ spec.add_development_dependency "bundler", "~> 1.15"
36
+ spec.add_development_dependency "rake", "~> 10.0"
37
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resque-duplicateless
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeremy Linder
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-11-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: resque
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.x
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.x
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.15'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.15'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Resque allows a single queue to have multiple jobs of the same time.
56
+ This is because it uses a redis list, which does not have the concept of a unique
57
+ entry. Sometimes, however, a particular value is only needed if it isn't already
58
+ on the queue.
59
+ email:
60
+ - jeremy@nomibeauty.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - ".gitignore"
66
+ - ".rspec"
67
+ - CODE_OF_CONDUCT.md
68
+ - Gemfile
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - lib/resque-duplicateless.rb
75
+ - lib/resque-duplicateless/ext/resque.rb
76
+ - lib/resque-duplicateless/ext/resque/data_store.rb
77
+ - lib/resque-duplicateless/ext/resque/job.rb
78
+ - lib/resque-duplicateless/version.rb
79
+ - resque-duplicateless.gemspec
80
+ homepage: https://www.nomibeauty.com
81
+ licenses:
82
+ - MIT
83
+ metadata:
84
+ allowed_push_host: https://rubygems.org
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.4.5.2
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Allow for unique Resque Jobs on the queue
105
+ test_files: []