active_job_resque_solo 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4831e5d461672f44d255b1adcfbd3c72650cbe25
4
+ data.tar.gz: 76626abe03c74ef1d7cc4135cebfccf101dac1ac
5
+ SHA512:
6
+ metadata.gz: d7fce0cef4cefd50565ba4a944c9c0069f780fd463484714761b4aba9c0badabfadf78f4c788bf24bbbe6fee2c3de5a57d79d9b2f22cd25f61efa89e8d7f4367
7
+ data.tar.gz: 98e612019969deda8fa4fb83adeea9e4ae91ee9f2abda4de41209056d40f38b08413bdb0e96ea210c70dd7fbf438fdaf67253061b97d2bd379f05e60e2e98165
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+
14
+ # byebug
15
+ .byebug_history
16
+
17
+ # vim
18
+ *.swp
19
+
20
+ # macOS
21
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.4
@@ -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 kinkadep@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,6 @@
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 active_job_resque_solo.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Phillip Kinkade
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.
data/README.md ADDED
@@ -0,0 +1,104 @@
1
+ # ActiveJobResqueSolo
2
+
3
+ A plugin for ActiveJob with Resque to prevent duplicate enqueing of jobs.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'active_job_resque_solo'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install active_job_resque_solo
20
+
21
+ ## Usage
22
+
23
+ In your job class, include the plugin:
24
+
25
+ ```ruby
26
+ class MyJob < ActiveJob::Base
27
+
28
+ include ActiveJob::Plugins::Resque::Solo
29
+
30
+ queue_as :default
31
+
32
+ def perform(*args); end
33
+ end
34
+ ```
35
+
36
+ If an instance of the job with matching arguments is either waiting for a worker or currently executing,
37
+ Solo will prevent a new instance of the job from being enqueued.
38
+
39
+ You can control which named arguments are used to determine uniqueness in the queue:
40
+
41
+ `solo_only_args`
42
+ `solo_except_args`
43
+
44
+ ```ruby
45
+ class MyJob < ActiveJob::Base
46
+
47
+ include ActiveJob::Plugins::Resque::Solo
48
+
49
+ # Only compare "user" args when checking for duplicate jobs.
50
+ solo_only_args :user
51
+
52
+ queue_as :default
53
+
54
+ def perform(user:, nonce:)
55
+ end
56
+ end
57
+ ```
58
+
59
+ Conversely, you can exclude arguments from being checked for duplicates. This
60
+ is useful when plugins add arguments of their own to your jobs.
61
+
62
+ ```ruby
63
+ class MyJob < ActiveJob::Base
64
+
65
+ include ActiveJob::Plugins::Resque::Solo
66
+
67
+ solo_except_args :nonce, :retry_count
68
+
69
+ queue_as :default
70
+
71
+ def perform(user:, nonce:)
72
+ end
73
+ end
74
+ ```
75
+
76
+ ## Duplicate enqueues are still possible
77
+
78
+ While this plugin will greatly reduce duplicate instances of a job from being
79
+ enqueued, there are two scenarios where duplicates may still be enqueued,
80
+ so be sure to check out other gems for locking if your job is not idempotent.
81
+
82
+ 1. When multiple processes simultaneously attempt to enqueue the same job two or
83
+ more may be enqueued.
84
+ 2. If your queue has many jobs, and workers remove a job during while Solo scans
85
+ the queue, it's possible for the original enqueued job to be missed. Solo will allow
86
+ the new instance of the job to be enqueued.
87
+
88
+ ## Development
89
+
90
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
91
+
92
+ 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).
93
+
94
+ ## Contributing
95
+
96
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/active_job_resque_solo. 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.
97
+
98
+ ## License
99
+
100
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
101
+
102
+ ## Code of Conduct
103
+
104
+ Everyone interacting in the ActiveJobResqueSolo project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/active_job_resque_solo/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "active_job_resque_solo/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_job_resque_solo"
8
+ spec.version = ActiveJobResqueSolo::VERSION
9
+ spec.authors = ["Phillip Kinkade"]
10
+ spec.email = ["kinkadep@gmail.com"]
11
+
12
+ spec.summary = %q{Prevents duplicate ActiveJob+Resque jobs from being enqueued.}
13
+ spec.description = %q{If you are using ActiveJob with the Resque Adapter, this gem will help prevent duplicate jobs, based on arguments, from being enqueued to Resque.}
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "rails", ">= 4.2.0", "< 6"
25
+
26
+ spec.add_development_dependency "byebug"
27
+ spec.add_development_dependency "bundler", "~> 1.15"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "active_job_resque_solo"
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__)
data/bin/setup ADDED
@@ -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,118 @@
1
+ module ActiveJob
2
+ module Plugins
3
+ module Resque
4
+ module Solo
5
+
6
+ def self.included(base_class)
7
+ base_class.extend(ClassMethods)
8
+
9
+ base_class.around_enqueue do |job, block|
10
+ base_class.solo_around_enqueue(job, block)
11
+ end
12
+ end
13
+
14
+ module ClassMethods
15
+ def resque_present?
16
+ ActiveJob::Base.queue_adapter == ActiveJob::QueueAdapters::ResqueAdapter
17
+ end
18
+
19
+ def solo_around_enqueue(job, block)
20
+ if resque_present?
21
+ # always ignore the ActiveJob symbol hash key.
22
+ @solo_except_args ||= []
23
+ @solo_except_args << "_aj_symbol_keys" unless @solo_except_args.include?("_aj_symbol_keys")
24
+
25
+ if !solo_job_enqueued?(job) && !solo_job_executing?(job)
26
+ block.call
27
+ end
28
+ else
29
+ # if resque is not present, always enqueue
30
+ block.call
31
+ end
32
+ end
33
+
34
+ def solo_only_args(*args)
35
+ @solo_only_args = args.compact.map(&:to_s).uniq
36
+ raise "Missing arguments for solo_only_args" if @solo_only_args.empty?
37
+ end
38
+
39
+ def solo_except_args(*args)
40
+ @solo_except_args = args.compact.map(&:to_s).uniq
41
+ raise "Missing arguments for solo_except_args" if @solo_except_args.empty?
42
+ end
43
+
44
+ def solo_job_enqueued?(job)
45
+ size = ::Resque.size(job.queue_name)
46
+ return false if size.zero?
47
+
48
+ page_size = 250
49
+ pages = (size/page_size).to_i + 1
50
+ jobs = []
51
+
52
+ # It's possible for this loop to skip jobs if they
53
+ # are dequeued while the loop is in progress.
54
+ (0..pages).each do |i|
55
+ page_start = i * page_size
56
+ page = ::Resque.peek(job.queue_name, page_start, page_size)
57
+ break if page.empty?
58
+ jobs += page
59
+ end
60
+
61
+ job_class, job_arguments = solo_job(job)
62
+
63
+ (jobs.size-1).downto(0) do |i|
64
+ scheduled_job = jobs[i]
65
+ return true if solo_job_enqueued_with_args?(job_class, job_arguments, scheduled_job)
66
+ end
67
+ false
68
+ end
69
+
70
+ def solo_job_executing?(job)
71
+ job_class, job_arguments = solo_job(job)
72
+
73
+ ::Resque.workers.any? do |worker|
74
+ processing = worker.processing
75
+ next false if processing.blank?
76
+ args = processing["payload"]["args"][0]
77
+ solo_job_with_args_eq?(job_class, job_arguments, args)
78
+ end
79
+ end
80
+
81
+ def solo_job_enqueued_with_args?(job_class, job_arguments, scheduled_job)
82
+ args = scheduled_job["args"][0]
83
+ solo_job_with_args_eq?(job_class, job_arguments, args)
84
+ end
85
+
86
+ def solo_job_with_args_eq?(job_class, job_arguments, wrapper_args)
87
+ return false if wrapper_args['job_class'] != job_class
88
+ encoded_arguments = wrapper_args['arguments']
89
+ encoded_arguments = solo_job_args(encoded_arguments)
90
+ encoded_arguments == job_arguments
91
+ end
92
+
93
+ def solo_job(job)
94
+ job_arguments = ActiveJob::Arguments.serialize(job.arguments)
95
+ job_arguments = solo_job_args(job_arguments)
96
+ job_class = job.class.name
97
+ [ job_class, job_arguments ]
98
+ end
99
+
100
+ def solo_job_args(args)
101
+ if args.present?
102
+ args.map do |arg|
103
+ if arg.is_a? Hash
104
+ arg.keep_if { |k,v| @solo_only_args.include?(k.to_s) } if @solo_only_args.present?
105
+ arg.keep_if { |k,v| !@solo_except_args.include?(k.to_s) } if @solo_except_args.present?
106
+ end
107
+
108
+ arg
109
+ end
110
+ end
111
+
112
+ args
113
+ end
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,3 @@
1
+ require "active_job_resque_solo/version"
2
+ require 'active_job/plugins/resque/solo'
3
+
@@ -0,0 +1,3 @@
1
+ module ActiveJobResqueSolo
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_job_resque_solo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Phillip Kinkade
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-09-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.2.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '6'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 4.2.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '6'
33
+ - !ruby/object:Gem::Dependency
34
+ name: byebug
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.15'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.15'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '10.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '10.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rspec
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '3.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '3.0'
89
+ description: If you are using ActiveJob with the Resque Adapter, this gem will help
90
+ prevent duplicate jobs, based on arguments, from being enqueued to Resque.
91
+ email:
92
+ - kinkadep@gmail.com
93
+ executables: []
94
+ extensions: []
95
+ extra_rdoc_files: []
96
+ files:
97
+ - ".gitignore"
98
+ - ".rspec"
99
+ - ".travis.yml"
100
+ - CODE_OF_CONDUCT.md
101
+ - Gemfile
102
+ - LICENSE.txt
103
+ - README.md
104
+ - Rakefile
105
+ - active_job_resque_solo.gemspec
106
+ - bin/console
107
+ - bin/setup
108
+ - lib/active_job/plugins/resque/solo.rb
109
+ - lib/active_job_resque_solo.rb
110
+ - lib/active_job_resque_solo/version.rb
111
+ homepage:
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.6.6
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Prevents duplicate ActiveJob+Resque jobs from being enqueued.
135
+ test_files: []