hiatus 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +65 -0
- data/Rakefile +6 -0
- data/hiatus.gemspec +27 -0
- data/lib/hiatus.rb +43 -0
- data/lib/hiatus/version.rb +3 -0
- data/lib/pausable.rb +19 -0
- data/spec/lib/hiatus_spec.rb +120 -0
- data/spec/lib/pausable_spec.rb +56 -0
- data/spec/spec_helper.rb +10 -0
- metadata +131 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fbf8e4ffa9ac68879d3d5b73fdc1a680f1b39ac8
|
4
|
+
data.tar.gz: 25e20bb996b32886f4f6a20fbeea5d4aea6736fe
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 448a544e93e12bc1e7447de8284337917d952946ad3fb7edabcb55e087f360d798c52e1dfa045dbb2e94c9fa0bed862e923609322995cd13df0d3d636a0b40fd
|
7
|
+
data.tar.gz: 540022f607baa584ed4da291dd2ffc8fbeb909049add2edec8030553c58de410bc2da5ed3cd5e400e3ffd0d8d0a6b6a852dc7e1de2a04511e490a48e485eb262
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Alex Gessner
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# Hiatus
|
2
|
+
|
3
|
+
This gem will allow you to use Redis to pause and check for paused status of things.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'hiatus'
|
10
|
+
|
11
|
+
And then install with Bundler:
|
12
|
+
|
13
|
+
bundle install
|
14
|
+
|
15
|
+
Or install it globally:
|
16
|
+
|
17
|
+
gem install hiatus
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
### Initialization / Dependency
|
22
|
+
|
23
|
+
You must have the thread-safe `Redis.current` set up
|
24
|
+
|
25
|
+
### Extending an existing model
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
class YeOldeBlob
|
29
|
+
extend Hiatus::Pausable
|
30
|
+
|
31
|
+
def self.process_everything
|
32
|
+
return if paused?
|
33
|
+
...
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
rake stop_blobs do
|
38
|
+
YeOldeBlob.pause
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
### Pausing processes by name
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
rake read_only_maintenance_mode do
|
46
|
+
Hiatus.pause([:blobs, :jobs, :screaming_sobs], 2000)
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
class BlobProcessor
|
52
|
+
def process
|
53
|
+
return if Hiatus.paused?(:blobs)
|
54
|
+
...
|
55
|
+
end
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
## Contributing
|
60
|
+
|
61
|
+
1. Fork it ( http://github.com/shopkeep/hiatus/fork )
|
62
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
63
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
64
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
65
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/hiatus.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'hiatus/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "hiatus"
|
8
|
+
spec.version = Hiatus::VERSION
|
9
|
+
spec.authors = ["Alex Gessner"]
|
10
|
+
spec.email = ["alex.gessner@gmail.com"]
|
11
|
+
spec.summary = "Use a key-value store to pause some of your functionality."
|
12
|
+
spec.description = "This is useful for such things as maintenance mode, testing, and heavy migrations."
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "rspec"
|
22
|
+
spec.add_development_dependency "bundler"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
spec.add_development_dependency "fakeredis"
|
25
|
+
|
26
|
+
spec.add_runtime_dependency "redis"
|
27
|
+
end
|
data/lib/hiatus.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'hiatus/version'
|
2
|
+
require 'pausable'
|
3
|
+
|
4
|
+
module Hiatus
|
5
|
+
NAMESPACE = 'hiatus:'
|
6
|
+
private_constant :NAMESPACE
|
7
|
+
|
8
|
+
def self.summary
|
9
|
+
hiatus_all_keys.map do |key|
|
10
|
+
{
|
11
|
+
process: key.sub(NAMESPACE, ''),
|
12
|
+
seconds_remaining: Redis.current.ttl(key),
|
13
|
+
paused_at: Redis.current.get(key)
|
14
|
+
}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.pause(processes, seconds = 1800)
|
19
|
+
Array(processes).all? { |process| hiatus_update(process, seconds) }
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.paused?(process)
|
23
|
+
!!Redis.current.get( namespace(process) )
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def self.namespace(process)
|
29
|
+
NAMESPACE + process.to_s
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.hiatus_update(process, time)
|
33
|
+
Redis.current.setex(namespace(process), time, timestamp)
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.hiatus_all_keys
|
37
|
+
Redis.current.keys(NAMESPACE + '*')
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.timestamp
|
41
|
+
Redis.current.time.first
|
42
|
+
end
|
43
|
+
end
|
data/lib/pausable.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'hiatus'
|
2
|
+
|
3
|
+
module Hiatus
|
4
|
+
module Pausable
|
5
|
+
def pause(seconds = 1800)
|
6
|
+
Hiatus.pause(hiatus_key, seconds)
|
7
|
+
end
|
8
|
+
|
9
|
+
def paused?
|
10
|
+
!!Hiatus.paused?(hiatus_key)
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def hiatus_key
|
16
|
+
self.to_s.downcase
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Hiatus do
|
4
|
+
let(:redis) { Redis.new }
|
5
|
+
let(:redis_time) { Time.at(1385107188) }
|
6
|
+
|
7
|
+
before { Redis.current.stub(:time).and_return([redis_time.to_i, 000000]) }
|
8
|
+
|
9
|
+
describe '.summary' do
|
10
|
+
context 'when there are no paused processes' do
|
11
|
+
it 'is an empty array' do
|
12
|
+
expect(Hiatus.summary).to eql []
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'when there are two paused processes' do
|
17
|
+
let(:first_item) { Hiatus.summary.first }
|
18
|
+
|
19
|
+
before { Hiatus.pause([:franks, :beans], 1000) }
|
20
|
+
|
21
|
+
it 'has two items' do
|
22
|
+
expect(Hiatus.summary.count).to eql 2
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'the first element has the correct process' do
|
26
|
+
expect(first_item[:process]).to eql 'franks'
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'the first element has the correct seconds_remaining' do
|
30
|
+
expect(first_item[:seconds_remaining]).to be_within(1).of(1000)
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'the first element has the correct paused_at' do
|
34
|
+
expect(first_item[:paused_at]).to eql '1385107188'
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
describe '.pause' do
|
41
|
+
context 'with a single process and no time given' do
|
42
|
+
before { Hiatus.pause(:arbitrary_migration) }
|
43
|
+
|
44
|
+
it 'the computed key is not nil' do
|
45
|
+
expect(redis.get('hiatus:arbitrary_migration')).not_to be_nil
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'has a ttl of 30 minutes (1800 seconds)' do
|
49
|
+
time_remaining = redis.ttl('hiatus:arbitrary_migration')
|
50
|
+
expect(time_remaining).to be_within(5).of(1800)
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'has a value with the current timestamp' do
|
54
|
+
expect(redis.get('hiatus:arbitrary_migration')).to eql '1385107188'
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
context 'with a single process and explicit 45 seconds' do
|
59
|
+
before { Hiatus.pause(:arbitrary_migration, 45) }
|
60
|
+
|
61
|
+
it 'the computed key is not nil' do
|
62
|
+
expect(redis.get('hiatus:arbitrary_migration')).not_to be_nil
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'has a ttl of 45 seconds' do
|
66
|
+
time_remaining = redis.ttl('hiatus:arbitrary_migration')
|
67
|
+
expect(time_remaining).to be_within(5).of(45)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context 'with an array of processes and explicit 45 seconds' do
|
72
|
+
before do
|
73
|
+
Hiatus.pause([ :arbitrary_migration, :regular_processing_job, :transmission_worker ], 45)
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'arbitrary_migration has a key set' do
|
77
|
+
expect(redis.get('hiatus:arbitrary_migration')).not_to be_nil
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'regular_processing_job has a key set' do
|
81
|
+
expect(redis.get('hiatus:regular_processing_job')).not_to be_nil
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'transmission_worker has a key set' do
|
85
|
+
expect(redis.get('hiatus:transmission_worker')).not_to be_nil
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'arbitrary_migration has a ttl of 45 seconds' do
|
89
|
+
time_remaining = redis.ttl('hiatus:arbitrary_migration')
|
90
|
+
expect(time_remaining).to be_within(5).of(45)
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'regular_processing_job has a ttl of 45 seconds' do
|
94
|
+
time_remaining = redis.ttl('hiatus:regular_processing_job')
|
95
|
+
expect(time_remaining).to be_within(5).of(45)
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'transmission_worker has a ttl of 45 seconds' do
|
99
|
+
time_remaining = redis.ttl('hiatus:transmission_worker')
|
100
|
+
expect(time_remaining).to be_within(5).of(45)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
describe 'paused?' do
|
106
|
+
context 'with a paused arbitrary_migration' do
|
107
|
+
before { Hiatus.pause(:arbitrary_migration) }
|
108
|
+
|
109
|
+
it 'is true' do
|
110
|
+
expect(Hiatus.paused?(:arbitrary_migration)).to be_true
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
context 'without anything paused' do
|
115
|
+
it 'is false' do
|
116
|
+
expect(Hiatus.paused?(:green_skittles)).to be_false
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class Dummy
|
4
|
+
extend Hiatus::Pausable
|
5
|
+
end
|
6
|
+
|
7
|
+
describe Hiatus::Pausable do
|
8
|
+
let(:redis) { Redis.new }
|
9
|
+
let(:redis_time) { Time.at(1385107188) }
|
10
|
+
|
11
|
+
before { Redis.current.stub(:time).and_return([redis_time.to_i, 000000]) }
|
12
|
+
|
13
|
+
describe 'pause' do
|
14
|
+
context 'without a number of seconds specified' do
|
15
|
+
before { Dummy.pause }
|
16
|
+
|
17
|
+
it 'the computed key is not nil' do
|
18
|
+
expect(redis.get('hiatus:dummy')).not_to be_nil
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'has a ttl of 30 minutes (1800 seconds)' do
|
22
|
+
time_remaining = redis.ttl('hiatus:dummy')
|
23
|
+
expect(time_remaining).to be_within(5).of(1800)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'with 246 seconds' do
|
28
|
+
before { Dummy.pause(246) }
|
29
|
+
|
30
|
+
it 'the computed key is not nil' do
|
31
|
+
expect(redis.get('hiatus:dummy')).not_to be_nil
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'has a ttl of 246 seconds' do
|
35
|
+
time_remaining = redis.ttl('hiatus:dummy')
|
36
|
+
expect(time_remaining).to be_within(5).of(246)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe 'paused?' do
|
42
|
+
context 'when it has been paused' do
|
43
|
+
before { Dummy.pause }
|
44
|
+
|
45
|
+
it 'is true' do
|
46
|
+
expect(Dummy.paused?).to be_true
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context 'when it has not been paused' do
|
51
|
+
it 'is false' do
|
52
|
+
expect(Dummy.paused?).to be_false
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hiatus
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alex Gessner
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
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: fakeredis
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: redis
|
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: This is useful for such things as maintenance mode, testing, and heavy
|
84
|
+
migrations.
|
85
|
+
email:
|
86
|
+
- alex.gessner@gmail.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- .gitignore
|
92
|
+
- .rspec
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- hiatus.gemspec
|
98
|
+
- lib/hiatus.rb
|
99
|
+
- lib/hiatus/version.rb
|
100
|
+
- lib/pausable.rb
|
101
|
+
- spec/lib/hiatus_spec.rb
|
102
|
+
- spec/lib/pausable_spec.rb
|
103
|
+
- spec/spec_helper.rb
|
104
|
+
homepage: ''
|
105
|
+
licenses:
|
106
|
+
- MIT
|
107
|
+
metadata: {}
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
require_paths:
|
111
|
+
- lib
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - '>='
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - '>='
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 2.1.11
|
125
|
+
signing_key:
|
126
|
+
specification_version: 4
|
127
|
+
summary: Use a key-value store to pause some of your functionality.
|
128
|
+
test_files:
|
129
|
+
- spec/lib/hiatus_spec.rb
|
130
|
+
- spec/lib/pausable_spec.rb
|
131
|
+
- spec/spec_helper.rb
|