<commit_before>require 'metric_collector'
require 'processor'

class ProcessingJob < ActiveJob::Base
  queue_as :default

  before_perform do |job|
    @context = Processor::Context.new
  end

  def perform(repository, processing)
    @context.repository = repository
    @context.processing = processing

    begin
      Processor::Preparer.perform(@context)
      Processor::Downloader.perform(@context)
      Processor::Collector.perform(@context)
      Processor::TreeBuilder.perform(@context)
      Processor::Aggregator.perform(@context)
      Processor::CompoundResultCalculator.perform(@context)
      Processor::Interpreter.perform(@context)

      @context.processing.update(state: "READY")
    rescue Errors::ProcessingCanceledError
      @context.processing.destroy
    rescue Errors::ProcessingError => error
      @context.processing.update(state: 'ERROR', error_message: error.message)
    rescue Errors::EmptyModuleResultsError
      self.processing.update(state: "READY")
    end
  end
end<commit_msg>Using ActiveJob native error handling<commit_after>require 'metric_collector'
require 'processor'

class ProcessingJob < ActiveJob::Base
  queue_as :default

  before_perform do |job|
    @context = Processor::Context.new
  end

  rescue_from(Errors::ProcessingCanceledError) do
    @context.processing.destroy
  end

  rescue_from(Errors::ProcessingError) do |exception|
    @context.processing.update(state: 'ERROR', error_message: exception.message)
  end

  def perform(repository, processing)
    @context.repository = repository
    @context.processing = processing

    Processor::Preparer.perform(@context)
    Processor::Downloader.perform(@context)
    Processor::Collector.perform(@context)
    Processor::TreeBuilder.perform(@context)
    Processor::Aggregator.perform(@context)
    Processor::CompoundResultCalculator.perform(@context)
    Processor::Interpreter.perform(@context)

    @context.processing.update(state: "READY")
    rescue Errors::EmptyModuleResultsError
      self.processing.update(state: "READY")
  end
end
{'fraction_non_alphanumeric': 0.10500729217306758, 'fraction_numerical': 0.0, 'mean_word_length': 4.913793103448276, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6057934', 'n_tokens_mistral': 575, 'n_tokens_neox': 556, 'n_words': 101}