acts_as_lockable_by 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CODE_OF_CONDUCT.md +14 -0
- data/Gemfile.lock +1 -1
- data/README.md +101 -6
- data/acts_as_lockable_by.gemspec +4 -3
- data/lib/acts_as_lockable_by/lockable.rb +0 -1
- data/lib/acts_as_lockable_by/version.rb +1 -1
- metadata +4 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d975f3f181fa0770661057cc10c95e5aa3b4524aa3f224d100702db3b78fe119
|
4
|
+
data.tar.gz: c0df706de2d54848492c894ca14d04ea8cb696726e9acb89c429f01c68f29046
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d6306434f556cb62351a68579830c96f62f86dc530a04efca47794e021bf126f87db735889666f9fad347515e0d28d4b6a4aead036eb770e0ad687d1658b461d
|
7
|
+
data.tar.gz: 6a1a071b7a180ec017545f981f79f4efcd36448490aab4d1beb9de816b901431b95d50af596cd8982d4a147839497622392da196704e734eee5d0813b8c9e479
|
data/CODE_OF_CONDUCT.md
CHANGED
@@ -1,3 +1,17 @@
|
|
1
|
+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
2
|
+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
3
|
+
**Table of Contents**
|
4
|
+
|
5
|
+
- [Contributor Covenant Code of Conduct](#contributor-covenant-code-of-conduct)
|
6
|
+
- [Our Pledge](#our-pledge)
|
7
|
+
- [Our Standards](#our-standards)
|
8
|
+
- [Our Responsibilities](#our-responsibilities)
|
9
|
+
- [Scope](#scope)
|
10
|
+
- [Enforcement](#enforcement)
|
11
|
+
- [Attribution](#attribution)
|
12
|
+
|
13
|
+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
14
|
+
|
1
15
|
# Contributor Covenant Code of Conduct
|
2
16
|
|
3
17
|
## Our Pledge
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,28 @@
|
|
1
1
|
# ActsAsLockableBy
|
2
2
|
|
3
|
-
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/acts_as_lockable_by.svg)](http://badge.fury.io/rb/acts_as_lockable_by)
|
4
|
+
[![Build Status](https://travis-ci.com/tareksamni/acts_as_lockable_by.svg?branch=master)](https://travis-ci.com/tareksamni/acts_as_lockable_by)
|
4
5
|
|
5
|
-
|
6
|
+
This gem was originally developed and maintained by [ABTION](https://abtion.com/). Its main goal is providing the ability to lock a resource so that no other users/lockers can access it till the lock is released or the ttl expires. It uses `redis` a shared memory space to share locks across different deployments which enables easy horizontal scalability for your ruby/rails project on multiple servers.
|
7
|
+
|
8
|
+
An example usage for this gem is when you need a blog post (resource) to be only edtiable by 1 user concurrently. So the first user to lock the blog post to himself will always have access and be able to edit it. This user will need to renew the lock before the `ttl` expires otherwise the post will be unlocked/released and any other users can lock it to themselves.
|
9
|
+
|
10
|
+
`ActsAsLockableBy` uses `redis` as a shared distributed efficient lock manager with its built-in ability to expire locks when `ttl` expires.
|
11
|
+
|
12
|
+
The `lock`, `unlock` and `renew_lock` methods in this gem are all atomic operations and running as one redis call on the redis server. That even multiple clients calling and of these methods against the same key will never enter into a race condition or thread unsafety.
|
13
|
+
|
14
|
+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
15
|
+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
16
|
+
|
17
|
+
- [Installation](#installation)
|
18
|
+
- [Post Installation](#post-installation)
|
19
|
+
- [Usage](#usage)
|
20
|
+
- [Development](#development)
|
21
|
+
- [Contributing](#contributing)
|
22
|
+
- [License](#license)
|
23
|
+
- [Code of Conduct](#code-of-conduct)
|
24
|
+
|
25
|
+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
6
26
|
|
7
27
|
## Installation
|
8
28
|
|
@@ -20,19 +40,94 @@ Or install it yourself as:
|
|
20
40
|
|
21
41
|
$ gem install acts_as_lockable_by
|
22
42
|
|
43
|
+
### Post Installation
|
44
|
+
|
45
|
+
You need to configure the gem as follows:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
# config/initializers/acts_as_lockable_by.rb
|
49
|
+
ActsAsLockableBy.configure do |config|
|
50
|
+
config.redis = Redis.new(url: ENV['REDIS_URL']) # redis client
|
51
|
+
config.ttl = 30.seconds # global ttl
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
23
55
|
## Usage
|
24
56
|
|
25
|
-
|
57
|
+
Setup
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
class User < ActiveRecord::Base
|
61
|
+
# :id is a unique identifier(attribute/method) for this object
|
62
|
+
# :ttl default to global configured ttl if not provided
|
63
|
+
acts_as_lockable_by :id, ttl: 60.seconds
|
64
|
+
end
|
65
|
+
# or if not using ActiveRecord
|
66
|
+
class Post
|
67
|
+
include ActsAsLockableBy::Lockable
|
68
|
+
acts_as_lockable_by :post_id # default to global configured ttl
|
69
|
+
|
70
|
+
def post_id
|
71
|
+
"SOME UNIQUE IDENTIFIER"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
post = Post.new
|
76
|
+
```
|
77
|
+
|
78
|
+
Lock and unlock a post
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
post.lock('Tarek Elsamni') # true
|
82
|
+
post.lock('Tarek Elsamni') # false - already locked!
|
83
|
+
post.unlock('Someone Else') # false - 'Someone Else' did not lock it!
|
84
|
+
post.unlock('Tarek Elsamni') # true - 'Tarek Elsamni' locked it!
|
85
|
+
post.unlock('Tarek Elsamni') # false - It is already unlocked!
|
86
|
+
post.lock!('Tarek Elsamni') # true
|
87
|
+
post.lock!('Tarek Elsamni') # will raise LockError - already locked!
|
88
|
+
post.unlock!('Tarek Elsamni') # true - 'Tarek Elsamni' locked it!
|
89
|
+
post.unlock!('Tarek Elsamni') # will raise UnLockError - not locked!
|
90
|
+
```
|
91
|
+
|
92
|
+
Check if an object/resource is locked
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
post.locked? # false
|
96
|
+
post.lock('Tarek Elsamni') # true
|
97
|
+
post.locked? # true
|
98
|
+
```
|
99
|
+
|
100
|
+
Renew a lock before it expires
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
post.lock('Tarek Elsamni') # true
|
104
|
+
post.renew_lock('Someone Else') # false - 'Someone Else' did not lock it!
|
105
|
+
post.renew_lock('Tarek Elsamni') # true - 'Tarek Elsamni' locked it!
|
106
|
+
```
|
107
|
+
|
108
|
+
Who locked an object?
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
post.lock('Tarek Elsamni') # true
|
112
|
+
post.locked? # true
|
113
|
+
post.locked_by_id # 'Tarek Elsamni'
|
114
|
+
```
|
115
|
+
|
116
|
+
Is a class lockable?
|
117
|
+
|
118
|
+
```ruby
|
119
|
+
Post.lockable? # true - an alias to Post.is_lockable?
|
120
|
+
```
|
26
121
|
|
27
122
|
## Development
|
28
123
|
|
29
124
|
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.
|
30
125
|
|
31
|
-
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
|
126
|
+
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 open a PR with your changes against the main gem repo. If your code is passing current tests and highly covered by new tests then one of the maintainers will review it and merge. Auto releasing to a new gem version to `rubygems` is automated with [travis](http://travis-ci.org).
|
32
127
|
|
33
128
|
## Contributing
|
34
129
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
130
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/tareksamni/acts_as_lockable_by. 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.
|
36
131
|
|
37
132
|
## License
|
38
133
|
|
@@ -40,4 +135,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
40
135
|
|
41
136
|
## Code of Conduct
|
42
137
|
|
43
|
-
Everyone interacting in the ActsAsLockable project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/
|
138
|
+
Everyone interacting in the ActsAsLockable project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/tareksamni/acts_as_lockable_by/blob/master/CODE_OF_CONDUCT.md).
|
data/acts_as_lockable_by.gemspec
CHANGED
@@ -10,9 +10,10 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.authors = ['Tarek N. Elsamni']
|
11
11
|
spec.email = ['tarek.samni@gmail.com']
|
12
12
|
|
13
|
-
spec.summary = '
|
14
|
-
|
15
|
-
concurrent
|
13
|
+
spec.summary = 'Atomically lock resources from concurrent access'
|
14
|
+
# rubocop:disable Metrics/LineLength
|
15
|
+
spec.description = 'A ruby gem to atomically lock resources to prevent concurrent/multiple lockers from accessing or editing the resource'
|
16
|
+
# rubocop:enable Metrics/LineLength
|
16
17
|
spec.homepage = 'https://github.com/tareksamni/acts_as_lockable_by'
|
17
18
|
spec.license = 'MIT'
|
18
19
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_lockable_by
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tarek N. Elsamni
|
@@ -136,9 +136,8 @@ dependencies:
|
|
136
136
|
- - "~>"
|
137
137
|
- !ruby/object:Gem::Version
|
138
138
|
version: '0.28'
|
139
|
-
description:
|
140
|
-
|
141
|
-
concurrent (multiple actors) access/editing of the resource
|
139
|
+
description: A ruby gem to atomically lock resources to prevent concurrent/multiple
|
140
|
+
lockers from accessing or editing the resource
|
142
141
|
email:
|
143
142
|
- tarek.samni@gmail.com
|
144
143
|
executables:
|
@@ -192,5 +191,5 @@ rubyforge_project:
|
|
192
191
|
rubygems_version: 2.7.8
|
193
192
|
signing_key:
|
194
193
|
specification_version: 4
|
195
|
-
summary:
|
194
|
+
summary: Atomically lock resources from concurrent access
|
196
195
|
test_files: []
|