batchmaker 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4e3307b2208f99ffe62096726ed2d86534fc8ef28ad3afb160f9353282c9af2a
4
+ data.tar.gz: a76355d448bb4fb726d5ffb4d7f2437d20afde8df33ce439542e9bc6c9c49ca1
5
+ SHA512:
6
+ metadata.gz: a48cde0031613f8c67d41f3d4a3f8a49559353be20742b7245dd673c1fec8982aaf1e7a162f15e685fc325cfa0dda76e449a62cd3f54dae27d451b560a240808
7
+ data.tar.gz: 892a4a328f10eb62660af8d32cd28df42151e7a834411c202425647464f02d6559351c5917d2980d30bf9e5f9ea1895676fa5b84221808b10a0822ef4c9984b7
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at krajagopalan@doximity.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
@@ -0,0 +1,2 @@
1
+ 0.1.3
2
+ - Log via debug instead of info
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in batcher.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Krish Rajagopalan
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,60 @@
1
+ # Batchmaker
2
+
3
+ The Batchmaker is a simply utility that using a separate thread and an atomic queue, easily allows to
4
+ store objects on the queue and perform a operation on a batch of the objects. The batch is
5
+ controlled by a maximum size and a tick period, ensuring that the batch have at most N objects or
6
+ that at most M time-unit have elapsed.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'batchmaker'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install batchmaker
23
+
24
+ ## Usage
25
+
26
+ The batchmaker class requires several arguments on initialization:
27
+ ```
28
+ batchmaker = Batchmaker.new(name, size, tick_period, on_error: nil, &block)
29
+ ```
30
+
31
+ `name` - The name of the queue (for emails, referencing the priority of the worker, e.g. critical, default, low)
32
+
33
+ `size` - Size of the queue
34
+
35
+ `tick_period` - The amount of time to wait before processing the queue
36
+
37
+ `on_error` - Optional error callback (Proc)
38
+
39
+ `&block` - Action to occur when the queue is processed (In this case, enqueuing the message data to the specified send email worker)
40
+
41
+ ```
42
+ BATCH_EXCEPTION_NOTIFIER = -> (err, ident_str) {
43
+ ExceptionNotification.log_and_notify(err, batchmaker_id: ident_str)
44
+ }
45
+
46
+ batchmaker = Batchmaker.new("email-default", 100, 20, on_error: BATCH_EXCEPTION_NOTIFIER) do |batch|
47
+ Emails::SendEmailDefaultWorker(batch.as_json)
48
+ end
49
+
50
+ # Add items to the batchmaker queue
51
+ batchmaker << 1
52
+ batchmaker << 2
53
+ ```
54
+
55
+ ## Development
56
+
57
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
58
+
59
+ To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the VERSION number in lib/batchmaker.rb, and then follow the instructions for releasing it as a private gem on Gemfury.
60
+
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'batchmaker/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "batchmaker"
8
+ spec.version = Batchmaker::VERSION
9
+ spec.authors = ["Rodrigo Kochenburger", "Krish Rajagopalan"]
10
+ spec.email = ["rodrigo@doximity.com", "krajagopalan@doximity.com"]
11
+
12
+ spec.summary = %q{Async queue system that batches items together based on time and size}
13
+ spec.description = %q{Async queue system that batches items together based on time and size}
14
+ spec.homepage = "https://github.com/doximity/batchmaker"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ #
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
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_development_dependency "bundler", "~> 1.13"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rspec", "~> 3.5.0"
36
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "batcher"
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
@@ -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,144 @@
1
+ require 'batchmaker/null_logger'
2
+
3
+ class Batchmaker
4
+ StoppedError = Class.new(StandardError)
5
+
6
+ def initialize(name, size, tick_period, on_error: nil, &block)
7
+ @name = name
8
+ @size = size
9
+ @queue = Queue.new
10
+ @mutex = Mutex.new
11
+ @count = 0
12
+ @action = block
13
+ @on_error = on_error
14
+ @stopping = false
15
+
16
+ # Main thread that process the batches
17
+ @thread = Thread.new(&method(:run))
18
+
19
+ # Thread that ticks on a specified frequency
20
+ @tick = Thread.new(tick_period, &method(:tick))
21
+ @tick.abort_on_exception = true
22
+ @tick.priority = 1
23
+ end
24
+
25
+ def running?
26
+ ["run", "sleep"].include?(@thread.status) && !@stopping
27
+ end
28
+
29
+ def <<(item)
30
+ if !running?
31
+ raise StoppedError, "failure to queue item, #{ident_str} has already stopped"
32
+ end
33
+
34
+ @mutex.synchronize do
35
+ @queue << [:add, item]
36
+ @count += 1
37
+
38
+ if @count >= @size
39
+ @count = 0
40
+ @queue << [:process, :size]
41
+ end
42
+ end
43
+
44
+ Thread.pass
45
+ end
46
+
47
+ def shutdown!
48
+ shutdown && wait
49
+ end
50
+
51
+ def shutdown
52
+ info "shutting down"
53
+
54
+ @mutex.synchronize do
55
+ @stopping = true
56
+ @queue << [:process]
57
+ @queue << [:stop]
58
+ end
59
+
60
+ @tick.exit
61
+ end
62
+
63
+ def wait
64
+ @thread.join
65
+ end
66
+
67
+ def self.logger=(logger)
68
+ @logger = logger
69
+ end
70
+
71
+ def self.logger
72
+ @logger
73
+ end
74
+
75
+ self.logger = NullLogger.new
76
+
77
+ private
78
+
79
+ def run
80
+ batch = []
81
+
82
+ info "starting batch processing loop"
83
+ loop do
84
+ action, *args = @queue.pop
85
+
86
+ case action
87
+ when :add
88
+ batch << args.first
89
+ debug "item added to #{@name} batch: #{args.first.inspect}"
90
+
91
+ when :process
92
+ unless batch.empty?
93
+ begin
94
+ @action.(batch.freeze)
95
+ debug "batch processed with #{batch.size} items"
96
+ rescue => e
97
+ error "batch with #{batch.size} failed to process due to '#{e.message}' - batch: #{batch.inspect}"
98
+ @on_error.call(e, ident_str) if @on_error
99
+ next
100
+ ensure
101
+ batch = []
102
+ end
103
+ end
104
+
105
+ when :stop
106
+ break
107
+ end
108
+ end
109
+
110
+ info "exiting batch processing loop"
111
+ end
112
+
113
+ def tick(period)
114
+ loop do
115
+ sleep(period)
116
+
117
+ @mutex.synchronize do
118
+ @queue << [:process, :tick]
119
+ end
120
+ end
121
+ end
122
+
123
+ def ident_str
124
+ "[#{self.class.name}(#{@name}):#{Process.pid}]"
125
+ end
126
+
127
+ def error(msg)
128
+ logger.error "#{ident_str} error: #{msg}"
129
+ end
130
+
131
+ def info(msg)
132
+ logger.info "#{ident_str} info: #{msg}"
133
+ end
134
+
135
+ def debug(msg)
136
+ logger.debug "#{ident_str} debug: #{msg}"
137
+ end
138
+
139
+ def logger
140
+ self.class.logger
141
+ end
142
+ end
143
+
144
+ require 'batchmaker/railtie' if defined?(Rails)
@@ -0,0 +1,12 @@
1
+ class Batchmaker
2
+ class NullLogger
3
+ def error(msg)
4
+ end
5
+
6
+ def info(msg)
7
+ end
8
+
9
+ def debug(msg)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ class Batchmaker
2
+ class Railtie < Rails::Railtie
3
+ initializer 'batcher.configure_logger' do
4
+ Batchmaker.logger = Rails.logger
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ class Batchmaker
2
+ VERSION = '1.0.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: batchmaker
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Rodrigo Kochenburger
8
+ - Krish Rajagopalan
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2018-01-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.13'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.13'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: 3.5.0
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: 3.5.0
56
+ description: Async queue system that batches items together based on time and size
57
+ email:
58
+ - rodrigo@doximity.com
59
+ - krajagopalan@doximity.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - CODE_OF_CONDUCT.md
66
+ - Changelog
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - batchmaker.gemspec
72
+ - bin/console
73
+ - bin/setup
74
+ - lib/batchmaker.rb
75
+ - lib/batchmaker/null_logger.rb
76
+ - lib/batchmaker/railtie.rb
77
+ - lib/batchmaker/version.rb
78
+ homepage: https://github.com/doximity/batchmaker
79
+ licenses: []
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.7.3
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Async queue system that batches items together based on time and size
101
+ test_files: []