sidekiq_lockable_job 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.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +51 -0
- data/LICENSE.txt +21 -0
- data/README.md +231 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/rspec +2 -0
- data/bin/setup +8 -0
- data/lib/sidekiq/lockable_job/lockable_job.rb +69 -0
- data/lib/sidekiq/lockable_job/middleware/client/set_locks.rb +31 -0
- data/lib/sidekiq/lockable_job/middleware/middleware.rb +5 -0
- data/lib/sidekiq/lockable_job/middleware/server/raise_if_locked.rb +26 -0
- data/lib/sidekiq/lockable_job/middleware/server/set_locks.rb +28 -0
- data/lib/sidekiq/lockable_job/middleware/server/unset_locks.rb +28 -0
- data/lib/sidekiq_lockable_job/version.rb +3 -0
- data/lib/sidekiq_lockable_job.rb +7 -0
- data/sidekiq_lockable_job.gemspec +41 -0
- metadata +157 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 2c4078ebd3f7c4c03e6167d73f0ec1c4510403045ad09f09bef28386321932ef
|
|
4
|
+
data.tar.gz: 87b5bb9b9f8a2b8a61b5b69b63487fa2a44d992e43aa2090db017c69bfdc0229
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 72966453281278f758578bb8e904e9a61c0fab7e05e7cceaa47184a61974dc4277d4620185d592410f042a2b98f5650d2595b310aeb14554e5cd90890c2ece6b
|
|
7
|
+
data.tar.gz: 7b194fced1ac5ec4e12f148c67b70202680bc88bfe0a03dfd720dcddc6aa2ba2c725791c417f9341399b0211192d375b7aed7a0e54a65f53ddffaee9705772ed
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
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 hugues@xdev.fr. 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
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
sidekiq_lockable_job (0.1.0)
|
|
5
|
+
redis (= 4.0.1)
|
|
6
|
+
sidekiq (= 5.0.5)
|
|
7
|
+
|
|
8
|
+
GEM
|
|
9
|
+
remote: https://rubygems.org/
|
|
10
|
+
specs:
|
|
11
|
+
concurrent-ruby (1.1.5)
|
|
12
|
+
connection_pool (2.2.2)
|
|
13
|
+
diff-lcs (1.3)
|
|
14
|
+
fakeredis (0.7.0)
|
|
15
|
+
redis (>= 3.2, < 5.0)
|
|
16
|
+
rack (2.0.8)
|
|
17
|
+
rack-protection (2.0.5)
|
|
18
|
+
rack
|
|
19
|
+
rake (12.3.3)
|
|
20
|
+
redis (4.0.1)
|
|
21
|
+
rspec (3.9.0)
|
|
22
|
+
rspec-core (~> 3.9.0)
|
|
23
|
+
rspec-expectations (~> 3.9.0)
|
|
24
|
+
rspec-mocks (~> 3.9.0)
|
|
25
|
+
rspec-core (3.9.1)
|
|
26
|
+
rspec-support (~> 3.9.1)
|
|
27
|
+
rspec-expectations (3.9.1)
|
|
28
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
29
|
+
rspec-support (~> 3.9.0)
|
|
30
|
+
rspec-mocks (3.9.1)
|
|
31
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
32
|
+
rspec-support (~> 3.9.0)
|
|
33
|
+
rspec-support (3.9.2)
|
|
34
|
+
sidekiq (5.0.5)
|
|
35
|
+
concurrent-ruby (~> 1.0)
|
|
36
|
+
connection_pool (~> 2.2, >= 2.2.0)
|
|
37
|
+
rack-protection (>= 1.5.0)
|
|
38
|
+
redis (>= 3.3.4, < 5)
|
|
39
|
+
|
|
40
|
+
PLATFORMS
|
|
41
|
+
ruby
|
|
42
|
+
|
|
43
|
+
DEPENDENCIES
|
|
44
|
+
bundler (~> 2.0)
|
|
45
|
+
fakeredis (= 0.7.0)
|
|
46
|
+
rake (~> 12.3.3)
|
|
47
|
+
rspec (~> 3.0)
|
|
48
|
+
sidekiq_lockable_job!
|
|
49
|
+
|
|
50
|
+
BUNDLED WITH
|
|
51
|
+
2.0.2
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Hugues Bernet-Rollande
|
|
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,231 @@
|
|
|
1
|
+
# SidekiqLockableJob
|
|
2
|
+
|
|
3
|
+
Prevent a job to run until another one complete.
|
|
4
|
+
|
|
5
|
+
[Sidekiq](https://github.com/mperham/sidekiq) includes a jobs dependencies mechanism to prevent a job from running before another one when enqueued.
|
|
6
|
+
|
|
7
|
+
But sometime your jobs will be enqueued independently, then for you do not know the job id on which you depend on (you could parse Sidekiq queue, but...)
|
|
8
|
+
|
|
9
|
+
`SidekiqLockableJob` allows you to set some locks ( based on job params ) when a job is enqueued or processed (store in redis), to prevent any other jobs to run if locked ( based on job params ) and will unlock any previously set locks ( based on job params ) when a job is **succesfully** completed.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
Add this line to your application's Gemfile:
|
|
14
|
+
|
|
15
|
+
```ruby
|
|
16
|
+
gem 'sidekiq_lockable_job'
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
And then execute:
|
|
20
|
+
|
|
21
|
+
$ bundle
|
|
22
|
+
|
|
23
|
+
Or install it yourself as:
|
|
24
|
+
|
|
25
|
+
$ gem install sidekiq_lockable_job
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
The gem is compose of four parts:
|
|
30
|
+
|
|
31
|
+
- Setting locks when job is **enqueued**
|
|
32
|
+
- Setting locks when job is **processed**
|
|
33
|
+
- Raising `LockableJob::LockedJobError` when job **start** to processed but is locked
|
|
34
|
+
- Unsetting locks when job is **succesfully** processed
|
|
35
|
+
|
|
36
|
+
### Setting locks when job is **enqueued**
|
|
37
|
+
|
|
38
|
+
> happens in the sidekiq client middleware (on you rails app)
|
|
39
|
+
> including `Sidekiq::LockableJob` auto set the middleware chain
|
|
40
|
+
|
|
41
|
+
```ruby
|
|
42
|
+
class Worker
|
|
43
|
+
include Sidekiq::Worker
|
|
44
|
+
include Sidekiq::LockableJob
|
|
45
|
+
|
|
46
|
+
def self.lockable_job_client_lock_keys(args)
|
|
47
|
+
# jobs args are always in an array like **args
|
|
48
|
+
lock_key = args.first['a_key']
|
|
49
|
+
"some_context_prefix:#{lock_key}"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def perform
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
When your job is **ENQUEUED** (`Worker.perform_async`), sidekiq LockableJob client middleware, will call `lockable_job_client_lock_keys` (before enqueuing the job) with the jobs arguments and set a lock for any returned keys
|
|
58
|
+
|
|
59
|
+
### Setting locks when job is processed
|
|
60
|
+
|
|
61
|
+
> happens in the sidekiq server middleware (on you rails worker)
|
|
62
|
+
> including `Sidekiq::LockableJob` auto set the middleware chain
|
|
63
|
+
|
|
64
|
+
```ruby
|
|
65
|
+
class Worker
|
|
66
|
+
include Sidekiq::Worker
|
|
67
|
+
include Sidekiq::LockableJob
|
|
68
|
+
|
|
69
|
+
def self.lockable_job_server_lock_keys(args)
|
|
70
|
+
# jobs args are always in an array like **args
|
|
71
|
+
lock_key = args.first['a_key']
|
|
72
|
+
"some_context_prefix:#{lock_key}"
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def perform
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
When your job is **PROCESSED**, sidekiq LockableJob server middleware, will call `lockable_job_client_lock_keys` (before running the job) with the jobs arguments and set a lock for any returned keys
|
|
81
|
+
|
|
82
|
+
### Raising `LockableJob::LockedJobError` when job start to processed but is locked
|
|
83
|
+
|
|
84
|
+
> happens in the sidekiq server middleware (on you rails worker)
|
|
85
|
+
> including `Sidekiq::LockableJob` auto set the middleware chain
|
|
86
|
+
|
|
87
|
+
```ruby
|
|
88
|
+
class Worker
|
|
89
|
+
include Sidekiq::Worker
|
|
90
|
+
include Sidekiq::LockableJob
|
|
91
|
+
|
|
92
|
+
def self.lockable_job_locked_by_keys(args)
|
|
93
|
+
# jobs args are always in an array like **args
|
|
94
|
+
lock_key = args.first['a_key']
|
|
95
|
+
"some_context_prefix:#{lock_key}"
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def perform
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
When your job is **about** to be **processed**, sidekiq LockableJob server middleware, will call `lockable_job_locked_by_keys` (before processing the job) with the jobs arguments and raise `Sidekiq::LockableJob::LockedJobError` if any of the returned keys is locked
|
|
104
|
+
|
|
105
|
+
### Unsetting locks when job is **succesfully** processed
|
|
106
|
+
|
|
107
|
+
> happens in the sidekiq server middleware (on you rails worker)
|
|
108
|
+
> including `Sidekiq::LockableJob` auto set the middleware chain
|
|
109
|
+
|
|
110
|
+
```ruby
|
|
111
|
+
class Worker
|
|
112
|
+
include Sidekiq::Worker
|
|
113
|
+
include Sidekiq::LockableJob
|
|
114
|
+
|
|
115
|
+
def self.lockable_job_unlock_keys(args)
|
|
116
|
+
# jobs args are always in an array like **args
|
|
117
|
+
lock_key = args.first['a_key']
|
|
118
|
+
"some_context_prefix:#{lock_key}"
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def perform
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
When your job is **successfully** to be **performed**, sidekiq LockableJob server middleware, will call `lockable_job_unlock_keys` (after processing the job) with the jobs arguments and unset lock for any returned keys
|
|
127
|
+
|
|
128
|
+
## Specs
|
|
129
|
+
|
|
130
|
+
```
|
|
131
|
+
Sidekiq::LockableJob
|
|
132
|
+
.lock
|
|
133
|
+
lock
|
|
134
|
+
.unlock
|
|
135
|
+
unlock
|
|
136
|
+
.locked?
|
|
137
|
+
true if locked
|
|
138
|
+
false if NOT locked
|
|
139
|
+
.raise_if_locked_by
|
|
140
|
+
raise if locked by any key
|
|
141
|
+
raise if locked by single key
|
|
142
|
+
DOT NOT raise if not locked by
|
|
143
|
+
.included
|
|
144
|
+
include middlewares
|
|
145
|
+
|
|
146
|
+
Sidekiq::LockableJob::Middleware::Client::SetLocks
|
|
147
|
+
LockableJob
|
|
148
|
+
behaves like it yield
|
|
149
|
+
is expected to eq true
|
|
150
|
+
behaves like set locks
|
|
151
|
+
set locks
|
|
152
|
+
with single lock key
|
|
153
|
+
behaves like it yield
|
|
154
|
+
is expected to eq true
|
|
155
|
+
behaves like set locks
|
|
156
|
+
set locks
|
|
157
|
+
|
|
158
|
+
Sidekiq::LockableJob::Middleware::Server::RaiseIfLocked
|
|
159
|
+
with no lock
|
|
160
|
+
behaves like perform the job
|
|
161
|
+
example at ./spec/sidekiq/lockable_job/middleware/server/shared.rb:13
|
|
162
|
+
behaves like it yield
|
|
163
|
+
is expected to eq true
|
|
164
|
+
with lock set
|
|
165
|
+
behaves like raise an error
|
|
166
|
+
is expected to raise Sidekiq::LockableJob::LockedJobError
|
|
167
|
+
with another lock key
|
|
168
|
+
behaves like raise an error
|
|
169
|
+
is expected to raise Sidekiq::LockableJob::LockedJobError
|
|
170
|
+
with a non lock key
|
|
171
|
+
behaves like perform the job
|
|
172
|
+
example at ./spec/sidekiq/lockable_job/middleware/server/shared.rb:13
|
|
173
|
+
with single lock key
|
|
174
|
+
behaves like raise an error
|
|
175
|
+
is expected to raise Sidekiq::LockableJob::LockedJobError
|
|
176
|
+
|
|
177
|
+
Sidekiq::LockableJob::Middleware::Server::SetLocks
|
|
178
|
+
LockableJob
|
|
179
|
+
set locks
|
|
180
|
+
behaves like it yield
|
|
181
|
+
is expected to eq true
|
|
182
|
+
behaves like perform the job
|
|
183
|
+
example at ./spec/sidekiq/lockable_job/middleware/server/shared.rb:13
|
|
184
|
+
with single lock key
|
|
185
|
+
set lock
|
|
186
|
+
behaves like it yield
|
|
187
|
+
is expected to eq true
|
|
188
|
+
behaves like perform the job
|
|
189
|
+
example at ./spec/sidekiq/lockable_job/middleware/server/shared.rb:13
|
|
190
|
+
|
|
191
|
+
Sidekiq::LockableJob::Middleware::Server::UnsetLocks
|
|
192
|
+
with previous locks
|
|
193
|
+
behaves like lock keys
|
|
194
|
+
remove all locks
|
|
195
|
+
behaves like it yield
|
|
196
|
+
is expected to eq true
|
|
197
|
+
behaves like perform the job
|
|
198
|
+
example at ./spec/sidekiq/lockable_job/middleware/server/shared.rb:13
|
|
199
|
+
for another job
|
|
200
|
+
DOES NOT remove the lock
|
|
201
|
+
with single lock key
|
|
202
|
+
behaves like lock keys
|
|
203
|
+
remove all locks
|
|
204
|
+
behaves like it yield
|
|
205
|
+
is expected to eq true
|
|
206
|
+
behaves like perform the job
|
|
207
|
+
example at ./spec/sidekiq/lockable_job/middleware/server/shared.rb:13
|
|
208
|
+
for another job
|
|
209
|
+
DOES NOT remove the lock
|
|
210
|
+
|
|
211
|
+
SidekiqLockableJob
|
|
212
|
+
has a version number
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
## Development
|
|
216
|
+
|
|
217
|
+
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.
|
|
218
|
+
|
|
219
|
+
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).
|
|
220
|
+
|
|
221
|
+
## Contributing
|
|
222
|
+
|
|
223
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/huguesbr/sidekiq_lockable_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.
|
|
224
|
+
|
|
225
|
+
## License
|
|
226
|
+
|
|
227
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
228
|
+
|
|
229
|
+
## Code of Conduct
|
|
230
|
+
|
|
231
|
+
Everyone interacting in the sidekiq_lockable_job project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/huguesbr/sidekiq_lockable_job/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "sidekiq_lockable_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__)
|
data/bin/rspec
ADDED
data/bin/setup
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
require 'sidekiq'
|
|
2
|
+
require 'redis'
|
|
3
|
+
require_relative 'middleware/middleware.rb'
|
|
4
|
+
|
|
5
|
+
module Sidekiq
|
|
6
|
+
module LockableJob
|
|
7
|
+
class Error < StandardError; end
|
|
8
|
+
class LockedJobError < StandardError; end
|
|
9
|
+
|
|
10
|
+
LOCKABLE_JOB_REDIS_PREFIX_KEY = 'sidekiq_locks'
|
|
11
|
+
|
|
12
|
+
def self.lock(key)
|
|
13
|
+
redis.set(redis_key(key), Time.now)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def self.unlock(key)
|
|
17
|
+
redis.del(redis_key(key))
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def self.locked?(key)
|
|
21
|
+
redis.exists(redis_key(key))
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def self.raise_if_locked_by(keys)
|
|
25
|
+
keys = [keys] unless keys.nil? || keys.is_a?(Array)
|
|
26
|
+
keys&.each do |key|
|
|
27
|
+
# perform in instead of retry?
|
|
28
|
+
# perform_in(2.minutes, *args)
|
|
29
|
+
raise LockedJobError.new("Locked by #{key}") if locked?(key)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def self.redis_key(key)
|
|
34
|
+
"#{LOCKABLE_JOB_REDIS_PREFIX_KEY}:#{key}"
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def self.redis
|
|
38
|
+
$redis = Redis.new
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def self.included(base)
|
|
42
|
+
unless base.ancestors.include? Sidekiq::Worker
|
|
43
|
+
raise ArgumentError, "Sidekiq::LockableJob can only be included in a Sidekiq::Worker"
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# base.extend(ClassMethods)
|
|
47
|
+
|
|
48
|
+
# Automatically add sidekiq middleware when we're first included
|
|
49
|
+
#
|
|
50
|
+
# This might only occur when the worker class is first loaded in a
|
|
51
|
+
# development rails environment, but that happens before the middleware
|
|
52
|
+
# chain is invoked so we're all good.
|
|
53
|
+
#
|
|
54
|
+
Sidekiq.configure_server do |config|
|
|
55
|
+
unless config.server_middleware.exists? Sidekiq::LockableJob::Middleware::Server::RaiseIfLocked
|
|
56
|
+
config.server_middleware.add Sidekiq::LockableJob::Middleware::Server::RaiseIfLocked
|
|
57
|
+
end
|
|
58
|
+
unless config.server_middleware.exists? Sidekiq::LockableJob::Middleware::Server::UnsetLocks
|
|
59
|
+
config.server_middleware.add Sidekiq::LockableJob::Middleware::Server::UnsetLocks
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
Sidekiq.configure_client do |config|
|
|
63
|
+
unless config.client_middleware.exists? Sidekiq::LockableJob::Middleware::Client::SetLocks
|
|
64
|
+
config.client_middleware.add Sidekiq::LockableJob::Middleware::Client::SetLocks
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# https://github.com/mperham/sidekiq/wiki/Middleware
|
|
2
|
+
require 'sidekiq'
|
|
3
|
+
|
|
4
|
+
module Sidekiq::LockableJob
|
|
5
|
+
module Middleware
|
|
6
|
+
module Client
|
|
7
|
+
class SetLocks
|
|
8
|
+
# @param [String, Class] worker_class the string or class of the worker class being enqueued
|
|
9
|
+
# @param [Hash] job the full job payload
|
|
10
|
+
# * @see https://github.com/mperham/sidekiq/wiki/Job-Format
|
|
11
|
+
# @param [String] queue the name of the queue the job was pulled from
|
|
12
|
+
# @param [ConnectionPool] redis_pool the redis pool
|
|
13
|
+
# @return [Hash, FalseClass, nil] if false or nil is returned,
|
|
14
|
+
# the job is not to be enqueued into redis, otherwise the block's
|
|
15
|
+
# return value is returned
|
|
16
|
+
# @yield the next middleware in the chain or the enqueuing of the job
|
|
17
|
+
def call(worker_class, job, queue, redis_pool)
|
|
18
|
+
worker_klass = worker_class.is_a?(String) ? worker_class.constantize : worker_class
|
|
19
|
+
if worker_klass.respond_to?(:lockable_job_client_lock_keys)
|
|
20
|
+
keys = worker_klass.send(:lockable_job_client_lock_keys, job['args'])
|
|
21
|
+
keys = [keys] unless keys.nil? || keys.is_a?(Array)
|
|
22
|
+
keys&.compact&.each do |key|
|
|
23
|
+
Sidekiq::LockableJob.lock(key)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
yield
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# https://github.com/mperham/sidekiq/wiki/Middleware
|
|
2
|
+
require 'sidekiq'
|
|
3
|
+
|
|
4
|
+
module Sidekiq::LockableJob
|
|
5
|
+
module Middleware
|
|
6
|
+
module Server
|
|
7
|
+
class RaiseIfLocked
|
|
8
|
+
# @param [Object] worker the worker instance
|
|
9
|
+
# @param [Hash] job the full job payload
|
|
10
|
+
# * @see https://github.com/mperham/sidekiq/wiki/Job-Format
|
|
11
|
+
# @param [String] queue the name of the queue the job was pulled from
|
|
12
|
+
# @yield the next middleware in the chain or worker `perform` method
|
|
13
|
+
# @return [Void]
|
|
14
|
+
def call(worker, job, queue)
|
|
15
|
+
worker_klass = worker.class
|
|
16
|
+
if worker_klass.respond_to?(:lockable_job_locked_by_keys)
|
|
17
|
+
keys = worker_klass.send(:lockable_job_locked_by_keys, job['args'])
|
|
18
|
+
keys = [keys] unless keys.nil? || keys.is_a?(Array)
|
|
19
|
+
Sidekiq::LockableJob.raise_if_locked_by(keys)
|
|
20
|
+
end
|
|
21
|
+
yield
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# https://github.com/mperham/sidekiq/wiki/Middleware
|
|
2
|
+
require 'sidekiq'
|
|
3
|
+
|
|
4
|
+
module Sidekiq::LockableJob
|
|
5
|
+
module Middleware
|
|
6
|
+
module Server
|
|
7
|
+
class SetLocks
|
|
8
|
+
# @param [Object] worker the worker instance
|
|
9
|
+
# @param [Hash] job the full job payload
|
|
10
|
+
# * @see https://github.com/mperham/sidekiq/wiki/Job-Format
|
|
11
|
+
# @param [String] queue the name of the queue the job was pulled from
|
|
12
|
+
# @yield the next middleware in the chain or worker `perform` method
|
|
13
|
+
# @return [Void]
|
|
14
|
+
def call(worker, job, queue)
|
|
15
|
+
worker_klass = worker.class
|
|
16
|
+
if worker_klass.respond_to?(:lockable_job_server_lock_keys)
|
|
17
|
+
keys = worker_klass.send(:lockable_job_server_lock_keys, job['args'])
|
|
18
|
+
keys = [keys] unless keys.nil? || keys.is_a?(Array)
|
|
19
|
+
keys&.compact&.each do |key|
|
|
20
|
+
Sidekiq::LockableJob.lock(key)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
yield
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# https://github.com/mperham/sidekiq/wiki/Middleware
|
|
2
|
+
require 'sidekiq'
|
|
3
|
+
|
|
4
|
+
module Sidekiq::LockableJob
|
|
5
|
+
module Middleware
|
|
6
|
+
module Server
|
|
7
|
+
class UnsetLocks
|
|
8
|
+
# @param [Object] worker the worker instance
|
|
9
|
+
# @param [Hash] job the full job payload
|
|
10
|
+
# * @see https://github.com/mperham/sidekiq/wiki/Job-Format
|
|
11
|
+
# @param [String] queue the name of the queue the job was pulled from
|
|
12
|
+
# @yield the next middleware in the chain or worker `perform` method
|
|
13
|
+
# @return [Void]
|
|
14
|
+
def call(worker, job, queue)
|
|
15
|
+
yield
|
|
16
|
+
worker_klass = worker.class
|
|
17
|
+
if worker_klass.respond_to?(:lockable_job_unlock_keys)
|
|
18
|
+
keys = worker_klass.send(:lockable_job_unlock_keys, job['args'])
|
|
19
|
+
keys = [keys] unless keys.nil? || keys.is_a?(Array)
|
|
20
|
+
keys&.compact&.each do |key|
|
|
21
|
+
Sidekiq::LockableJob.unlock(key)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
lib = File.expand_path("lib", __dir__)
|
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
3
|
+
require "sidekiq_lockable_job/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = "sidekiq_lockable_job"
|
|
7
|
+
spec.version = SidekiqLockableJob::VERSION
|
|
8
|
+
spec.authors = ["Hugues Bernet-Rollande"]
|
|
9
|
+
spec.email = ["hugues@xdev.fr"]
|
|
10
|
+
|
|
11
|
+
spec.summary = 'Prevent a job to run until another one complete'
|
|
12
|
+
spec.description = <<~EOF
|
|
13
|
+
Sidekiq includes a jobs dependencies mechanism to prevent a job from running before another one when enqueued.
|
|
14
|
+
|
|
15
|
+
But sometime your jobs will be enqueued independently, then for you do not know the job id on which you depend on (you could parse Sidekiq queue, but...)
|
|
16
|
+
|
|
17
|
+
`SidekiqLockableJob` allows you to set some locks ( based on job params ) when a job is enqueued or processed (store in redis), to prevent any other jobs to run if locked ( based on job params ) and will unlock any previously set locks ( based on job params ) when a job is **succesfully** completed.
|
|
18
|
+
EOF
|
|
19
|
+
spec.homepage = 'https://github.com/huguesbr/sidekiq_lockable_job'
|
|
20
|
+
spec.license = "MIT"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
|
24
|
+
spec.metadata["source_code_uri"] = 'https://github.com/huguesbr/sidekiq_lockable_job'
|
|
25
|
+
spec.metadata["changelog_uri"] = 'https://github.com/huguesbr/sidekiq_lockable_job/README.md'
|
|
26
|
+
|
|
27
|
+
# Specify which files should be added to the gem when it is released.
|
|
28
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
29
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
|
30
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
31
|
+
end
|
|
32
|
+
spec.require_paths = ["lib"]
|
|
33
|
+
|
|
34
|
+
spec.add_dependency 'sidekiq', '5.0.5'
|
|
35
|
+
spec.add_dependency 'redis', '4.0.1'
|
|
36
|
+
|
|
37
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
|
38
|
+
spec.add_development_dependency "rake", "~> 12.3.3"
|
|
39
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
|
40
|
+
spec.add_development_dependency "fakeredis", '0.7.0'
|
|
41
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: sidekiq_lockable_job
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Hugues Bernet-Rollande
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2020-04-20 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: sidekiq
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - '='
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: 5.0.5
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - '='
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: 5.0.5
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: redis
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - '='
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: 4.0.1
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - '='
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: 4.0.1
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: bundler
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '2.0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '2.0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: rake
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: 12.3.3
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: 12.3.3
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: rspec
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - "~>"
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '3.0'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - "~>"
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '3.0'
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: fakeredis
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - '='
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: 0.7.0
|
|
90
|
+
type: :development
|
|
91
|
+
prerelease: false
|
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - '='
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: 0.7.0
|
|
97
|
+
description: |
|
|
98
|
+
Sidekiq includes a jobs dependencies mechanism to prevent a job from running before another one when enqueued.
|
|
99
|
+
|
|
100
|
+
But sometime your jobs will be enqueued independently, then for you do not know the job id on which you depend on (you could parse Sidekiq queue, but...)
|
|
101
|
+
|
|
102
|
+
`SidekiqLockableJob` allows you to set some locks ( based on job params ) when a job is enqueued or processed (store in redis), to prevent any other jobs to run if locked ( based on job params ) and will unlock any previously set locks ( based on job params ) when a job is **succesfully** completed.
|
|
103
|
+
email:
|
|
104
|
+
- hugues@xdev.fr
|
|
105
|
+
executables: []
|
|
106
|
+
extensions: []
|
|
107
|
+
extra_rdoc_files: []
|
|
108
|
+
files:
|
|
109
|
+
- ".gitignore"
|
|
110
|
+
- ".rspec"
|
|
111
|
+
- ".travis.yml"
|
|
112
|
+
- CODE_OF_CONDUCT.md
|
|
113
|
+
- Gemfile
|
|
114
|
+
- Gemfile.lock
|
|
115
|
+
- LICENSE.txt
|
|
116
|
+
- README.md
|
|
117
|
+
- Rakefile
|
|
118
|
+
- bin/console
|
|
119
|
+
- bin/rspec
|
|
120
|
+
- bin/setup
|
|
121
|
+
- lib/sidekiq/lockable_job/lockable_job.rb
|
|
122
|
+
- lib/sidekiq/lockable_job/middleware/client/set_locks.rb
|
|
123
|
+
- lib/sidekiq/lockable_job/middleware/middleware.rb
|
|
124
|
+
- lib/sidekiq/lockable_job/middleware/server/raise_if_locked.rb
|
|
125
|
+
- lib/sidekiq/lockable_job/middleware/server/set_locks.rb
|
|
126
|
+
- lib/sidekiq/lockable_job/middleware/server/unset_locks.rb
|
|
127
|
+
- lib/sidekiq_lockable_job.rb
|
|
128
|
+
- lib/sidekiq_lockable_job/version.rb
|
|
129
|
+
- sidekiq_lockable_job.gemspec
|
|
130
|
+
homepage: https://github.com/huguesbr/sidekiq_lockable_job
|
|
131
|
+
licenses:
|
|
132
|
+
- MIT
|
|
133
|
+
metadata:
|
|
134
|
+
homepage_uri: https://github.com/huguesbr/sidekiq_lockable_job
|
|
135
|
+
source_code_uri: https://github.com/huguesbr/sidekiq_lockable_job
|
|
136
|
+
changelog_uri: https://github.com/huguesbr/sidekiq_lockable_job/README.md
|
|
137
|
+
post_install_message:
|
|
138
|
+
rdoc_options: []
|
|
139
|
+
require_paths:
|
|
140
|
+
- lib
|
|
141
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
142
|
+
requirements:
|
|
143
|
+
- - ">="
|
|
144
|
+
- !ruby/object:Gem::Version
|
|
145
|
+
version: '0'
|
|
146
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
147
|
+
requirements:
|
|
148
|
+
- - ">="
|
|
149
|
+
- !ruby/object:Gem::Version
|
|
150
|
+
version: '0'
|
|
151
|
+
requirements: []
|
|
152
|
+
rubyforge_project:
|
|
153
|
+
rubygems_version: 2.7.7
|
|
154
|
+
signing_key:
|
|
155
|
+
specification_version: 4
|
|
156
|
+
summary: Prevent a job to run until another one complete
|
|
157
|
+
test_files: []
|