Sha256: ac80f0b69e41237f57ee3b905db7fe933e7293081be54fece2113d491911764b

Contents?: true

Size: 969 Bytes

Versions: 3

Compression:

Stored size: 969 Bytes

Contents

module NulogyMessageBusConsumer
  module Tasks
    # Deletes any ProcessedMessage rows that are older than max_age
    #
    # interval_seconds: how many seconds between invocations
    # max_age: ActiveSupport::Duration of how old ProcessedMessage are
    # removed. This calls `#ago` on it.
    # For example, setting this to `1.week` will delete records more than
    # `1.week.ago` on each invocation.
    class PruneProcessedMessages
      attr_reader :interval

      def initialize(logger, interval_seconds, max_age)
        @logger = logger
        @interval = interval_seconds
        @max_age = max_age
      end

      def extract_args(**_)
      end

      def call
        deleted = prune_stale

        @logger.info("Pruned #{deleted} processed messages")
      end

      private

      def prune_stale
        ProcessedMessage
          .where(ProcessedMessage.arel_table[:created_at].lt(@max_age.ago))
          .delete_all
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nulogy_message_bus_consumer-2.0.1 lib/nulogy_message_bus_consumer/tasks/prune_processed_messages.rb
nulogy_message_bus_consumer-2.0.0 lib/nulogy_message_bus_consumer/tasks/prune_processed_messages.rb
nulogy_message_bus_consumer-1.0.0 lib/nulogy_message_bus_consumer/tasks/prune_processed_messages.rb