Fix race analytics: retry su unique violation in aggregazione heatmap.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-21 16:23:02 +02:00
co-authored by Cursor
parent 4573edfedc
commit 873e0ea55c
2 changed files with 41 additions and 16 deletions
+32 -15
View File
@@ -3,6 +3,7 @@
module Analytics module Analytics
class Aggregate class Aggregate
BATCH = 500 BATCH = 500
UPSERT_RETRIES = 3
def call def call
loop do loop do
@@ -45,13 +46,15 @@ module Analytics
now = Time.current now = Time.current
grouped.each do |(day, page_path, device, cell_x, cell_y), count| grouped.each do |(day, page_path, device, cell_x, cell_y), count|
cell = AnalyticsPageCell.find_or_initialize_by( with_unique_retry do
day: day, page_path: page_path, device: device, cell_x: cell_x, cell_y: cell_y cell = AnalyticsPageCell.find_or_initialize_by(
) day: day, page_path: page_path, device: device, cell_x: cell_x, cell_y: cell_y
cell[counter_attr] = cell[counter_attr].to_i + count )
cell.created_at ||= now cell[counter_attr] = cell[counter_attr].to_i + count
cell.updated_at = now cell.created_at ||= now
cell.save! cell.updated_at = now
cell.save!
end
end end
end end
@@ -76,14 +79,28 @@ module Analytics
now = Time.current now = Time.current
grouped.each do |(day, page_path, device), vals| grouped.each do |(day, page_path, device), vals|
stat = AnalyticsPageStat.find_or_initialize_by(day: day, page_path: page_path, device: device) with_unique_retry do
stat.pageview_count = stat.pageview_count.to_i + vals[:pageviews] stat = AnalyticsPageStat.find_or_initialize_by(day: day, page_path: page_path, device: device)
stat.scroll_samples = stat.scroll_samples.to_i + vals[:scroll_samples] stat.pageview_count = stat.pageview_count.to_i + vals[:pageviews]
stat.scroll_sum_pct = stat.scroll_sum_pct.to_i + vals[:scroll_sum] stat.scroll_samples = stat.scroll_samples.to_i + vals[:scroll_samples]
stat.max_scroll_pct = [stat.max_scroll_pct.to_i, vals[:max_scroll]].max stat.scroll_sum_pct = stat.scroll_sum_pct.to_i + vals[:scroll_sum]
stat.created_at ||= now stat.max_scroll_pct = [stat.max_scroll_pct.to_i, vals[:max_scroll]].max
stat.updated_at = now stat.created_at ||= now
stat.save! stat.updated_at = now
stat.save!
end
end
end
def with_unique_retry
attempts = 0
begin
attempts += 1
yield
rescue ActiveRecord::RecordNotUnique
raise if attempts >= UPSERT_RETRIES
retry
end end
end end
end end
+9 -1
View File
@@ -31,7 +31,15 @@ module Analytics
end end
AnalyticsEvent.insert_all(rows) if rows.any? AnalyticsEvent.insert_all(rows) if rows.any?
Analytics::Aggregate.new.call if rows.any? if rows.any?
begin
Analytics::Aggregate.new.call
rescue ActiveRecord::RecordNotUnique => e
# Dopo i retry interni: non far fallire la richiesta analytics.
Rails.logger.warn("[analytics] aggregate race after retries, enqueue job: #{e.message}")
Analytics::AggregateJob.perform_later
end
end
Result.new(accepted: accepted, rejected: rejected + (@events.size - slice.size), rate_limited: false) Result.new(accepted: accepted, rejected: rejected + (@events.size - slice.size), rate_limited: false)
end end