Skip to content

Commit e78b8c9

Browse files
bharathpbhattrungpham
authored andcommitted
parallelize batch requests
1 parent d44a371 commit e78b8c9

18 files changed

Lines changed: 139 additions & 40 deletions

.ruby-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
ruby-1.9.3-p448
1+
ruby-2.2.0

.travis.yml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
11
language: ruby
22
rvm:
3-
- 1.9.3
4-
- rbx-19mode
5-
- jruby-19mode
6-
matrix:
7-
allow_failures:
8-
- rvm: jruby-19mode
3+
- 2.2.0

Gemfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ group :development, :test do
1212
gem 'guard-rspec'
1313
gem 'faker'
1414
gem 'timecop'
15-
gem "debugger", platform: :mri
15+
gem 'pry'
16+
gem 'test-unit'
1617

17-
if RUBY_PLATFORM =~ /darwin/
18+
group :darwin do
1819
# OS X integration
1920
gem "ruby_gntp"
2021
gem "rb-fsevent"

Gemfile.lock

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ PATH
22
remote: .
33
specs:
44
batch_api (0.2.1)
5+
celluloid
56
middleware
67

78
GEM
@@ -36,14 +37,9 @@ GEM
3637
multi_json (~> 1.0)
3738
arel (3.0.2)
3839
builder (3.0.4)
40+
celluloid (0.16.0)
41+
timers (~> 4.0.0)
3942
coderay (1.0.8)
40-
columnize (0.3.6)
41-
debugger (1.6.1)
42-
columnize (>= 0.3.1)
43-
debugger-linecache (~> 1.2.0)
44-
debugger-ruby_core_source (~> 1.2.3)
45-
debugger-linecache (1.2.0)
46-
debugger-ruby_core_source (1.2.3)
4743
diff-lcs (1.1.3)
4844
erubis (2.7.0)
4945
faker (1.1.2)
@@ -57,9 +53,10 @@ GEM
5753
guard (>= 1.1)
5854
rspec (~> 2.11)
5955
hike (1.2.1)
56+
hitimes (1.2.2)
6057
i18n (0.6.1)
6158
journey (1.0.4)
62-
json (1.7.5)
59+
json (1.8.2)
6360
listen (0.5.3)
6461
lumberjack (1.0.2)
6562
mail (2.4.4)
@@ -71,6 +68,7 @@ GEM
7168
mime-types (1.19)
7269
multi_json (1.3.6)
7370
polyglot (0.3.3)
71+
power_assert (0.2.2)
7472
pry (0.9.10)
7573
coderay (~> 1.0.5)
7674
method_source (~> 0.8)
@@ -128,10 +126,14 @@ GEM
128126
hike (~> 1.2)
129127
rack (~> 1.0)
130128
tilt (~> 1.1, != 1.3.0)
131-
sqlite3 (1.3.6)
129+
sqlite3 (1.3.10)
130+
test-unit (3.0.9)
131+
power_assert
132132
thor (0.16.0)
133133
tilt (1.3.3)
134134
timecop (0.5.3)
135+
timers (4.0.1)
136+
hitimes
135137
treetop (1.4.11)
136138
polyglot
137139
polyglot (>= 0.3.1)
@@ -142,10 +144,10 @@ PLATFORMS
142144

143145
DEPENDENCIES
144146
batch_api!
145-
debugger
146147
faker
147148
guard
148149
guard-rspec
150+
pry
149151
rack-contrib
150152
rails (~> 3.2)
151153
rb-fsevent
@@ -154,4 +156,5 @@ DEPENDENCIES
154156
ruby_gntp
155157
sinatra
156158
sqlite3
159+
test-unit
157160
timecop

batch_api.gemspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ Gem::Specification.new do |s|
1818
s.test_files = Dir["spec/**/*"]
1919

2020
s.add_runtime_dependency("middleware")
21-
21+
s.add_runtime_dependency("celluloid")
22+
2223
s.add_development_dependency("rails", "~> 3.2")
2324
s.add_development_dependency("sinatra")
2425
s.add_development_dependency("rspec")

lib/batch_api/configuration.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module BatchApi
1919
verb: :post,
2020
endpoint: "/batch",
2121
limit: 50,
22+
parallel_size: 10,
2223
batch_middleware: InternalMiddleware::DEFAULT_BATCH_MIDDLEWARE,
2324
operation_middleware: InternalMiddleware::DEFAULT_OPERATION_MIDDLEWARE
2425
}

lib/batch_api/internal_middleware.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'middleware'
22
require 'batch_api/processor/sequential'
3+
require 'batch_api/processor/parallel'
34
require 'batch_api/processor/executor'
45
require 'batch_api/internal_middleware/decode_json_body'
56
require 'batch_api/internal_middleware/response_filter'
@@ -68,7 +69,6 @@ def self.batch_stack(processor)
6869
Middleware::Builder.new do
6970
# evaluate these in the context of the middleware stack
7071
self.instance_eval &BatchApi.config.batch_middleware
71-
# for now, everything's sequential, but that will change
7272
use processor.strategy
7373
end
7474
end

lib/batch_api/internal_middleware/decode_json_body.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def initialize(app)
1010

1111
def call(env)
1212
@app.call(env).tap do |result|
13-
result.body = MultiJson.load(result.body) if should_decode?(result)
13+
result.body = MultiJson.load(result.body) if (should_decode?(result) && !result.body.blank?)
1414
end
1515
end
1616

lib/batch_api/parallel_actor.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require 'celluloid'
2+
3+
module BatchApi
4+
class ParallelActor
5+
include ::Celluloid
6+
7+
def run(env)
8+
middleware = InternalMiddleware.operation_stack
9+
10+
if defined?(ActiveRecord)
11+
ActiveRecord::Base.connection_pool.with_connection do
12+
middleware.call(env).tap {|r| env.delete(:op) }
13+
end
14+
else
15+
middleware.call(env).tap {|r| env.delete(:op) }
16+
end
17+
end
18+
end
19+
end
20+
21+

lib/batch_api/processor.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def initialize(request, app)
2828
# provided in BatchApi setup and the request.
2929
# Currently only Sequential is supported.
3030
def strategy
31-
BatchApi::Processor::Sequential
31+
@request.params["sequential"] ? BatchApi::Processor::Sequential : BatchApi::Processor::Parallel
3232
end
3333

3434
# Public: run the batch operations according to the appropriate strategy.
@@ -95,18 +95,11 @@ def self.operation_klass
9595
end
9696

9797
# Internal: Processes any other provided options for validity.
98-
# Currently, the :sequential option is REQUIRED (until parallel
99-
# implementation is created).
10098
#
10199
# options - an options hash
102100
#
103-
# Raises Errors::BadOptionError if sequential is not provided.
104-
#
105101
# Returns the valid options hash.
106102
def process_options
107-
unless @request.params["sequential"]
108-
raise Errors::BadOptionError, "Sequential flag is currently required"
109-
end
110103
@request.params
111104
end
112105
end

0 commit comments

Comments
 (0)