parallel_batch 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in parallel_batch.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Alexis Bernard
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,9 @@
1
+ ParallelBatch runs concurrent batches on tables with huge amount of rows.
2
+
3
+ To Contribute:
4
+
5
+ 1. Fork it
6
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
7
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
8
+ 4. Push to the branch (`git push origin my-new-feature`)
9
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,72 @@
1
+ # encoding: utf-8
2
+
3
+ class ParallelBatch < ActiveRecord::Base
4
+
5
+ #################
6
+ ### Constants ###
7
+ #################
8
+
9
+ VERSION = "0.0.1"
10
+
11
+ #####################
12
+ ### Class methods ###
13
+ #####################
14
+
15
+ def self.find_or_create!
16
+ first || create!(offset: 0)
17
+ # When starting many batches at the same time we are pretty sure to get a MySQL
18
+ # error reporting a duplicated entry. That's why we are retrying one time only.
19
+ rescue ActiveRecord::StatementInvalid
20
+ first || create!(offset: 0)
21
+ end
22
+
23
+ def self.start(concurrency = 1)
24
+ concurrency.times { fork { start_fork } }
25
+ end
26
+
27
+ def self.start_fork
28
+ puts "#{self} has started with pid #{Process.pid}"
29
+ ActiveRecord::Base.connection.reconnect!
30
+ find_or_create!.run
31
+ end
32
+
33
+ def self.reset
34
+ find_or_create!.update_attributes!(offset: null)
35
+ end
36
+
37
+ ########################
38
+ ### Instance methods ###
39
+ ########################
40
+
41
+ def find_records
42
+ offset ? scope.where('id > ?', offset).order(:id).limit(batch_size) : scope.order(:id).limit(batch_size)
43
+ end
44
+
45
+ def next_batch
46
+ transaction do
47
+ reload(lock: true)
48
+ next unless (records = find_records).last
49
+ update_attributes!(offset: records.last.id)
50
+ records
51
+ end
52
+ end
53
+
54
+ def run
55
+ while records = next_batch
56
+ records.each { |record| perform(record) }
57
+ end
58
+ end
59
+
60
+ def perfom(record)
61
+ raise NotImplementedError, 'You must override this method to perform your batch.'
62
+ end
63
+
64
+ def scope
65
+ raise NotImplementedError, 'You must override this method to scope your records.'
66
+ end
67
+
68
+ def batch_size
69
+ 1000
70
+ end
71
+
72
+ end
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path('../lib/parallel_batch/version', __FILE__)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.authors = ["Alexis Bernard"]
7
+ gem.email = ["alexis@official.com"]
8
+ gem.description = "Run safely concurent batches"
9
+ gem.summary = "Run safely concurent batches"
10
+ gem.homepage = "https://github.com/officialfm/parallel_batch"
11
+
12
+ gem.files = `git ls-files`.split($\)
13
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
+ gem.name = "parallel_batch"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = ParallelBatch::VERSION.dup
18
+ end
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'test_helper'
4
+
5
+ class ParallelBatchTest < ActiveSupport::TestCase
6
+ # Sample batch for testing purposes
7
+ class SampleBatch < ParallelBatch
8
+ def perform(record)
9
+ record.name.upcase! and record.save!
10
+ end
11
+ end
12
+
13
+ def test_find_or_create_when_batch_does_not_exist
14
+ ParallelBatch.expects(:first => nil)
15
+ ParallelBatch.expects(:create! => batch = ParallelBatch.new)
16
+ assert_equal(batch, ParallelBatch.find_or_create!)
17
+ end
18
+
19
+ def test_find_or_create_when_batch_already_present
20
+ ParallelBatch.expects(:first => batch = ParallelBatch.new)
21
+ ParallelBatch.expects(:create!).never
22
+ assert_equal(batch, ParallelBatch.find_or_create!)
23
+ end
24
+
25
+ def test_find_records
26
+ (scope = mock).expects(:where).with('id > ?', 2000).returns(where = mock)
27
+ where.expects(:order).with(:id).returns(order = mock)
28
+ order.expects(:limit).with(1000).returns([1, 2, 3])
29
+ batch = ParallelBatch.new(offset: 2000)
30
+ batch.expects(scope: scope)
31
+ assert_equal([1, 2, 3], batch.find_records)
32
+ end
33
+
34
+ def test_next_batch
35
+ batch = SampleBatch.new
36
+ batch.expects(:reload).with(:lock => true)
37
+ batch.expects(:find_records => records = [stub(:id => 123)])
38
+ batch.expects(:update_attributes!).with(:offset => 123)
39
+ assert_equal(records, batch.next_batch)
40
+ end
41
+
42
+ def test_run
43
+ batch = SampleBatch.new
44
+ batch.expects(:perform).with(u = User.new)
45
+ batch.expects(:next_batch => [u]).in_sequence(run = sequence('run'))
46
+ batch.expects(:next_batch => nil).in_sequence(run)
47
+ batch.run
48
+ end
49
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: parallel_batch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alexis Bernard
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-17 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Run safely concurent batches
15
+ email:
16
+ - alexis@official.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README
25
+ - Rakefile
26
+ - lib/parallel_batch.rb
27
+ - parallel_batch.gemspec
28
+ - test/parallel_batch_test.rb
29
+ homepage: https://github.com/officialfm/parallel_batch
30
+ licenses: []
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 1.8.10
50
+ signing_key:
51
+ specification_version: 3
52
+ summary: Run safely concurent batches
53
+ test_files:
54
+ - test/parallel_batch_test.rb