summaryrefslogtreecommitdiff
path: root/app/workers/prune_web_hook_logs_worker.rb
blob: 38054069f4e3edd9d0e4cc19a6bd045890bd3c5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# frozen_string_literal: true

# Worker that deletes a fixed number of outdated rows from the "web_hook_logs"
# table.
class PruneWebHookLogsWorker
  include ApplicationWorker
  include CronjobQueue

  # The maximum number of rows to remove in a single job.
  DELETE_LIMIT = 50_000

  # rubocop: disable CodeReuse/ActiveRecord
  def perform
    # MySQL doesn't allow "DELETE FROM ... WHERE id IN ( ... )" if the inner
    # query refers to the same table. To work around this we wrap the IN body in
    # another sub query.
    WebHookLog
      .where(
        'id IN (SELECT id FROM (?) ids_to_remove)',
        WebHookLog
          .select(:id)
          .where('created_at < ?', 90.days.ago.beginning_of_day)
          .limit(DELETE_LIMIT)
      )
      .delete_all
  end
  # rubocop: enable CodeReuse/ActiveRecord
end