Sha256: c5b481a3515907ebb4f0ed38f4105b1c35b91ea079955fda623f64a1c03c072a

Contents?: true

Size: 1.82 KB

Versions: 24

Compression:

Stored size: 1.82 KB

Contents

require "active_support/concern"

module RocketJob
  module Batch
    module Logger
      extend ActiveSupport::Concern

      included do
        # Log all state transitions
        after_start :rocket_job_batch_log_state_change
        after_complete :rocket_job_batch_log_state_change
        after_fail :rocket_job_batch_log_state_change
        after_retry :rocket_job_batch_log_state_change
        after_pause :rocket_job_batch_log_state_change
        after_resume :rocket_job_batch_log_state_change
        after_abort :rocket_job_batch_log_state_change
        after_requeue :rocket_job_batch_log_state_change

        around_slice :rocket_job_batch_slice_logger

        # Remove perform level logger and replace with slice level logger
        skip_callback(:perform, :around, :rocket_job_around_logger)
      end

      private

      # Add logging around processing of each slice
      #   - metric allows duration to be forwarded to statsd, etc.
      #   - log_exception logs entire exception if raised
      #   - on_exception_level changes log level from info to error on exception
      #   - silence noisy jobs by raising log level
      def rocket_job_batch_slice_logger(&block)
        logger.measure_info(
          "Completed slice",
          metric:             "#{self.class.name}/slice",
          log_exception:      :full,
          on_exception_level: :error,
          silence:            log_level,
          payload:            {records: rocket_job_slice&.size},
          &block
        )
      end

      def rocket_job_batch_log_state_change
        logger.info(aasm.current_event.to_s.camelcase, rocket_job_batch_log_payload)
      end

      def rocket_job_batch_log_payload
        {
          from:  aasm.from_state,
          to:    aasm.to_state,
          event: aasm.current_event
        }
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
rocketjob-5.2.0 lib/rocket_job/batch/logger.rb
rocketjob-5.2.0.beta3 lib/rocket_job/batch/logger.rb
rocketjob-5.2.0.beta2 lib/rocket_job/batch/logger.rb
rocketjob-5.2.0.beta1 lib/rocket_job/batch/logger.rb