redis_batch 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ead79130daaa5dd59b528e53f0069b2d7520f2b40001e611ea0e1ce97baadc33
4
- data.tar.gz: c63abeed3e5f550ea5ce84356ac4183e9042fec24e12d87c72a6210240846d57
3
+ metadata.gz: 64bdfe75e083eae21c7aa0b366a44850903cb0911546644a0ade2f071d753a02
4
+ data.tar.gz: 30ea83d6a94380af5c01df6768869cd8c25dbb472d150549fda31279e74684f1
5
5
  SHA512:
6
- metadata.gz: 9db50906c0891b09654a35d151096df5ebdd89b8fa6b854645d6eba5223840876f16fed94f3299bb12497f59dc92933169b4b67a73fb2609b82df810ab9d195e
7
- data.tar.gz: 34ad2d47797b5e5fe7d73da4a300072e8a028ecb6f8a9fb971c42d7b67d5a0ee0cab5bf8e39ed763bc330bd0ec719a5a944e4d6d2df54525e69feeed284c19fb
6
+ metadata.gz: 93edb30262b55d8d1ba17e2a551cecbf8e8236c33cac3f60eb7288dfd831c3614e0565a6d448f0b25171b862021033779612e9595e55ea2e692ae6760bfa5ec8
7
+ data.tar.gz: 4c143ce57f44c0ae0638b4901672c06fe37278a18b7ce1b7f846a507bf71bd0b748c71f2a65b0891cd84602e7a8ca4e045aaa5e09e75b56e5545c80af547678f
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Christian Avemark
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.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ ## Redis Batch
2
+
3
+ ![tests](https://github.com/avemark/redis_batch_rb/actions/workflows/tests.yaml/badge.svg)
4
+
5
+ A redis queue with reliable multi-element dequeue, intended for work aggregation.
6
+ Requires redis version >= 7
7
+
8
+ Common background work libraries like Resque, Sidekiq and GoodJob assume no coherence in
9
+ the elements of their work queues, and will act on elements one by one.
10
+
11
+ Sometimes you want to asynchronously aggregate work units and later deal with them in batches, and this gem intends
12
+ to facilitate that kind of pattern.
13
+
14
+ This gem does not try to deal with scheduling of dequeueing nor does it assist (much) with error handling.
15
+
16
+ If an error is raised within the `take` block, the taken values are returned to the queue.
17
+
18
+ If your process is killed for external reasons, the taken items might get stuck in the processing queue. `myqueue.abort_all` will clear all processing queues.
19
+ If you take out elements concurrently, there is currently no way to distinguish such stuck processing queues from healthy queues. A timestamping feature to alleviate this might be added in the future.
20
+ ### Usage
21
+ ```Shell
22
+ gem install redis_batch
23
+ ```
24
+ ```Ruby
25
+ MessageQueue = Class.new(RedisBatch::Queue)
26
+ my_queue = MessageQueue.new
27
+
28
+ my_queue.add "Hello", "world"
29
+ my_queue.count => 2
30
+
31
+ the_same_queue = RedisBatch::Queue.new("MessageQueue")
32
+ the_same_queue.count => 2
33
+
34
+ my_queue.take(10) { |messages| messages.join(", ") } => "Hello, world"
35
+ my_queue.count => 0
36
+
37
+ my_queue.add "uh", "oh"
38
+ my_queue.take(10) { |messages| raise(messages.join("-")) } => StandardError: "uh-oh"
39
+ my_queue.count => 2
40
+ ```
@@ -0,0 +1,42 @@
1
+ module RedisBatch
2
+ class Lua
3
+ def self.function_load(redis)
4
+ redis.call("FUNCTION", "LOAD", "REPLACE", redis_batch_lua)
5
+ end
6
+
7
+ def self.redis_batch_lua
8
+ <<~LUA
9
+ #!lua name=redis_batch
10
+
11
+ local function lmove(keys, args)
12
+ local count = tonumber(args[1])
13
+ local source = keys[1]
14
+ local target = keys[2]
15
+ local llen = redis.call("LLEN", source)
16
+ if llen < count then
17
+ count = llen
18
+ end
19
+ local values = {}
20
+ for i=1,count do
21
+ values[i] = redis.call("LMOVE", source, target, 'LEFT', 'RIGHT')
22
+ end
23
+ return values
24
+ end
25
+
26
+ redis.register_function('rb_lmove', lmove)
27
+
28
+ local function lrestore(keys, args)
29
+ local source = keys[1]
30
+ local target = keys[2]
31
+ local llen = redis.call("LLEN", source)
32
+ for i=1,llen do
33
+ redis.call("LMOVE", source, target, 'RIGHT', 'LEFT')
34
+ end
35
+ return llen
36
+ end
37
+
38
+ redis.register_function('rb_lrestore', lrestore)
39
+ LUA
40
+ end
41
+ end
42
+ end
@@ -3,7 +3,7 @@ module RedisBatch
3
3
  def initialize(namespace = self.class.name)
4
4
  @namespace = namespace
5
5
  @client = Client.instance
6
- @client.with { |redis| Functions.function_load(redis) }
6
+ @client.with { |redis| RedisBatch::Lua.function_load(redis) }
7
7
  end
8
8
 
9
9
  def add(*items)
data/lib/redis_batch.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  require 'redis'
2
- require './lib/redis_batch/configurable'
3
- require './lib/redis_batch/client'
4
- require './lib/redis_batch/functions'
5
- require './lib/redis_batch/queue'
2
+ require 'redis_batch/configurable'
3
+ require 'redis_batch/lua'
4
+ require 'redis_batch/client'
5
+ require 'redis_batch/queue'
6
6
 
7
7
  module RedisBatch
8
8
  Error = Class.new(StandardError)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis_batch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Avemark
@@ -31,9 +31,12 @@ executables: []
31
31
  extensions: []
32
32
  extra_rdoc_files: []
33
33
  files:
34
+ - LICENSE
35
+ - README.md
34
36
  - lib/redis_batch.rb
35
37
  - lib/redis_batch/client.rb
36
38
  - lib/redis_batch/configurable.rb
39
+ - lib/redis_batch/lua.rb
37
40
  - lib/redis_batch/queue.rb
38
41
  homepage: https://github.com/avemark/redis_batch_rb
39
42
  licenses: