<commit_before># frozen_string_literal: true

# The class that most jobs inherit from.

module Que
  class Job
    attr_reader :que_attrs, :que_error

    def initialize(attrs)
      @que_attrs = attrs
    end

    # Subclasses should define their own run methods, but keep an empty one
    # here so that Que::Job.enqueue can queue an empty job in testing.
    def run(*args)
    end

    def _run
      run(*que_attrs.fetch(:args))
    rescue => error
      @que_error = error
      run_error_notifier = handle_error(error)

      if run_error_notifier && Que.error_notifier
        # Protect the work loop from a failure of the error notifier.
        Que.error_notifier.call(error, que_attrs) rescue nil
      end
    ensure
      finish unless @que_retried || @que_finished || @que_destroyed
    end

    private

    def error_count
      que_attrs.fetch(:error_count)
    end

    def handle_error(error)
      error_count    = que_attrs[:error_count] += 1
      retry_interval = self.class.retry_interval || Job.retry_interval

      wait =
        if retry_interval.respond_to?(:call)
          retry_interval.call(error_count)
        else
          retry_interval
        end

      retry_in(wait)
    end

    def retry_in(period)
      Que.execute :set_error, [period, que_error.message, que_attrs.fetch(:id)]
      @retried = true
    end

    def finish
      Que.execute :finish_job, [que_attrs.fetch(:id)]
      @finished = true
    end

    def destroy
      Que.execute :destroy_job, [que_attrs.fetch(:id)]
      @destroyed = true
    end

    @retry_interval = proc { |count| count ** 4 + 3 }

    class << self
      attr_reader :retry_interval

      def enqueue(*args, job_class: nil, run_at: nil, priority: nil, **arg_opts)
        args << arg_opts if arg_opts.any?

        attrs = {
          job_class: job_class || to_s,
          args: args,
        }<commit_msg>Simplify how jobs are marked as resolved.<commit_after># frozen_string_literal: true

# The class that most jobs inherit from.

module Que
  class Job
    attr_reader :que_attrs, :que_error

    def initialize(attrs)
      @que_attrs = attrs
    end

    # Subclasses should define their own run methods, but keep an empty one
    # here so that Que::Job.enqueue can queue an empty job in testing.
    def run(*args)
    end

    def _run
      run(*que_attrs.fetch(:args))
    rescue => error
      @que_error = error
      run_error_notifier = handle_error(error)

      if run_error_notifier && Que.error_notifier
        # Protect the work loop from a failure of the error notifier.
        Que.error_notifier.call(error, que_attrs) rescue nil
      end
    ensure
      finish unless @que_resolved
    end

    private

    def error_count
      que_attrs.fetch(:error_count)
    end

    def handle_error(error)
      error_count    = que_attrs[:error_count] += 1
      retry_interval = self.class.retry_interval || Job.retry_interval

      wait =
        if retry_interval.respond_to?(:call)
          retry_interval.call(error_count)
        else
          retry_interval
        end

      retry_in(wait)
    end

    def retry_in(period)
      Que.execute :set_error, [period, que_error.message, que_attrs.fetch(:id)]
      @que_resolved = true
    end

    def finish
      Que.execute :finish_job, [que_attrs.fetch(:id)]
      @que_resolved = true
    end

    def destroy
      Que.execute :destroy_job, [que_attrs.fetch(:id)]
      @que_resolved = true
    end

    @retry_interval = proc { |count| count ** 4 + 3 }

    class << self
      attr_reader :retry_interval

      def enqueue(*args, job_class: nil, run_at: nil, priority: nil, **arg_opts)
        args << arg_opts if arg_opts.any?

        attrs = {
          job_class: job_class || to_s,
          args: args,
        }
{'fraction_non_alphanumeric': 0.08258575197889181, 'fraction_numerical': 0.0015831134564643799, 'mean_word_length': 2.4091726618705036, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, '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': '4389506', 'n_tokens_mistral': 1189, 'n_tokens_neox': 1165, 'n_words': 324}