Skip to content

armgit / maybe

Public Imported from GitHub
0

The personal finance app for everyone

Batch upsert security prices on sync

Zach Gollwitzer7605b0221d83
diff --git a/app/models/security/provided.rb b/app/models/security/provided.rb
index b342c9e5..df81094b 100644
--- a/app/models/security/provided.rb
+++ b/app/models/security/provided.rb
@@ -63,7 +63,21 @@ module Security::Provided
is_invalid
end
- Security::Price.upsert_all(valid_prices, unique_by: %i[security_id date currency])
+ valid_prices.each_slice(100) do |batch|
+ retries ||= 0
+
+ begin
+ Security::Price.upsert_all(batch, unique_by: %i[security_id date currency])
+ rescue => e
+ if retries < 3
+ retries += 1
+ sleep(1)
+ retry
+ else
+ raise e
+ end
+ end
+ end
end
def find_or_fetch_price(date: Date.current, cache: true)