<commit_before>require 'delayed_job'

module Delayed
  module Plugins
    class Raven < ::Delayed::Plugin
      callbacks do |lifecycle|
        lifecycle.around(:invoke_job) do |job, *args, &block|
          begin
            # Forward the call to the next callback in the callback chain
            block.call(job, *args)

          rescue Exception => exception
            # Log error to Sentry
            extra = {
              :delayed_job => {
                :id          => job.id,
                :priority    => job.priority,
                :attempts    => job.attempts,
                :run_at      => job.run_at,
                :locked_at   => job.locked_at,
                :locked_by   => job.locked_by,
                :queue       => job.queue,
                :created_at  => job.created_at
                }
            }
            # last_error can be nil
            extra[:last_error] = job.last_error[0...100] if job.last_error
            # handlers are YAML objects in strings, we definitely can't
            # report all of that or the event will get truncated randomly
            extra[:handler] = job.handler[0...100] if job.handler

            if job.respond_to?('payload_object') && job.payload_object.respond_to?('job_data')
              extra[:active_job] = job.payload_object.job_data
            end
            ::Raven.capture_exception(exception,
              :logger  => 'delayed_job',
              :tags    => {
                 :delayed_job_queue => job.queue,
                 :delayed_job_id => job.id
              },
              :extra => extra)

            # Make sure we propagate the failure!
            raise exception
          ensure
            Context.clear!
            BreadcrumbBuffer.clear!
          end
        end
      end
    end

  end
end

##
# Register DelayedJob Raven plugin
#
Delayed::Worker.plugins << Delayed::Plugins::Raven
<commit_msg>Fix: NameError when clearing context in DelayedJob<commit_after>require 'delayed_job'

module Delayed
  module Plugins
    class Raven < ::Delayed::Plugin
      callbacks do |lifecycle|
        lifecycle.around(:invoke_job) do |job, *args, &block|
          begin
            # Forward the call to the next callback in the callback chain
            block.call(job, *args)

          rescue Exception => exception
            # Log error to Sentry
            extra = {
              :delayed_job => {
                :id          => job.id,
                :priority    => job.priority,
                :attempts    => job.attempts,
                :run_at      => job.run_at,
                :locked_at   => job.locked_at,
                :locked_by   => job.locked_by,
                :queue       => job.queue,
                :created_at  => job.created_at
                }
            }
            # last_error can be nil
            extra[:last_error] = job.last_error[0...100] if job.last_error
            # handlers are YAML objects in strings, we definitely can't
            # report all of that or the event will get truncated randomly
            extra[:handler] = job.handler[0...100] if job.handler

            if job.respond_to?('payload_object') && job.payload_object.respond_to?('job_data')
              extra[:active_job] = job.payload_object.job_data
            end
            ::Raven.capture_exception(exception,
              :logger  => 'delayed_job',
              :tags    => {
                 :delayed_job_queue => job.queue,
                 :delayed_job_id => job.id
              },
              :extra => extra)

            # Make sure we propagate the failure!
            raise exception
          ensure
            ::Raven::Context.clear!
            ::Raven::BreadcrumbBuffer.clear!
          end
        end
      end
    end

  end
end

##
# Register DelayedJob Raven plugin
#
Delayed::Worker.plugins << Delayed::Plugins::Raven

{'fraction_non_alphanumeric': 0.09216944801026958, 'fraction_numerical': 0.004107830551989731, 'mean_word_length': 1.4799490770210058, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '806001', 'n_tokens_mistral': 1090, 'n_tokens_neox': 1030, 'n_words': 260}