<commit_before>module ActiveJob
  class Chain
    def initialize(*args)
      @jobs = args.map do |job|
        if job.is_a? Array
          job
        else
          [job, nil]
        end
      end
      self
    end

    def then(success, failure = nil)
      @jobs << [success, failure]
      self
    end

    def catch(failure)
      @jobs << [nil, failure]
      self
    end

    def enqueue(options = {})
      return self if jobs.nil?

      first_job, fail_job = jobs.first
      first_job.catch(fail_job) if fail_job

      prev_job = first_job
      jobs[1..-1].each do |success, failure|
        next if success.nil?
        prev_job = prev_job.then(success, failure)
      end

      first_job.enqueue(options)

      self
    end

    def perform_now
      jobs.each do |success_job, fail_job|
        begin
          success_job.perform_now
        rescue
          fail_job.perform_now
          break
        end
      end

      self
    end

    def jobs
      fjob = nil
      @jobs.reverse.map do |job|
        fjob = job.last if job.last
        [job.first, fjob]
      end.reverse
    end
  end
end
<commit_msg>check that we have jobs to chain before chaining them<commit_after>module ActiveJob
  class Chain
    def initialize(*args)
      @jobs = args.map do |job|
        if job.is_a? Array
          job
        else
          [job, nil]
        end
      end
      self
    end

    def then(success, failure = nil)
      @jobs << [success, failure]
      self
    end

    def catch(failure)
      @jobs << [nil, failure]
      self
    end

    def enqueue(options = {})
      return self if jobs.blank?

      first_job, fail_job = jobs.first
      first_job.catch(fail_job) if fail_job

      if jobs.size > 1
        prev_job = first_job
        jobs[1..-1].each do |success, failure|
          next if success.nil?
          prev_job = prev_job.then(success, failure)
        end
      end

      first_job.enqueue(options)

      self
    end

    def perform_now
      jobs.each do |success_job, fail_job|
        begin
          success_job.perform_now
        rescue
          fail_job.perform_now
          break
        end
      end

      self
    end

    def jobs
      fjob = nil
      @jobs.reverse.map do |job|
        fjob = job.last if job.last
        [job.first, fjob]
      end.reverse
    end
  end
end

{'fraction_non_alphanumeric': 0.07460788469690546, 'fraction_numerical': 0.00211954217888936, 'mean_word_length': 1.5321888412017168, 'pattern_counts': {'":': 0, '<': 11, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '202403', 'n_tokens_mistral': 757, 'n_tokens_neox': 731, 'n_words': 207}