que-locks 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rubocop.yml +33 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +22 -0
- data/Gemfile.lock +80 -0
- data/LICENSE.txt +21 -0
- data/README.md +99 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/lint +4 -0
- data/bin/lintfix +4 -0
- data/bin/setup +8 -0
- data/lib/que/locks.rb +11 -0
- data/lib/que/locks/execution_lock.rb +54 -0
- data/lib/que/locks/job_extensions.rb +29 -0
- data/lib/que/locks/json_extensions.rb +11 -0
- data/lib/que/locks/lock_middleware.rb +23 -0
- data/lib/que/locks/version.rb +5 -0
- data/que-locks.gemspec +31 -0
- metadata +133 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: bd840db1d95e1dc04b8efae206dfd9600423999972af509b37266187d53dd535
|
4
|
+
data.tar.gz: 3505096ad35333c9b2fb00397e8c3ba5266097f8d180e9a0fb76d5ba9b12e16a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9aaee5425dc4819614715e3364f608d03bb2985b1a05420acb0fcc7ef500a63398ddfa19137dc6762a9747346cd72bb27747b27cfaa729fc2f56b93e4d833e3f
|
7
|
+
data.tar.gz: adb3ad01415719ad0845ff3141f3e102a2edde81b2ff9fa68cfe3fac68d41c6c6fdebf76e447e3112a9639c57244baa5f48938903dc1b7c3b8b0405862458935
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
AllCops:
|
2
|
+
Exclude:
|
3
|
+
- "vendor/**/*"
|
4
|
+
- "node_modules/**/*"
|
5
|
+
- "test/scratch/**/*"
|
6
|
+
TargetRubyVersion: 2.6
|
7
|
+
|
8
|
+
require:
|
9
|
+
- rubocop-performance
|
10
|
+
|
11
|
+
Security:
|
12
|
+
Enabled: true
|
13
|
+
|
14
|
+
Performance:
|
15
|
+
Enabled: true
|
16
|
+
|
17
|
+
Layout:
|
18
|
+
Enabled: false
|
19
|
+
|
20
|
+
Metrics:
|
21
|
+
Enabled: false
|
22
|
+
|
23
|
+
Style:
|
24
|
+
Enabled: false
|
25
|
+
|
26
|
+
Naming/MemoizedInstanceVariableName:
|
27
|
+
Enabled: false
|
28
|
+
|
29
|
+
Lint/AmbiguousBlockAssociation:
|
30
|
+
Enabled: false
|
31
|
+
|
32
|
+
Bundler/OrderedGems:
|
33
|
+
Enabled: false
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -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 harry.brundage@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,22 @@
|
|
1
|
+
source "https://rubygems.org"
|
2
|
+
|
3
|
+
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
|
4
|
+
|
5
|
+
gem 'que', '1.0.0.beta3', github: 'chanks/que'
|
6
|
+
|
7
|
+
# Specify your gem's dependencies in que-locks.gemspec
|
8
|
+
gemspec
|
9
|
+
|
10
|
+
group :development do
|
11
|
+
gem 'activerecord'
|
12
|
+
gem 'pg'
|
13
|
+
gem 'database_cleaner'
|
14
|
+
|
15
|
+
gem 'minitest'
|
16
|
+
|
17
|
+
gem 'byebug'
|
18
|
+
gem 'rufo'
|
19
|
+
gem 'rubocop'
|
20
|
+
gem 'rubocop-performance'
|
21
|
+
end
|
22
|
+
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
GIT
|
2
|
+
remote: https://github.com/chanks/que
|
3
|
+
revision: 53106609b24d7e8bc231ae3883f69dca8c989d9d
|
4
|
+
specs:
|
5
|
+
que (1.0.0.beta3)
|
6
|
+
|
7
|
+
PATH
|
8
|
+
remote: .
|
9
|
+
specs:
|
10
|
+
que-locks (0.2.0)
|
11
|
+
neatjson
|
12
|
+
que (~> 1.0.0.beta3)
|
13
|
+
xxhash
|
14
|
+
|
15
|
+
GEM
|
16
|
+
remote: https://rubygems.org/
|
17
|
+
specs:
|
18
|
+
activemodel (6.0.1)
|
19
|
+
activesupport (= 6.0.1)
|
20
|
+
activerecord (6.0.1)
|
21
|
+
activemodel (= 6.0.1)
|
22
|
+
activesupport (= 6.0.1)
|
23
|
+
activesupport (6.0.1)
|
24
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
25
|
+
i18n (>= 0.7, < 2)
|
26
|
+
minitest (~> 5.1)
|
27
|
+
tzinfo (~> 1.1)
|
28
|
+
zeitwerk (~> 2.2)
|
29
|
+
ast (2.4.0)
|
30
|
+
byebug (11.0.1)
|
31
|
+
concurrent-ruby (1.1.5)
|
32
|
+
database_cleaner (1.7.0)
|
33
|
+
i18n (1.7.0)
|
34
|
+
concurrent-ruby (~> 1.0)
|
35
|
+
jaro_winkler (1.5.4)
|
36
|
+
minitest (5.13.0)
|
37
|
+
neatjson (0.9)
|
38
|
+
parallel (1.19.1)
|
39
|
+
parser (2.6.5.0)
|
40
|
+
ast (~> 2.4.0)
|
41
|
+
pg (1.1.4)
|
42
|
+
rainbow (3.0.0)
|
43
|
+
rake (10.5.0)
|
44
|
+
rubocop (0.76.0)
|
45
|
+
jaro_winkler (~> 1.5.1)
|
46
|
+
parallel (~> 1.10)
|
47
|
+
parser (>= 2.6)
|
48
|
+
rainbow (>= 2.2.2, < 4.0)
|
49
|
+
ruby-progressbar (~> 1.7)
|
50
|
+
unicode-display_width (>= 1.4.0, < 1.7)
|
51
|
+
rubocop-performance (1.5.1)
|
52
|
+
rubocop (>= 0.71.0)
|
53
|
+
ruby-progressbar (1.10.1)
|
54
|
+
rufo (0.7.0)
|
55
|
+
thread_safe (0.3.6)
|
56
|
+
tzinfo (1.2.5)
|
57
|
+
thread_safe (~> 0.1)
|
58
|
+
unicode-display_width (1.6.0)
|
59
|
+
xxhash (0.4.0)
|
60
|
+
zeitwerk (2.2.1)
|
61
|
+
|
62
|
+
PLATFORMS
|
63
|
+
ruby
|
64
|
+
|
65
|
+
DEPENDENCIES
|
66
|
+
activerecord
|
67
|
+
bundler (~> 1.17)
|
68
|
+
byebug
|
69
|
+
database_cleaner
|
70
|
+
minitest
|
71
|
+
pg
|
72
|
+
que (= 1.0.0.beta3)!
|
73
|
+
que-locks!
|
74
|
+
rake (~> 10.0)
|
75
|
+
rubocop
|
76
|
+
rubocop-performance
|
77
|
+
rufo
|
78
|
+
|
79
|
+
BUNDLED WITH
|
80
|
+
1.17.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Harry Brundage
|
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,99 @@
|
|
1
|
+
# Que::Locks
|
2
|
+
|
3
|
+
`que-locks` adds an opt-in feature to `Que::Jobs` allowing jobs to specify that exactly one instance of a job should be executing at once. This is useful for jobs that are doing something important that should only ever happen one at a time, like processing a payment for a given user, or super expensive jobs that could cause thundering herd problems if enqueued all at the same time.
|
4
|
+
|
5
|
+
`que-locks` uses Postgres' advisory locks in a similar manner as que `0.x` did to provide scalable and automatically cleaned up locking around job execution. `que-locks` provides slightly better atomicity guarantees compared to the locking functionality of Redis based job queues for the same reasons `que` can as well. Because locks are taken and released using the same database connection that the `que` worker uses to pull jobs, the transactional semantics apply just the same where locks are automatically released if the connection fails, unlike the multistep Redis logic that requires complicated crash cleanup.
|
6
|
+
|
7
|
+
This is also sometimes called "unique jobs", "job concurrency limiting", and "exclusive jobs".
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'que-locks'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install que-locks
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
After requiring the gem, set the `exclusive_execution_lock` property on your job class:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
class SomeJob < Que::Job
|
31
|
+
self.exclusive_execution_lock = true
|
32
|
+
|
33
|
+
def run(user_id:, bar:)
|
34
|
+
# useful stuff
|
35
|
+
end
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
That's it!
|
40
|
+
|
41
|
+
#### Checking lock status
|
42
|
+
|
43
|
+
Occasionally, code enqueuing a job might want to check if the job is already running and do something different if it is, like display a message to the user or log the skipped execution. `que-locks` supports some basic job lock introspection like so:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
SomeJobClass.exclusive_execution_lock #=> returns true if the job is indeed using que-locks
|
47
|
+
|
48
|
+
SomeJobClass.lock_available?(user_id: 1) #=> returns true if no job is currently enqueued with these arguments or running right now holding the lock
|
49
|
+
```
|
50
|
+
|
51
|
+
Note that checking the lock's availability reports on the current state of the locks, but that state might change in between when the check is made and if/when the job is enqueued with the same arguments. Put differently, the `#lock_available?` method is advisory to user code, and doesn't actaully reserve the lock or execute a compare-and-swap operation. It's safe for multiple processes to race to enqueue a job after checking to see that the lock is available, as only one will still be executed, but they may both report that the lock was available before enqueuing.
|
52
|
+
|
53
|
+
## Semantics
|
54
|
+
|
55
|
+
`que-locks` guarantees that the maximum number of instances given job class executing with a given set of arguments will be one. This means that:
|
56
|
+
|
57
|
+
- two of the same job class each pushed with different arguments can both execute simultaneously
|
58
|
+
- if the job takes no arguments, there can only ever be one executing globally
|
59
|
+
- two of the same job class each pushed with the same arguments may result in one or two executions, but they won't ever be simultaneous
|
60
|
+
|
61
|
+
In some instances, multiple jobs with the same arguments can be enqueued and sit in the queue simultaneously. Despite this the semantics above will remain in tact: only one job will execute at once. The first one worked will get the lock and the second one worked will skip execution if the first job is still executing. `que-locks` tries to avoid extraneous job pushes by checking to see if the lock for a job is available at enqueue time, and skipping enqueue if so. This preemptive lock check helps keeps queues small in the event that a huge number of identical jobs are pushed at once.
|
62
|
+
|
63
|
+
`que-locks` uses a sorted JSON serialized version of the arguments to compute lock keys, so it's important that arguments that should be considered identical JSON serialize using `Que.serialize_json` to the exact same string.
|
64
|
+
|
65
|
+
`que-locks` adds no overhead to jobs that don't use locking, adds one more SQL query to check an advisory lock (which is only memory access) to enqueuing jobs, and adds two more SQL queries to lock and unlock to job execution.
|
66
|
+
|
67
|
+
## Missing features
|
68
|
+
|
69
|
+
- Configurable preemptive lock checking at enqueue time
|
70
|
+
- Selective argument comparison for lock key computation
|
71
|
+
- maybe a `que-web` integration to expose lock info
|
72
|
+
|
73
|
+
If you wish for any of this stuff, feel free to open a PR, contributions are always welcome!!
|
74
|
+
|
75
|
+
## Non features
|
76
|
+
|
77
|
+
- Locking to a limited concurrency greater than 1. If you want a lock that several different jobs can take out, a good option is to use Que's multiple queue support and run a limited number of workers working a certain queue so the concurrency is limited by the available worker slots.
|
78
|
+
|
79
|
+
## Development
|
80
|
+
|
81
|
+
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.
|
82
|
+
|
83
|
+
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).
|
84
|
+
|
85
|
+
## Contributing
|
86
|
+
|
87
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/hornairs/que-locks. 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.
|
88
|
+
|
89
|
+
## License
|
90
|
+
|
91
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
92
|
+
|
93
|
+
## Code of Conduct
|
94
|
+
|
95
|
+
Everyone interacting in the Que::Locks project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/hornairs/que-locks/blob/master/CODE_OF_CONDUCT.md).
|
96
|
+
|
97
|
+
```
|
98
|
+
|
99
|
+
```
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "que/locks"
|
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/lint
ADDED
data/bin/lintfix
ADDED
data/bin/setup
ADDED
data/lib/que/locks.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require "xxhash"
|
2
|
+
|
3
|
+
module Que
|
4
|
+
SQL[:args_already_enqueued] = %{
|
5
|
+
SELECT COUNT(*) FROM public.que_jobs WHERE job_class = $1 AND args = $2 AND finished_at IS NULL AND expired_at IS NULL;
|
6
|
+
}
|
7
|
+
|
8
|
+
SQL[:try_aquire_execution_lock] = %{
|
9
|
+
SELECT pg_try_advisory_lock(42, $1) AS locked
|
10
|
+
}
|
11
|
+
|
12
|
+
SQL[:release_execution_lock] = %{
|
13
|
+
SELECT pg_advisory_unlock(42, $1)
|
14
|
+
}
|
15
|
+
|
16
|
+
module Locks::ExecutionLock
|
17
|
+
class << self
|
18
|
+
def already_enqueued_job_wanting_lock?(klass, args)
|
19
|
+
args_string = Que.serialize_json(args)
|
20
|
+
values = Que.execute(:args_already_enqueued, [klass.name, args_string]).first
|
21
|
+
values[:count] != 0
|
22
|
+
end
|
23
|
+
|
24
|
+
def can_aquire?(klass, args)
|
25
|
+
can_aquire_key?(lock_key(klass, args))
|
26
|
+
end
|
27
|
+
|
28
|
+
def can_aquire_key?(key)
|
29
|
+
result = false
|
30
|
+
begin
|
31
|
+
result = aquire!(key)
|
32
|
+
ensure
|
33
|
+
if result
|
34
|
+
release!(key)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
result
|
38
|
+
end
|
39
|
+
|
40
|
+
def lock_key(klass, args)
|
41
|
+
XXhash.xxh32(klass.name + ":" + Que.serialize_json(args), 42) / 2
|
42
|
+
end
|
43
|
+
|
44
|
+
def aquire!(key)
|
45
|
+
result = Que.execute(:try_aquire_execution_lock, [key]).first
|
46
|
+
result[:locked]
|
47
|
+
end
|
48
|
+
|
49
|
+
def release!(key)
|
50
|
+
Que.execute(:release_execution_lock, [key])
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Que::Locks
|
2
|
+
module JobExtensions
|
3
|
+
attr_accessor :exclusive_execution_lock
|
4
|
+
|
5
|
+
def lock_available?(*args, queue: nil, priority: nil, run_at: nil, job_class: nil, tags: nil, **arg_opts) # rubocop:disable Lint/UnusedMethodArgument
|
6
|
+
args << arg_opts if arg_opts.any?
|
7
|
+
return true unless self.exclusive_execution_lock
|
8
|
+
return false if Que::Locks::ExecutionLock.already_enqueued_job_wanting_lock?(self, args)
|
9
|
+
return Que::Locks::ExecutionLock.can_aquire?(self, args)
|
10
|
+
end
|
11
|
+
|
12
|
+
def enqueue(*args, queue: nil, priority: nil, run_at: nil, job_class: nil, tags: nil, **arg_opts)
|
13
|
+
if self.exclusive_execution_lock
|
14
|
+
args_list = args.clone
|
15
|
+
args_list << arg_opts if arg_opts.any?
|
16
|
+
|
17
|
+
if Que::Locks::ExecutionLock.already_enqueued_job_wanting_lock?(self, args_list)
|
18
|
+
Que.log(level: :info, event: :skipped_enqueue_due_to_preemptive_lock_check, args: args_list)
|
19
|
+
else
|
20
|
+
super
|
21
|
+
end
|
22
|
+
else
|
23
|
+
super
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
Que::Job.singleton_class.send(:prepend, Que::Locks::JobExtensions)
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require "neatjson"
|
2
|
+
|
3
|
+
# For comparing job args, the JSON serialization of them is used
|
4
|
+
# This patches Que to use a stable json serialization that is independent of the insertion order of keys into hash arguments
|
5
|
+
module Que::Locks::JSONExtensions
|
6
|
+
def serialize_json(object)
|
7
|
+
JSON.neat_generate(object, sort: true)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
Que.singleton_class.send(:prepend, Que::Locks::JSONExtensions)
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Que::Locks
|
2
|
+
LockMiddleware = ->(job, &block) {
|
3
|
+
if job.class.exclusive_execution_lock
|
4
|
+
args = job.que_attrs[:args]
|
5
|
+
lock_key = ExecutionLock.lock_key(job.class, args)
|
6
|
+
if ExecutionLock.aquire!(lock_key)
|
7
|
+
begin
|
8
|
+
block.call
|
9
|
+
ensure
|
10
|
+
ExecutionLock.release!(lock_key)
|
11
|
+
end
|
12
|
+
else
|
13
|
+
Que.log(level: :info, event: :skipped_execution_due_to_lock, args: args, job_class: job.class.name)
|
14
|
+
end
|
15
|
+
else
|
16
|
+
block.call
|
17
|
+
end
|
18
|
+
|
19
|
+
nil
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
Que.job_middleware.push(Que::Locks::LockMiddleware)
|
data/que-locks.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "que/locks/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "que-locks"
|
8
|
+
spec.version = Que::Locks::VERSION
|
9
|
+
spec.authors = ["Harry Brundage"]
|
10
|
+
spec.email = ["harry.brundage@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Job locking for que jobs such that only one can be in the queue or executing at once.}
|
13
|
+
spec.homepage = "https://github.com/superpro-inc/que-locks"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
# Specify which files should be added to the gem when it is released.
|
17
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
18
|
+
spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|.github)/}) }
|
20
|
+
end
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
|
28
|
+
spec.add_dependency "neatjson"
|
29
|
+
spec.add_dependency "que", "~> 1.0.0.beta3"
|
30
|
+
spec.add_dependency "xxhash"
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: que-locks
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Harry Brundage
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-11-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.17'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.17'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: neatjson
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: que
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.0.0.beta3
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 1.0.0.beta3
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: xxhash
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- harry.brundage@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rubocop.yml"
|
92
|
+
- CODE_OF_CONDUCT.md
|
93
|
+
- Gemfile
|
94
|
+
- Gemfile.lock
|
95
|
+
- LICENSE.txt
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- bin/console
|
99
|
+
- bin/lint
|
100
|
+
- bin/lintfix
|
101
|
+
- bin/setup
|
102
|
+
- lib/que/locks.rb
|
103
|
+
- lib/que/locks/execution_lock.rb
|
104
|
+
- lib/que/locks/job_extensions.rb
|
105
|
+
- lib/que/locks/json_extensions.rb
|
106
|
+
- lib/que/locks/lock_middleware.rb
|
107
|
+
- lib/que/locks/version.rb
|
108
|
+
- que-locks.gemspec
|
109
|
+
homepage: https://github.com/superpro-inc/que-locks
|
110
|
+
licenses:
|
111
|
+
- MIT
|
112
|
+
metadata: {}
|
113
|
+
post_install_message:
|
114
|
+
rdoc_options: []
|
115
|
+
require_paths:
|
116
|
+
- lib
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
requirements: []
|
128
|
+
rubygems_version: 3.0.3
|
129
|
+
signing_key:
|
130
|
+
specification_version: 4
|
131
|
+
summary: Job locking for que jobs such that only one can be in the queue or executing
|
132
|
+
at once.
|
133
|
+
test_files: []
|