resque-one 1.0.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
+ SHA256:
3
+ metadata.gz: 8c0556c80e549f3e8633256222bc399828b978bbd6f948fe546d26cd3ceadde2
4
+ data.tar.gz: bedb58a9c4a9f4d856f021389b0d7c51122bc444c6cca1720228c1e3b857396b
5
+ SHA512:
6
+ metadata.gz: 847b705d23d9f02cb36646f96ba38bdfaf606d41dcf39cd28e66bf6f95178cca2d486b778b528dcc83d8b5077a7613be16d778e94569c359739b480cb1ca9179
7
+ data.tar.gz: ef95240af85388fd81ae1ff6c4db0ccc6cb173fdf4b2fd68db4670254b5a4a542106134ef9edbe1afe6a0c4af9ef122c3f6bfb52b170dc2ff037f5c14d10ebb3
data/.coveralls.yml ADDED
@@ -0,0 +1,2 @@
1
+ service_name: github-actions
2
+ repo_token: rbqp7Q8NEyVxbjc0dulpiHIKNVQMdmkcM
@@ -0,0 +1,29 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [ '**' ]
6
+ pull_request:
7
+ branches: [ '**' ]
8
+
9
+ jobs:
10
+ test:
11
+
12
+ name: Tests
13
+ runs-on: ubuntu-latest
14
+ strategy:
15
+ matrix:
16
+ ruby-version: ['2.0', '2.1', '2.2', '2.3', '2.4', '2.5', '2.6', '2.7', '3.0', 'jruby-9.2.9.0']
17
+
18
+ steps:
19
+ - uses: actions/checkout@v3
20
+ - uses: zhulik/redis-action@1.1.0
21
+ with:
22
+ redis version: '6'
23
+ - name: Set up Ruby
24
+ uses: ruby/setup-ruby@v1
25
+ with:
26
+ ruby-version: ${{ matrix.ruby-version }}
27
+ bundler-cache: true
28
+ - name: Run tests
29
+ run: bundle exec rake
data/.gitignore ADDED
@@ -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/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ resque-one
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.3.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hash_ext.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Gabriel Naiman
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,51 @@
1
+ # Resque::One
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/resque-one.svg)](https://rubygems.org/gems/resque-one)
4
+ [![CI](https://github.com/gabynaiman/resque-one/actions/workflows/ci.yml/badge.svg)](https://github.com/gabynaiman/resque-one/actions/workflows/ci.yml)
5
+ [![Coverage Status](https://coveralls.io/repos/gabynaiman/resque-one/badge.svg?branch=master)](https://coveralls.io/r/gabynaiman/resque-one?branch=master)
6
+ [![Code Climate](https://codeclimate.com/github/gabynaiman/resque-one.svg)](https://codeclimate.com/github/gabynaiman/resque-one)
7
+
8
+ Resque plugin to specify uniq jobs with the same payload per queue. Have support for resque-status.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'resque-one'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install resque-one
25
+
26
+ ## Usage
27
+
28
+ ```ruby
29
+ class MyJob
30
+ extend Resque::Plugins::One
31
+ end
32
+ ```
33
+
34
+ or
35
+
36
+ ```ruby
37
+ class MyJob
38
+ def self.one?
39
+ true
40
+ end
41
+ end
42
+ ```
43
+
44
+ ## Contributing
45
+
46
+ Bug reports and pull requests are welcome on GitHub at https://github.com/gabynaiman/resque-one.
47
+
48
+
49
+ ## License
50
+
51
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,25 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:spec) do |t|
5
+ t.libs << 'spec'
6
+ t.libs << 'lib'
7
+ t.pattern = ENV['DIR'] ? File.join(ENV['DIR'], '**', '*_spec.rb') : 'spec/**/*_spec.rb'
8
+ t.verbose = false
9
+ t.warning = false
10
+ t.loader = nil if ENV['TEST']
11
+ ENV['TEST'], ENV['LINE'] = ENV['TEST'].split(':') if ENV['TEST'] && !ENV['LINE']
12
+ t.options = ''
13
+ t.options << "--name=/#{ENV['NAME']}/ " if ENV['NAME']
14
+ t.options << "-l #{ENV['LINE']} " if ENV['LINE'] && ENV['TEST']
15
+ end
16
+
17
+ task default: :spec
18
+
19
+ desc 'Pry console'
20
+ task :console do
21
+ require 'resque-one'
22
+ require 'pry'
23
+ ARGV.clear
24
+ Pry.start
25
+ end
@@ -0,0 +1,43 @@
1
+ module Resque
2
+ module One
3
+ class JobInfo
4
+
5
+ attr_reader :klass, :id, :args
6
+
7
+ def self.parse(job_payload)
8
+ new job_payload['class'], job_payload['args']
9
+ end
10
+
11
+ def initialize(klass, args)
12
+ @klass = klass.is_a?(Class) ? klass : Consty.get(klass)
13
+
14
+ if include_plugin_status?
15
+ @id = args.first
16
+ @args = args[1..-1]
17
+ else
18
+ @id = nil
19
+ @args = args
20
+ end
21
+ end
22
+
23
+ def key
24
+ @key ||= "#{klass.name}:#{args_digest}"
25
+ end
26
+
27
+ def one?
28
+ klass.respond_to?(:one?) ? klass.one? : false
29
+ end
30
+
31
+ private
32
+
33
+ def args_digest
34
+ Digest::SHA1.hexdigest JSON.dump(args)
35
+ end
36
+
37
+ def include_plugin_status?
38
+ klass.ancestors.include? Resque::Plugins::Status
39
+ end
40
+
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,50 @@
1
+ module Resque
2
+ module One
3
+ class QueueLocker
4
+
5
+ PREFIX = 'resque-one'.freeze
6
+
7
+ attr_reader :redis, :queue
8
+
9
+ def initialize(redis, queue)
10
+ @redis = redis
11
+ @queue = queue
12
+ end
13
+
14
+ def locked?(job_info)
15
+ redis.keys(key_for(job_info)).any?
16
+ end
17
+
18
+ def lock(job_info)
19
+ return false if locked? job_info
20
+
21
+ job_key = key_for job_info
22
+ redis.set job_key, job_info.id
23
+
24
+ true
25
+ end
26
+
27
+ def unlock(job_info)
28
+ redis.del key_for(job_info)
29
+ end
30
+
31
+ def unlock_all(klass=nil)
32
+ filter = klass ? "#{queue_key}:#{klass.to_s}:*" : "#{queue_key}:*"
33
+ redis.keys(filter).each do |key|
34
+ redis.del key
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def queue_key
41
+ "#{PREFIX}:#{queue}"
42
+ end
43
+
44
+ def key_for(job_info)
45
+ "#{queue_key}:#{job_info.key}"
46
+ end
47
+
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,54 @@
1
+ module Resque
2
+ module One
3
+ module ResqueExt
4
+
5
+ def enqueue_to(queue, klass, *args)
6
+ job_info = JobInfo.new klass, args
7
+ queue_locker = QueueLocker.new redis, queue
8
+
9
+ if !job_info.one? || queue_locker.lock(job_info)
10
+ super queue, job_info.klass, *args
11
+ else
12
+ nil
13
+ end
14
+ end
15
+
16
+ def reserve(queue)
17
+ job = super queue
18
+ return nil if job.nil?
19
+
20
+ job_info = JobInfo.parse job.payload
21
+
22
+ if job_info.one?
23
+ queue_locker = QueueLocker.new redis, queue
24
+ queue_locker.unlock job_info
25
+ end
26
+
27
+ job
28
+ end
29
+
30
+ def dequeue(klass, *args)
31
+ job_info = JobInfo.new klass, args
32
+
33
+ if job_info.one?
34
+ queue_locker = QueueLocker.new redis, Resque.queue_from_class(job_info.klass)
35
+ if args.empty?
36
+ queue_locker.unlock_all job_info.klass
37
+ else
38
+ queue_locker.unlock job_info
39
+ end
40
+ end
41
+
42
+ super job_info.klass, *args
43
+ end
44
+
45
+ def remove_queue(queue)
46
+ queue_locker = QueueLocker.new redis, queue
47
+ queue_locker.unlock_all
48
+
49
+ super queue
50
+ end
51
+
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,5 @@
1
+ module Resque
2
+ module One
3
+ VERSION = '1.0.0'
4
+ end
5
+ end
data/lib/resque/one.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'resque'
2
+ require 'consty'
3
+ require_relative 'one/version'
4
+ require_relative 'one/job_info'
5
+ require_relative 'one/queue_locker'
6
+ require_relative 'one/resque_ext'
7
+ require_relative 'plugins/one'
8
+
9
+ module Resque
10
+ extend One::ResqueExt
11
+ end
@@ -0,0 +1,11 @@
1
+ module Resque
2
+ module Plugins
3
+ module One
4
+
5
+ def one?
6
+ true
7
+ end
8
+
9
+ end
10
+ end
11
+ end
data/lib/resque-one.rb ADDED
@@ -0,0 +1 @@
1
+ require_relative 'resque/one'
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'resque/one/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'resque-one'
8
+ spec.version = Resque::One::VERSION
9
+ spec.authors = ['Gabriel Naiman']
10
+ spec.email = ['gabynaiman@gmail.com']
11
+
12
+ spec.summary = 'Resque plugin to specify uniq jobs with the same payload per queue'
13
+ spec.description = 'Resque plugin to specify uniq jobs with the same payload per queue'
14
+ spec.homepage = 'https://github.com/gabynaiman/resque-one'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_runtime_dependency 'resque', '~> 1.25'
23
+ spec.add_runtime_dependency 'consty', '~> 1.0'
24
+
25
+ spec.add_development_dependency 'resque-status', '~> 0.5'
26
+ spec.add_development_dependency 'rake', '~> 11.0'
27
+ spec.add_development_dependency 'minitest', '~> 5.0', '< 5.11'
28
+ spec.add_development_dependency 'minitest-colorin', '~> 0.1'
29
+ spec.add_development_dependency 'minitest-line', '~> 0.6'
30
+ spec.add_development_dependency 'simplecov', '~> 0.14'
31
+ spec.add_development_dependency 'coveralls', '~> 0.8'
32
+ spec.add_development_dependency 'pry-nav', '~> 0.2'
33
+ end
metadata ADDED
@@ -0,0 +1,206 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resque-one
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Gabriel Naiman
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-08-20 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.25'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.25'
27
+ - !ruby/object:Gem::Dependency
28
+ name: consty
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: resque-status
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '11.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '11.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ - - "<"
77
+ - !ruby/object:Gem::Version
78
+ version: '5.11'
79
+ type: :development
80
+ prerelease: false
81
+ version_requirements: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - "~>"
84
+ - !ruby/object:Gem::Version
85
+ version: '5.0'
86
+ - - "<"
87
+ - !ruby/object:Gem::Version
88
+ version: '5.11'
89
+ - !ruby/object:Gem::Dependency
90
+ name: minitest-colorin
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '0.1'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '0.1'
103
+ - !ruby/object:Gem::Dependency
104
+ name: minitest-line
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '0.6'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: '0.6'
117
+ - !ruby/object:Gem::Dependency
118
+ name: simplecov
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: '0.14'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - "~>"
129
+ - !ruby/object:Gem::Version
130
+ version: '0.14'
131
+ - !ruby/object:Gem::Dependency
132
+ name: coveralls
133
+ requirement: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - "~>"
136
+ - !ruby/object:Gem::Version
137
+ version: '0.8'
138
+ type: :development
139
+ prerelease: false
140
+ version_requirements: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - "~>"
143
+ - !ruby/object:Gem::Version
144
+ version: '0.8'
145
+ - !ruby/object:Gem::Dependency
146
+ name: pry-nav
147
+ requirement: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - "~>"
150
+ - !ruby/object:Gem::Version
151
+ version: '0.2'
152
+ type: :development
153
+ prerelease: false
154
+ version_requirements: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - "~>"
157
+ - !ruby/object:Gem::Version
158
+ version: '0.2'
159
+ description: Resque plugin to specify uniq jobs with the same payload per queue
160
+ email:
161
+ - gabynaiman@gmail.com
162
+ executables: []
163
+ extensions: []
164
+ extra_rdoc_files: []
165
+ files:
166
+ - ".coveralls.yml"
167
+ - ".github/workflows/ci.yml"
168
+ - ".gitignore"
169
+ - ".ruby-gemset"
170
+ - ".ruby-version"
171
+ - Gemfile
172
+ - LICENSE.txt
173
+ - README.md
174
+ - Rakefile
175
+ - lib/resque-one.rb
176
+ - lib/resque/one.rb
177
+ - lib/resque/one/job_info.rb
178
+ - lib/resque/one/queue_locker.rb
179
+ - lib/resque/one/resque_ext.rb
180
+ - lib/resque/one/version.rb
181
+ - lib/resque/plugins/one.rb
182
+ - resque-one.gemspec
183
+ homepage: https://github.com/gabynaiman/resque-one
184
+ licenses:
185
+ - MIT
186
+ metadata: {}
187
+ post_install_message:
188
+ rdoc_options: []
189
+ require_paths:
190
+ - lib
191
+ required_ruby_version: !ruby/object:Gem::Requirement
192
+ requirements:
193
+ - - ">="
194
+ - !ruby/object:Gem::Version
195
+ version: '0'
196
+ required_rubygems_version: !ruby/object:Gem::Requirement
197
+ requirements:
198
+ - - ">="
199
+ - !ruby/object:Gem::Version
200
+ version: '0'
201
+ requirements: []
202
+ rubygems_version: 3.3.14
203
+ signing_key:
204
+ specification_version: 4
205
+ summary: Resque plugin to specify uniq jobs with the same payload per queue
206
+ test_files: []