Skip to content

Instantly share code, notes, and snippets.

@khun84
Last active August 20, 2022 06:31
Show Gist options
  • Save khun84/bd7460e0b90819244230544e83745c51 to your computer and use it in GitHub Desktop.
Save khun84/bd7460e0b90819244230544e83745c51 to your computer and use it in GitHub Desktop.
Ruby snippets
require 'rest-client'
require 'concurrent-ruby'
require 'logger'
require 'csv'
require 'active_support/all'
MyLogger = Logger.new('tmp/run.log')
datas = CSV.open('tmp/datas.csv', 'r', headers: true).map(&:to_h)
datas.each_with_index do |row, idx|
puts "#{idx + 1} of #{datas.size}"
begin
puts "Making request"
res = RestClient.post(
"http://localhost:3000/api/v1/customers/#{row['customer_id']}/settlement_transactions",
row.slice('idempotency_key', 'amount', 'scheduled_at')
)
rescue StandardError => e
MyLogger.error(e.message)
end
end
pool = Concurrent::FixedThreadPool.new(10)
counter = Concurrent::AtomicFixnum.new
error_counter = Concurrent::AtomicFixnum.new
trap("SIGTERM") do
pool.shutdown
pool.wait_for_termination
end
# datas.each do |row|
# pool.post do
# begin
# res = RestClient.post(
# "http://localhost:3000/api/v1/customers/#{row['customer_id']}/settlement_transactions",
# row.slice('idempotency_key', 'amount', 'scheduled_at')
# )
# rescue StandardError => e
# MyLogger.error(e.message)
# error_counter.increment
# ensure
# counter.increment
# end
# end
# end
while counter.value < datas.size
sleep 1
end
puts "Job is completed. #{counter.value}"
# return keys that exists in LHS but no in RHS
def diff_hash(lhs, rhs)
return rhs if lhs.keys.nil?
return lhs if rhs.keys.nil?
diff = []
queue = lhs.keys.map{|k| Array(k)}
while queue.length > 0
keys = queue.shift
if has_key?(rhs, keys)
child_keys = get_child_keys(lhs, keys)
queue.push(*child_keys.map { |k| keys + [k] }) unless child_keys.empty?
else
diff.push(keys)
end
end
diff.map{|k| k.join('.')}
end
def has_key?(h, keys)
dup_h = h
found = true
keys.each do |k|
break unless found
if dup_h.respond_to?(:key?) && dup_h.key?(k)
dup_h = dup_h[k]
else
found = false
end
end
found
end
def get_child_keys(h, keys)
child = h.dig(*keys)
!child.nil? && child.respond_to?(:keys) ? child.keys : []
end
- concurrent thread pool
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment