stubborn_queue 1.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cd521876afe1fe93b9c321a2e327d6febafbffca
4
+ data.tar.gz: 4ec69ba695ae13d3ae15ab0be2f97a20c46f1b3f
5
+ SHA512:
6
+ metadata.gz: 9711271fa224a1a5790fc96d902da9909585ec4a0feeaa46984582e43df7067dd88ebcd912cfd42160df91ae6522017b1795ed1f824699e6e05c8efad3a97e50
7
+ data.tar.gz: ef3826b2a010680d2b0bcf4b9a164b8ef4d5f6e15505b0d585d0e426561316ea48ca64ebfddcf473cdbd3a60c1553902ec470c464f0bdcbb71661f6cbd226b13
data/.gitignore ADDED
@@ -0,0 +1,39 @@
1
+ .*.db
2
+
3
+ # ---
4
+
5
+ *.gem
6
+ *.rbc
7
+ /.config
8
+ /coverage/
9
+ /InstalledFiles
10
+ /pkg/
11
+ /spec/reports/
12
+ /test/tmp/
13
+ /test/version_tmp/
14
+ /tmp/
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+
21
+ ## Documentation cache and generated files:
22
+ /.yardoc/
23
+ /_yardoc/
24
+ /doc/
25
+ /rdoc/
26
+
27
+ ## Environment normalisation:
28
+ /.bundle/
29
+ /vendor/bundle
30
+ /lib/bundler/man/
31
+
32
+ # for a library or gem, you might want to ignore these files since the code is
33
+ # intended to run in multiple environments; otherwise, check them in:
34
+ # Gemfile.lock
35
+ # .ruby-version
36
+ # .ruby-gemset
37
+
38
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
39
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'http://rubygems.org'
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ stubborn_queue (0.1.0)
5
+ bundler
6
+ daybreak
7
+ moneta
8
+ redislike
9
+
10
+ GEM
11
+ remote: http://rubygems.org/
12
+ specs:
13
+ daybreak (0.3.0)
14
+ docile (1.1.5)
15
+ json (1.8.2)
16
+ kintama (0.1.13)
17
+ moneta (0.8.0)
18
+ rake (10.4.2)
19
+ redislike (0.2.5)
20
+ moneta (~> 0.8)
21
+ simplecov (0.10.0)
22
+ docile (~> 1.1.0)
23
+ json (~> 1.8)
24
+ simplecov-html (~> 0.10.0)
25
+ simplecov-html (0.10.0)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ kintama
32
+ rake
33
+ simplecov
34
+ stubborn_queue!
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Justin Scott
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 all
13
+ 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 THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ This is in no way inherently thread- or process-safe. It's built for one process owning one queue file and not interacting with it asynchronously.
2
+
3
+ Huge props to [Chris Olstrom](https://github.com/colstrom) who helped me understand the idea and was a good sport when I implemented my own version instead of waiting for him to finish his.
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'bundler/setup'
2
+ require 'simplecov'
3
+
4
+ task :test do
5
+ SimpleCov.command_name 'Kintama Tests'
6
+ SimpleCov.start do
7
+ add_filter "/vendor/"
8
+ add_filter "/test/"
9
+ end
10
+ require_relative 'test/queueing_spec'
11
+ end
@@ -0,0 +1,87 @@
1
+ require 'moneta'
2
+ require 'daybreak'
3
+ require 'redislike'
4
+
5
+ class StubbornQueue
6
+ attr_reader :db
7
+ def initialize(options = {})
8
+ @name = options.fetch :name, 'default'
9
+ @timeout = options.fetch :timeout, 60
10
+ @db = Moneta.new :Daybreak, expires: true, file: ".#{@name}.db"
11
+ end
12
+
13
+ def key_for(type, with_id: 0)
14
+ key = case type
15
+ when :pending_list
16
+ "#{@name}:pending"
17
+ when :claimed_list
18
+ "#{@name}:claimed"
19
+ when :claimed_flag
20
+ "#{@name}:task:#{with_id}:claimed"
21
+ when :finished_flag
22
+ "#{@name}:task:#{with_id}:finished"
23
+ when :item_store
24
+ "#{@name}:@{id}"
25
+ when :id_count
26
+ "#{@name}:id_count"
27
+ else
28
+ raise "Key for '#{type}' is unrecognized."
29
+ end
30
+ "#{@name}:#{key}"
31
+ end
32
+
33
+ def create_id
34
+ @db.increment key_for(:id_count)
35
+ end
36
+
37
+ def lookup(id)
38
+ @db.fetch key_for(:item_store, with_id: id), nil
39
+ end
40
+
41
+ def claims
42
+ @db.fetch key_for(:claimed_list), []
43
+ end
44
+
45
+ def recent_claim?(id)
46
+ @db.fetch key_for(:claimed_flag, with_id: id), false
47
+ end
48
+
49
+ def finished?(id)
50
+ @db.fetch key_for(:finished_flag, with_id: id), false
51
+ end
52
+
53
+ def remove_claim_on(id)
54
+ @db.lrem key_for(:claimed_list), 0, id
55
+ @db.delete key_for(:claimed_flag, with_id: id)
56
+ end
57
+
58
+ def enqueue(item)
59
+ process_expired_claims
60
+ id = create_id
61
+ @db.store key_for(:item_store, with_id: id), item
62
+ @db.lpush key_for(:pending_list), id
63
+ end
64
+
65
+ def dequeue
66
+ process_expired_claims
67
+ id = @db.rpoplpush key_for(:pending_list), key_for(:claimed_list)
68
+ @db.store key_for(:claimed_flag, with_id: id), true, expires: @timeout
69
+ id
70
+ end
71
+
72
+ def requeue(id)
73
+ @db.lpush key_for(:pending_list), id
74
+ end
75
+
76
+ def finish(id)
77
+ @db.store key_for(:finished_flag, with_id: id), true
78
+ end
79
+
80
+ def process_expired_claims
81
+ claims.each do |id|
82
+ next if recent_claim? id
83
+ remove_claim_on id
84
+ requeue id unless finished? id
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,25 @@
1
+ Gem::Specification.new do |gem|
2
+ gem.name = 'stubborn_queue'
3
+ gem.version = '1.0.0'
4
+ gem.licenses = 'MIT'
5
+ gem.authors = ['Justin Scott']
6
+ gem.email = 'jvscott@gmail.com'
7
+ gem.homepage = 'http://www.github.com/jscott/stubborn_queue/'
8
+ gem.summary = 'Stubborn Queue'
9
+ gem.description = 'Queueing that keeps on kicking until you tell it to stop.'
10
+
11
+ gem.files = `git ls-files`.split("\n")
12
+ gem.test_files = `git ls-files -- test/**/*`.split("\n")
13
+ gem.require_paths = ['lib']
14
+
15
+ gem.required_ruby_version = '>= 2.1'
16
+
17
+ gem.add_runtime_dependency 'bundler'
18
+ gem.add_runtime_dependency 'moneta'
19
+ gem.add_runtime_dependency 'daybreak'
20
+ gem.add_runtime_dependency 'redislike'
21
+
22
+ gem.add_development_dependency 'rake'
23
+ gem.add_development_dependency 'kintama'
24
+ gem.add_development_dependency 'simplecov'
25
+ end
@@ -0,0 +1,33 @@
1
+ require 'bundler/setup'
2
+ require 'kintama'
3
+ require 'stubborn_queue'
4
+
5
+ context 'queueing' do
6
+ setup do
7
+ @timeout = 1
8
+ @queue = StubbornQueue.new name: 'test', timeout: @timeout
9
+ @queue.db.clear
10
+ @item = 'test_item'
11
+ end
12
+
13
+ should 'add and remove items' do
14
+ assert_equal 1, @queue.enqueue(@item)
15
+ id = @queue.dequeue
16
+ assert_equal @item, @queue.lookup(id)
17
+ end
18
+
19
+ should 'requeue items that don\'t finish' do
20
+ assert_equal 1, @queue.enqueue(@item)
21
+ @queue.dequeue
22
+ sleep (@timeout+1)
23
+ assert_equal 2, @queue.enqueue(@item)
24
+ end
25
+
26
+ should 'be able to finish items and not requeue them' do
27
+ assert_equal 1, @queue.enqueue(@item)
28
+ id = @queue.dequeue
29
+ @queue.finish id
30
+ sleep (@timeout+1)
31
+ assert_equal 1, @queue.enqueue(@item)
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stubborn_queue
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Justin Scott
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-08 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: '0'
20
+ type: :runtime
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: moneta
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: daybreak
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: redislike
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
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: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: kintama
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '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'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Queueing that keeps on kicking until you tell it to stop.
112
+ email: jvscott@gmail.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - ".gitignore"
118
+ - Gemfile
119
+ - Gemfile.lock
120
+ - LICENSE
121
+ - README.md
122
+ - Rakefile
123
+ - lib/stubborn_queue.rb
124
+ - stubborn_queue.gemspec
125
+ - test/queueing_spec.rb
126
+ homepage: http://www.github.com/jscott/stubborn_queue/
127
+ licenses:
128
+ - MIT
129
+ metadata: {}
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '2.1'
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ requirements: []
145
+ rubyforge_project:
146
+ rubygems_version: 2.4.5
147
+ signing_key:
148
+ specification_version: 4
149
+ summary: Stubborn Queue
150
+ test_files: []
151
+ has_rdoc: