batch_queue 0.1.0 → 1.1.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 +5 -5
- data/.github/workflows/ci.yml +18 -0
- data/.gitignore +1 -1
- data/CHANGELOG.md +9 -0
- data/Gemfile.lock +6 -5
- data/README.md +65 -4
- data/batch_queue.gemspec +2 -2
- data/lib/batch_queue/batch_queue.rb +14 -7
- data/lib/batch_queue/version.rb +1 -1
- metadata +13 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3b0f824265dd97a0c9992e49bb276d1131a79851ee6392c477941aa2c34261f4
|
4
|
+
data.tar.gz: 3f4067e301015ee56ba0263d8da5ef79b5b2a897bac6ffc863e14b31a5fdd4b9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eab99b1882c61b1ae01d68ece1680e055f5818e979869c6bd9f38e505f388e1768350468c8ac344326c5d7baf96968bd61bd3f74abaad144f3563f8c449e515d
|
7
|
+
data.tar.gz: 5101491e33aca1f85e1d614ff6dabe3c825b68cc131164dbf4f4cc43074ee17aeb2868d26e62645aa6a617395e50a42d8834fe65d21274a690d0fab11bf7708f
|
@@ -0,0 +1,18 @@
|
|
1
|
+
name: CI
|
2
|
+
on: [push, pull_request]
|
3
|
+
jobs:
|
4
|
+
test:
|
5
|
+
strategy:
|
6
|
+
fail-fast: false
|
7
|
+
matrix:
|
8
|
+
os: [ubuntu-latest, macos-latest]
|
9
|
+
# Could consider adding head, jruby-head, jruby in the future
|
10
|
+
ruby: ['2.7', '3.0', '3.1', '3.2', '3.3', truffleruby, truffleruby-head]
|
11
|
+
runs-on: ${{ matrix.os }}
|
12
|
+
steps:
|
13
|
+
- uses: actions/checkout@v4
|
14
|
+
- uses: ruby/setup-ruby@v1
|
15
|
+
with:
|
16
|
+
ruby-version: ${{ matrix.ruby }}
|
17
|
+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
18
|
+
- run: bundle exec rake
|
data/.gitignore
CHANGED
data/CHANGELOG.md
ADDED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
batch_queue (
|
4
|
+
batch_queue (1.1.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -14,18 +14,19 @@ GEM
|
|
14
14
|
builder
|
15
15
|
minitest (>= 5.0)
|
16
16
|
ruby-progressbar
|
17
|
-
rake (
|
17
|
+
rake (13.0.1)
|
18
18
|
ruby-progressbar (1.10.0)
|
19
19
|
|
20
20
|
PLATFORMS
|
21
21
|
ruby
|
22
|
+
universal-darwin-24
|
22
23
|
|
23
24
|
DEPENDENCIES
|
24
25
|
batch_queue!
|
25
|
-
bundler (~>
|
26
|
+
bundler (~> 2.2)
|
26
27
|
minitest (~> 5.0)
|
27
28
|
minitest-reporters (~> 1.4)
|
28
|
-
rake (~>
|
29
|
+
rake (~> 13.0)
|
29
30
|
|
30
31
|
BUNDLED WITH
|
31
|
-
|
32
|
+
2.3.26
|
data/README.md
CHANGED
@@ -1,8 +1,25 @@
|
|
1
1
|
# BatchQueue
|
2
2
|
|
3
|
-
|
3
|
+
BatchQueue is queue that takes jobs and runs them, in aggregate, via a callback on a background thread. You can process a “batch” of N jobs at a time or after T seconds whichever comes sooner.
|
4
4
|
|
5
|
-
|
5
|
+
## Example
|
6
|
+
You want to send metrics to Amazon’s AWS CloudWatch service every 60 seconds or when the batch size reaches 20, whichever comes first. You might write code like this:
|
7
|
+
|
8
|
+
```
|
9
|
+
# Create the AWS CloudWatch Client
|
10
|
+
cw_client = Aws::CloudWatch::Client.new(...)
|
11
|
+
|
12
|
+
# Set up the BatchQueue
|
13
|
+
BatchQueue.new(max_batch_size: 20, max_interval_seconds: 60) do |batch_metric_data|
|
14
|
+
cw_client.put_metric_data(:metric_data => batch_metric_data)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Add to the BatchQueue
|
18
|
+
@bq << {
|
19
|
+
metric_name: 'Widgets',
|
20
|
+
value: 1
|
21
|
+
}
|
22
|
+
```
|
6
23
|
|
7
24
|
## Installation
|
8
25
|
|
@@ -22,7 +39,51 @@ Or install it yourself as:
|
|
22
39
|
|
23
40
|
## Usage
|
24
41
|
|
25
|
-
|
42
|
+
### 1. Set up the BatchQueue
|
43
|
+
Each BatchQueue gets its own background thread that executes jobs.
|
44
|
+
```
|
45
|
+
bq = BatchQueue.new(max_batch_size: 20, max_interval_seconds: 60) do |batch_metric_data|
|
46
|
+
# Put your code that you want to execute here.
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
### 2. Add a job to the queue
|
51
|
+
You can add any object to the queue.
|
52
|
+
```
|
53
|
+
bq << {
|
54
|
+
# your object here.
|
55
|
+
}
|
56
|
+
|
57
|
+
```
|
58
|
+
or
|
59
|
+
```
|
60
|
+
bq << MyJob.new(...)
|
61
|
+
|
62
|
+
```
|
63
|
+
### 3. Error handling
|
64
|
+
You have two options for handling errors in `BatchQueue`:
|
65
|
+
|
66
|
+
* Rescue exceptions within the processing block:
|
67
|
+
|
68
|
+
```
|
69
|
+
bq = BatchQueue.new(max_batch_size: 20, max_interval_seconds: 60) do |batch_metric_data|
|
70
|
+
begin
|
71
|
+
# Put your code that you want to execute here.
|
72
|
+
rescue => e
|
73
|
+
# Handle the exception here.
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
```
|
78
|
+
|
79
|
+
* Set a global error handler:
|
80
|
+
|
81
|
+
```
|
82
|
+
bq.on_error = ->(e) { puts e.message }
|
83
|
+
```
|
84
|
+
|
85
|
+
If neither method is used, `BatchQueue` will catch the exception and print it to
|
86
|
+
the standard console output.
|
26
87
|
|
27
88
|
## Development
|
28
89
|
|
@@ -32,7 +93,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
32
93
|
|
33
94
|
## Contributing
|
34
95
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
96
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/flivni/batch_queue.
|
36
97
|
|
37
98
|
## License
|
38
99
|
|
data/batch_queue.gemspec
CHANGED
@@ -23,8 +23,8 @@ Gem::Specification.new do |spec|
|
|
23
23
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
24
|
spec.require_paths = ["lib"]
|
25
25
|
|
26
|
-
spec.add_development_dependency "bundler", "~>
|
27
|
-
spec.add_development_dependency "rake", "~>
|
26
|
+
spec.add_development_dependency "bundler", "~> 2.2"
|
27
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
28
28
|
spec.add_development_dependency "minitest", "~> 5.0"
|
29
29
|
spec.add_development_dependency "minitest-reporters", '~> 1.4'
|
30
30
|
end
|
@@ -1,4 +1,7 @@
|
|
1
1
|
class BatchQueue
|
2
|
+
attr_reader :max_batch_size
|
3
|
+
attr_reader :max_interval_seconds
|
4
|
+
|
2
5
|
# starts the queue
|
3
6
|
# either max_batch_size or interval_milliseconds or both must be set
|
4
7
|
def initialize(max_batch_size: nil, max_interval_seconds: nil, &block)
|
@@ -13,17 +16,13 @@ class BatchQueue
|
|
13
16
|
@mutex = Mutex.new
|
14
17
|
@cond_var = ConditionVariable.new
|
15
18
|
@runner = Thread.new { run }
|
19
|
+
@on_error_callback = nil
|
16
20
|
|
17
21
|
at_exit do
|
18
22
|
stop
|
19
23
|
end
|
20
24
|
end
|
21
25
|
|
22
|
-
# a block taking taking an exception as a parameter
|
23
|
-
def on_error(&block)
|
24
|
-
@on_error = block
|
25
|
-
end
|
26
|
-
|
27
26
|
def push(object)
|
28
27
|
@mutex.synchronize do
|
29
28
|
raise 'BatchQueue is stopped' unless @is_running
|
@@ -49,6 +48,10 @@ class BatchQueue
|
|
49
48
|
@runner.join
|
50
49
|
end
|
51
50
|
|
51
|
+
def on_error(&block)
|
52
|
+
@on_error_callback = block
|
53
|
+
end
|
54
|
+
|
52
55
|
private
|
53
56
|
|
54
57
|
def run
|
@@ -86,8 +89,12 @@ class BatchQueue
|
|
86
89
|
@mutex.unlock
|
87
90
|
begin
|
88
91
|
@block.call(arr)
|
89
|
-
rescue StandardError =>
|
90
|
-
|
92
|
+
rescue StandardError => e
|
93
|
+
if @on_error_callback
|
94
|
+
@on_error_callback.call(e)
|
95
|
+
else
|
96
|
+
puts "BatchQueue: Unhandled exception #{exc.inspect}"
|
97
|
+
end
|
91
98
|
ensure
|
92
99
|
@mutex.lock
|
93
100
|
end
|
data/lib/batch_queue/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: batch_queue
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Felix Livni
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-11-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -16,28 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '2.2'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '2.2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '13.0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '13.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: minitest
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,15 +66,17 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '1.4'
|
69
|
-
description:
|
69
|
+
description:
|
70
70
|
email:
|
71
71
|
- flivni@gmail.com
|
72
72
|
executables: []
|
73
73
|
extensions: []
|
74
74
|
extra_rdoc_files: []
|
75
75
|
files:
|
76
|
+
- ".github/workflows/ci.yml"
|
76
77
|
- ".gitignore"
|
77
78
|
- ".travis.yml"
|
79
|
+
- CHANGELOG.md
|
78
80
|
- Gemfile
|
79
81
|
- Gemfile.lock
|
80
82
|
- LICENSE.txt
|
@@ -90,7 +92,7 @@ homepage: https://github.com/flivni/batch_queue
|
|
90
92
|
licenses:
|
91
93
|
- MIT
|
92
94
|
metadata: {}
|
93
|
-
post_install_message:
|
95
|
+
post_install_message:
|
94
96
|
rdoc_options: []
|
95
97
|
require_paths:
|
96
98
|
- lib
|
@@ -105,9 +107,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
105
107
|
- !ruby/object:Gem::Version
|
106
108
|
version: '0'
|
107
109
|
requirements: []
|
108
|
-
|
109
|
-
|
110
|
-
signing_key:
|
110
|
+
rubygems_version: 3.3.27
|
111
|
+
signing_key:
|
111
112
|
specification_version: 4
|
112
113
|
summary: An in-memory queue that takes data and allows you to process it, in batches,
|
113
114
|
on a background thread.
|