Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/qonto/activejob-lockable
Gem to make jobs lockable
https://github.com/qonto/activejob-lockable
activejob lockable rails ruby
Last synced: 2 months ago
JSON representation
Gem to make jobs lockable
- Host: GitHub
- URL: https://github.com/qonto/activejob-lockable
- Owner: qonto
- License: mit
- Created: 2017-11-07T14:47:57.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-06-06T08:55:51.000Z (over 1 year ago)
- Last Synced: 2024-10-31T13:19:22.780Z (3 months ago)
- Topics: activejob, lockable, rails, ruby
- Language: Ruby
- Homepage:
- Size: 22.5 KB
- Stars: 9
- Watchers: 12
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
![Gem Version](https://badge.fury.io/rb/activejob-lockable.svg) ![CI Status](https://github.com/qonto/activejob-lockable/actions/workflows/tests.yml/badge.svg)
# ActiveJob::Lockable
Gem to make to make jobs lockable. Useful when a job is called N times, but only a single execution is needed.
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'activejob-lockable'
```And then execute:
$ bundle
Or install it yourself as:
$ gem install activejob-lockable
## Configuration
Create an initializer with redis connection:
```ruby
ActiveJob::Lockable.redis = Redis.current # if you have a redis instance# or
ActiveJob::Lockable.redis = 'redis://localhost:6379/0'
# or
ActiveJob::Lockable.redis = {
host: '10.0.1.1',
port: 6380,
db: 15
}
```## Usage
```ruby
# job
# nothing to change!
```
```ruby
# code
MyJob.set(lock: 10.seconds).perform_later(id)
```Now, after the first enqueue (perform_later), a lock will be created and the following enqueues within 10 seconds will be rejected.
### Lock key
A lock key by default:
`job_name_in_downcase:md5(arguments)`
To override the key you can override method `lock_key`:
```ruby
# job
def lock_key
'my-custom-key'
end
```### Lock period
You can set a fixed `lock_period`, in that case `.set(lock: N)` will be ignored and the job will always be lockable:
```ruby
# job
def lock_period
1.day
end
```### On lock action
When a job is locked and another one is enqueued, you can set up a custom callback that will be called. This is useful if you want to raise exception or be notified:
```ruby
# job
class MyJob < ApplicationJob
on_locked :raise_if_lockeddef raise_if_locked
raise 'Job is locked'
end
end
```## Dependencies
* [ActiveSupport](https://github.com/rails/rails/tree/master/activesupport)
* [ActibeJob](https://github.com/rails/rails/tree/master/activejob)
* [Redis](https://redis.io/)## Contributing
Bug reports and pull requests are welcome on GitHub at [qonto/activejob-lockable](https://github.com/qonto/activejob-lockable). 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.
## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
## Releasing
To publish a new version to rubygems, update the version in `lib/activejob/lockable/version.rb`, and merge.