Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions day_2/tn_rails_concurrent/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ group :development do
end

gem "parallel", "~> 1.26.3"
gem "async"
gem "sidekiq", "~> 7.3.6"

gem "activerecord-import", "~> 2.0"
15 changes: 15 additions & 0 deletions day_2/tn_rails_concurrent/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ GEM
annotate (3.2.0)
activerecord (>= 3.2, < 8.0)
rake (>= 10.4, < 14.0)
async (2.21.1)
console (~> 1.29)
fiber-annotation
io-event (~> 1.6, >= 1.6.5)
base64 (0.2.0)
benchmark (0.4.0)
bigdecimal (3.1.8)
Expand All @@ -92,13 +96,21 @@ GEM
builder (3.3.0)
concurrent-ruby (1.3.4)
connection_pool (2.4.1)
console (1.29.2)
fiber-annotation
fiber-local (~> 1.1)
json
crass (1.0.6)
date (3.4.1)
debug (1.9.2)
irb (~> 1.10)
reline (>= 0.3.8)
drb (2.2.1)
erubi (1.13.0)
fiber-annotation (0.2.0)
fiber-local (1.1.0)
fiber-storage
fiber-storage (1.0.0)
globalid (1.2.1)
activesupport (>= 6.1)
i18n (1.14.6)
Expand All @@ -108,9 +120,11 @@ GEM
activesupport (>= 6.0.0)
railties (>= 6.0.0)
io-console (0.8.0)
io-event (1.7.5)
irb (1.14.2)
rdoc (>= 4.0.0)
reline (>= 0.4.2)
json (2.9.1)
logger (1.6.3)
loofah (2.23.1)
crass (~> 1.0.2)
Expand Down Expand Up @@ -235,6 +249,7 @@ PLATFORMS
DEPENDENCIES
activerecord-import (~> 2.0)
annotate
async
bootsnap
debug
importmap-rails
Expand Down
35 changes: 23 additions & 12 deletions day_2/tn_rails_concurrent/lib/tasks/task_to_be_optimized.rake
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ namespace :orders do

Order.destroy_all
# Оптимизировать тут
Array.new(200) { Order.create!(product_id: rand(1..100), quantity: rand(1..10), current_status: :pending) }
Parallel.map(Array.new(200)) do
{ product_id: rand(1..100), quantity: rand(1..10), current_status: :pending }
end.tap { Order.insert_all(_1) }

time = Benchmark.realtime do
puts Order.pending.count
# Оптимизировать тут
Order.pending.find_in_batches(batch_size: 50) do |batch|
process_batch_with_http(batch)
Async do
Order.pending.find_in_batches(batch_size: 50) do |batch|
Async { process_batch_with_http(batch) }
end
end
end

Expand All @@ -24,16 +28,18 @@ namespace :orders do

def process_batch_with_http(batch)
batch.each do |order|
# Оптимизировать тут
response = send_to_external_service
if response.code.to_i == 200
order.process!
puts "Successfully processed Order ##{order.id}"
else
raise "Failed to process Order ##{order.id}: #{response.body}"
Async do
# Оптимизировать тут
response = send_to_external_service
if response.code.to_i == 200
order.process!
puts "Successfully processed Order ##{order.id}"
else
raise "Failed to process Order ##{order.id}: #{response.body}"
end
rescue StandardError => e
Rails.logger.error("Error processing Order ##{order.id}: #{e.message}")
end
rescue StandardError => e
Rails.logger.error("Error processing Order ##{order.id}: #{e.message}")
end
end

Expand All @@ -47,3 +53,8 @@ namespace :orders do
http.request(request)
end
end

# Processed orders in 20.1 seconds with HTTP request (перед оптимизацией)
# Processed orders in 0.11 seconds with HTTP request (async при io bound)
# Processed orders in 0.2 seconds with HTTP request (thread при io bound)
# Processed orders in 0.24 seconds with HTTP request (parallel (для параллельного запуска обработки каждого заказа в батче) + async (для обработки каждого заказа) при io bound)