Skip to content

Commit 369db46

Browse files
committed
Refector StatusHistoryRescalerJob transactions to prevent deadlocks
The StatusHistoryRescalerJob was wrapping multiple cleanup calls into a single large database transaction. This held locks on the status_histories table for the entire duration of the job.
1 parent 37f797c commit 369db46

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/api/app/jobs/status_history_rescaler_job.rb

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,21 @@ class StatusHistoryRescalerJob < ApplicationJob
44
# this is called from a delayed job triggered by clockwork
55
def perform
66
maxtime = StatusHistory.maximum(:time)
7-
StatusHistory.where(time: ...(maxtime - (365 * 24 * 3600))).delete_all if maxtime
7+
StatusHistory.where(time: ...(maxtime - (365 * 24 * 3600))).find_in_batches do |batch|
8+
ids = batch.map(&:id)
9+
StatusHistory.where(id: ids).delete_all
10+
end if maxtime
811

912
keys = StatusHistory.distinct.pluck(:key)
1013
keys.each do |key|
11-
StatusHistory.transaction do
12-
# first rescale a month old
13-
cleanup(key, 3600 * 12, 24 * 3600 * 30)
14-
# now a week old
15-
cleanup(key, 3600 * 6, 24 * 3600 * 7)
16-
# now rescale yesterday
17-
cleanup(key, 3600, 24 * 3600)
18-
# 2h stuff
19-
cleanup(key, 200, 3600 * 2)
20-
end
14+
# first rescale a month old
15+
cleanup(key, 3600 * 12, 24 * 3600 * 30)
16+
# now a week old
17+
cleanup(key, 3600 * 6, 24 * 3600 * 7)
18+
# now rescale yesterday
19+
cleanup(key, 3600, 24 * 3600)
20+
# 2h stuff
21+
cleanup(key, 200, 3600 * 2)
2122
end
2223
end
2324

0 commit comments

Comments
 (0)