Class: DBOperations::CleanStatsWorker

Inherits:
HasUtils::GoodJob::BaseWorker
  • Object
show all
Includes:
HasHelpers::DefaultWorker, HasHelpers::RedisConfigurable
Defined in:
app/workers/has_helpers/db_operations/clean_stats_worker.rb

Constant Summary collapse

AGE_LIMIT_STRING =
"1 month"

Instance Method Summary collapse

Methods included from HasHelpers::RedisConfigurable

included

Methods included from HasHelpers::DefaultWorker

included

Instance Method Details

#performObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/workers/has_helpers/db_operations/clean_stats_worker.rb', line 16

def perform
  ::Rails.logger.info "Removing db_operations_stats older than #{AGE_LIMIT_STRING}...."

  total_removed = 0

  loop do
    num_removed = ::HasHelpers::DBOperations::Operations.with_advisory_lock("'db_operations_stats'::regclass::integer") do |connection|
      connection.exec_delete <<-END_OF_SQL
        DELETE FROM db_operations_stats
        WHERE created_at <= now() - interval '#{AGE_LIMIT_STRING}'
      END_OF_SQL
    end

    break if num_removed == 0
    total_removed += num_removed
  end

  ::Rails.logger.info "Removed #{total_removed} operation stats."

  if config.db_operation_stats_resume_enabled
    HasHelpers::DBOperations::UpdateOperationsStatsResumeWorker.perform_later
  end
end