bg_results 0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cebb816663b5bd96fa1ff2c765736ed93b6547dcdd565a4c382f252e5288c6d5
4
+ data.tar.gz: d3a31791a234e31a826d792ac857bf3a12380cf3d490c0bf7fcec975d08e7189
5
+ SHA512:
6
+ metadata.gz: 109900b1428de3d74f39d3fd122b062acafb7cd8fa59f9404129aaba3b3bd3f62d4218dd4cba5d6b837f8f015b192ba0970baebb61c7211b1061721faab16fa6
7
+ data.tar.gz: 58fed360f9505a4c6347aa0586b58b348422537ecefcc2c6123eb34c8ec0135759b8876ce55bf3be5ee4a42f257e1cc4ce0cac2e514d44ce4a45912288e1dc97
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.16.0
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in bg_results.gemspec
6
+ gemspec
@@ -0,0 +1,29 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bg_results (0.1.0)
5
+ connection_pool (~> 2.0)
6
+ redis
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ connection_pool (2.2.2)
12
+ fakeredis (0.7.0)
13
+ redis (>= 3.2, < 5.0)
14
+ minitest (5.11.3)
15
+ rake (10.5.0)
16
+ redis (4.0.3)
17
+
18
+ PLATFORMS
19
+ ruby
20
+
21
+ DEPENDENCIES
22
+ bg_results!
23
+ bundler (~> 1.16)
24
+ fakeredis (~> 0.7)
25
+ minitest (~> 5.0)
26
+ rake (~> 10.0)
27
+
28
+ BUNDLED WITH
29
+ 1.17.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Jason
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
13
+ all 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
21
+ THE SOFTWARE.
@@ -0,0 +1,99 @@
1
+ # BgResults
2
+
3
+ A simple tool for collecting results from background job batches.
4
+ Results returned from the jobs are stored in a Redis hash.
5
+
6
+ Currently Sidekiq is supported using the 3rd party batch gem, but it should work with Sidekiq pro as well. Worker modules for other queue systems can easily be added to support different providers.
7
+
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'bg_results'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install bg_results
24
+
25
+ ## Usage
26
+
27
+ ### Configure Redis
28
+
29
+ A Redis connection pool is maintained to support flexibility from queue provider.
30
+
31
+ Redis can be configured using the `REDIS_URL` environment variable.
32
+
33
+ Otherwise you can supply your own connection pool or use Sidekiq's.
34
+
35
+ ```ruby
36
+ BgResults.redis_pool = your_pool || Sidekiq.redis_pool
37
+ ```
38
+ ### Data
39
+
40
+ Results are stored in redis as JSON strings. Any datatype valid in JSON will work.
41
+
42
+ ### Workers
43
+ To enable results in a sidekiq worker `prepend BgResults::Workers::Sidekiq` and the return value of `perform` will be collected.
44
+ ```ruby
45
+ class ResultsJobThe
46
+ include Sidekiq::Worker
47
+ prepend BgResults::Worker::Sidekiq
48
+
49
+ def perform
50
+ result = background_task
51
+ end
52
+ end
53
+ ```
54
+
55
+ ### Collect results
56
+ Use the results batch object to access background results in the batch callbacks. Results are identified by batch_id.
57
+
58
+ ```ruby
59
+ def on_success status, options
60
+ batch_res = BgResults::Batch.new status.bid
61
+ end
62
+ ```
63
+
64
+ #### All in one
65
+ Return all results in a hash with mapping `{job_id => result}`.
66
+
67
+ ```ruby
68
+ results = batch_res.results
69
+ ```
70
+
71
+ #### Scan
72
+ Collect results in batches with redis scan.
73
+ ```ruby
74
+ batch_res.results_each do |job_id, result|
75
+ puts result
76
+ end
77
+ ```
78
+
79
+ #### Lifetime
80
+ Results are persisted for up to 24 hours after first access to allow for callback retry and debugging.
81
+
82
+ In order to prevent redis from filling up with large results they should be cleared manually upon success.
83
+ ```ruby
84
+ batch_res.clear_results!
85
+ ```
86
+
87
+ ## Development
88
+
89
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
90
+
91
+ 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 run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
92
+
93
+ ## Contributing
94
+
95
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/bg_results.
96
+
97
+ ## License
98
+
99
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,40 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "bg_results/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bg_results"
8
+ spec.version = BgResults::VERSION
9
+ spec.authors = ["Jason"]
10
+ spec.email = ["jabrady42@gmail.com"]
11
+
12
+ spec.summary = %q{A tool for handling results from background workers}
13
+ spec.description = %q{A tool for handling results from background workers}
14
+ spec.homepage = "https://github.com/jbrady42/bg_results"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_dependency "redis"
34
+ spec.add_dependency "connection_pool", "~>2.0"
35
+
36
+ spec.add_development_dependency "bundler", "~> 1.16"
37
+ spec.add_development_dependency "rake", "~> 10.0"
38
+ spec.add_development_dependency "minitest", "~> 5.0"
39
+ spec.add_development_dependency "fakeredis", "~> 0.7"
40
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "bg_results"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,23 @@
1
+ require "connection_pool"
2
+ require "redis"
3
+ require "json"
4
+
5
+ require "bg_results/version"
6
+ require "bg_results/batch"
7
+ require "bg_results/worker/sidekiq"
8
+
9
+ module BgResults
10
+ def self.redis_pool
11
+ @redis ||= ConnectionPool.new(size: 5, timeout: 5) { Redis.new }
12
+ end
13
+
14
+ def self.redis_pool= pool
15
+ @redis = pool
16
+ end
17
+
18
+ def self.redis
19
+ redis_pool.with do |redis|
20
+ yield redis
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,67 @@
1
+ module BgResults
2
+ class Batch
3
+ RESULT_TTL = 86400 # 1 day
4
+ def initialize bid
5
+ @bid = bid
6
+ @key = "RESULTS-#{@bid}"
7
+ end
8
+
9
+ def results
10
+ data, _ = BgResults.redis do |conn|
11
+ conn.multi do
12
+ conn.hgetall @key
13
+ conn.expire @key, RESULT_TTL
14
+ end
15
+ end
16
+ parse_results data
17
+ end
18
+
19
+ def results_in_batches
20
+ count = 100
21
+ cursor = 0
22
+ loop do
23
+ cursor, data = scan cursor, count
24
+ data = parse_results data.to_h
25
+ yield data
26
+ break if cursor == 0
27
+ end
28
+ set_results_expire
29
+ end
30
+
31
+ def results_each
32
+ return unless block_given?
33
+ results_in_batches do |batch|
34
+ batch.each do |jid, res|
35
+ yield jid, res
36
+ end
37
+ end
38
+ end
39
+
40
+ def set_results_expire exp=RESULT_TTL
41
+ BgResults.redis do |conn|
42
+ conn.expire @key, exp
43
+ end
44
+ end
45
+
46
+ def clear_results!
47
+ BgResults.redis do |conn|
48
+ conn.del @key
49
+ end
50
+ end
51
+
52
+ private
53
+ def scan cursor, count
54
+ new_cursor, data = BgResults.redis do |conn|
55
+ conn.hscan @key, cursor, count: count
56
+ end
57
+ [new_cursor.to_i, data]
58
+ end
59
+
60
+ def parse_results data
61
+ data = data.map do |k,v|
62
+ payload = JSON.parse v
63
+ [k, payload["result"]]
64
+ end.to_h
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,3 @@
1
+ module BgResults
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,15 @@
1
+ module BgResults
2
+ module Worker
3
+ module Sidekiq
4
+ def perform *args
5
+ result = super
6
+ if bid && jid
7
+ data = {result: result}.to_json
8
+ BgResults.redis do |conn|
9
+ conn.hset "RESULTS-#{bid}", jid, data
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bg_results
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jason
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-10-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: redis
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: connection_pool
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: fakeredis
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.7'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.7'
97
+ description: A tool for handling results from background workers
98
+ email:
99
+ - jabrady42@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".travis.yml"
106
+ - Gemfile
107
+ - Gemfile.lock
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - bg_results.gemspec
112
+ - bin/console
113
+ - bin/setup
114
+ - lib/bg_results.rb
115
+ - lib/bg_results/batch.rb
116
+ - lib/bg_results/version.rb
117
+ - lib/bg_results/worker/sidekiq.rb
118
+ homepage: https://github.com/jbrady42/bg_results
119
+ licenses:
120
+ - MIT
121
+ metadata:
122
+ allowed_push_host: https://rubygems.org
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 2.7.8
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: A tool for handling results from background workers
143
+ test_files: []