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
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddIndexToOrders < ActiveRecord::Migration[7.1]
disable_ddl_transaction!
def change
add_index :orders, :current_status, where: "current_status = 'pending'", algorithm: :concurrently
end
end
37 changes: 26 additions & 11 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 @@ -8,33 +8,48 @@ namespace :orders do
require 'benchmark'

Order.destroy_all
# Оптимизировать тут
Array.new(200) { Order.create!(product_id: rand(1..100), quantity: rand(1..10), current_status: :pending) }
data = Array.new(200) { { product_id: rand(1..100), quantity: rand(1..10), current_status: :pending } }
Order.import!(data)

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

batches = pending_orders.find_in_batches(batch_size: 50)
results = Parallel.flat_map(batches, in_threads: batches.size) do |batch|
process_batch_with_http(batch)
end

result = Order.import!(results, options)
result.ids.each { |order_id| puts "Successfully processed Order ##{order_id}" }
end

puts "Processed orders in #{time.round(2)} seconds with HTTP request"
end

def process_batch_with_http(batch)
batch.each do |order|
# Оптимизировать тут
Parallel.map(batch, in_threads: batch.size) do |order|
response = send_to_external_service
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ещё запросы можно через gem 'async' сделать - будет быстрее
и батч стоит сделать поменьше, так как потоки дорого создавать, 4 например

if response.code.to_i == 200
order.process!
puts "Successfully processed Order ##{order.id}"
{ id: order.id, current_status: :processed }
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 Orders #{e.message}")
end

def options
{
on_duplicate_key_update: {
conflict_target: [:id],
columns: [:current_status],
},
}
end

def pending_orders
@pending_orders ||= Order.pending
end

def send_to_external_service
Expand Down