<commit_before>require 'active_record'
require 'hair_trigger'

class ActiveRecord::Base
  def self.load_for_delayed_job(id)
    if id
      find(id)
    else
      super
    end
  end
end

module Delayed
  module Backend
    module ActiveRecord
      # A job object that is persisted to the database.
      # Contains the work object as a YAML field.
      class Job < ::ActiveRecord::Base
        include Delayed::Backend::Base
        set_table_name :delayed_jobs
        attr_writer :current_shard

        def current_shard
          @current_shard || Shard.default
        end

        # be aware that some strand functionality is controlled by triggers on
        # the database. see
        # db/migrate/20110831210257_add_delayed_jobs_next_in_strand.rb
        #
        # next_in_strand defaults to true. if we insert a new job, and it has a
        # strand, and it's not the next in the strand, we set it to false.
        #
        # if we delete a job, and it has a strand, mark the next job in that
        # strand to be next_in_strand
        # (this is safe even if we're not deleting the job that was currently
        # next_in_strand)

        # special mysql support, since its triggers don't support modifying the
        # underlying table from within the trigger.
        # this means that deleting the first job from a strand from
        # outside rails is *not* safe when using mysql for the queue.
        after_destroy :update_strand_on_destroy
        cattr_accessor :adapter_name
        def update_strand_on_destroy
          if adapter_name.nil?
            self.class.adapter_name = connection.adapter_name
          end
          if strand.present? && next_in_strand? && adapter_name == 'MySQL'
            # this funky sub-sub-select is to force mysql to create a temporary
            # table, otherwise it fails complaining that you can't select from
            # the same table you are updating
            connection.execute(sanitize_sql(["UPDATE delayed_jobs SET next_in_strand = 1 WHERE id = (SELECT _id.id FROM (SELECT id FROM delayed_jobs j2 WHERE j2.strand = ? ORDER BY j2.strand, j2.id ASC LIMIT 1) AS _id)", strand]))
          end
        end

        # postgresql needs this lock to be taken before the before_insert
        # trigger starts, or we risk deadlock inside of the trigger when trying
        # to raise the lock level
        before_create :lock_strand_on_create
        def lock_strand_on_create
          if adapter_name.nil?
            self.class.adapter_name = connection.adapter_name
          end
          if strand.present? && adapter_name == 'PostgreSQL'
            connection.execute("LOCK delayed_jobs IN SHARE ROW EXCLUSIVE MODE")
          end
        end

        cattr_accessor :default_priority
        self.default_priority = Delayed::NORMAL_PRIORITY

        named_scope :current, lambda {
          { :conditions => ["run_at <= ?", db_time_now] }
        }

        named_scope :future, lambda {
          { :conditions => ["run_at > ?", db_time_now] }
        }

        named_scope :failed, :conditions => ["failed_at IS NOT NULL"]

        named_scope :running, :conditions => ["locked_at is NOT NULL AND locked_by <> 'on hold'"]

        # a nice stress test:
        # 10_000.times { |i| Kernel.send_later_enqueue_args(:system, { :strand => 's1', :run_at => (24.hours.ago + (rand(24.hours.to_i))) }, "echo #{i} >> test1.txt") }
        # 500.times { |i| "ohai".send_later_enqueue_args(:reverse, { :run_at => (12.hours.ago + (rand(24.hours.to_i))) }) }
        # then fire up your workers
        # you can check out strand correctness: diff test1.txt <(sort -n test1.txt)
        named_scope :ready_to_run, lambda {|worker_name, max_run_time|
          { :conditions => ["run_at <= ? AND locked_at IS NULL AND next_in_strand = ?", db_time_now, true] }
        }
        named_scope :by_priority, :order => 'priority ASC, run_at ASC'

        # When a worker is exiting, make sure we don't have any locked jobs.
        def self.clear_locks!(worker_name)
          update_all("locked_by = null, locked_at = null", ["locked_by = ?", worker_name])
        end

        def self.unlock_expired_jobs(max_run_time = Delayed::Worker.max_run_time)
          update_all("locked_by = null, locked_at = null", ["locked_by <> 'on hold' AND locked_at < ?", db_time_now - max_run_time])
        end

        def self.get_and_lock_next_available(worker_name,
                                             max_run_time,
                                             queue = nil,
                                             min_priority = nil,
                                             max_priority = nil)

          min_priority ||= Delayed::MIN_PRIORITY
          max_priority ||= Delayed::MAX_PRIORITY

          if connection.adapter_name == 'PostgreSQL' && Setting.get_cached('delayed_jobs_db_fn_pop', 'true') == 'true'
            # note postgresql is optimized to use this db function, and doesn't
            # use find_available, lock_exclusively and friends
            #
            # because pop_from_delayed_jobs returns a delayed_jobs row, we have
            # to re-create the function every time we change the schema of
            # delayed_jobs. fortunately that doesn't happen often. see the
            # define_pg_pop_function method below, and the
            # PopJobsWithDbFunctionOnPostgresql migration.
            now = db_time_now
            job = self.find_by_sql(sanitize_sql_array(["select * from pop_from_delayed_jobs(?, ?, ?, ?, ?)", worker_name, queue, min_priority, max_priority, now])).first
            job = nil if !job.id
            return job
          else
            @batch_size ||= Setting.get_cached('jobs_get_next_batch_size', '5').to_i
            loop do
              jobs = find_available(worker_name, @batch_size, max_run_time, queue, min_priority, max_priority)
              return nil if jobs.empty?<commit_msg>temporarily default the pg jobs popping fn to false<commit_after>require 'active_record'
require 'hair_trigger'

class ActiveRecord::Base
  def self.load_for_delayed_job(id)
    if id
      find(id)
    else
      super
    end
  end
end

module Delayed
  module Backend
    module ActiveRecord
      # A job object that is persisted to the database.
      # Contains the work object as a YAML field.
      class Job < ::ActiveRecord::Base
        include Delayed::Backend::Base
        set_table_name :delayed_jobs
        attr_writer :current_shard

        def current_shard
          @current_shard || Shard.default
        end

        # be aware that some strand functionality is controlled by triggers on
        # the database. see
        # db/migrate/20110831210257_add_delayed_jobs_next_in_strand.rb
        #
        # next_in_strand defaults to true. if we insert a new job, and it has a
        # strand, and it's not the next in the strand, we set it to false.
        #
        # if we delete a job, and it has a strand, mark the next job in that
        # strand to be next_in_strand
        # (this is safe even if we're not deleting the job that was currently
        # next_in_strand)

        # special mysql support, since its triggers don't support modifying the
        # underlying table from within the trigger.
        # this means that deleting the first job from a strand from
        # outside rails is *not* safe when using mysql for the queue.
        after_destroy :update_strand_on_destroy
        cattr_accessor :adapter_name
        def update_strand_on_destroy
          if adapter_name.nil?
            self.class.adapter_name = connection.adapter_name
          end
          if strand.present? && next_in_strand? && adapter_name == 'MySQL'
            # this funky sub-sub-select is to force mysql to create a temporary
            # table, otherwise it fails complaining that you can't select from
            # the same table you are updating
            connection.execute(sanitize_sql(["UPDATE delayed_jobs SET next_in_strand = 1 WHERE id = (SELECT _id.id FROM (SELECT id FROM delayed_jobs j2 WHERE j2.strand = ? ORDER BY j2.strand, j2.id ASC LIMIT 1) AS _id)", strand]))
          end
        end

        # postgresql needs this lock to be taken before the before_insert
        # trigger starts, or we risk deadlock inside of the trigger when trying
        # to raise the lock level
        before_create :lock_strand_on_create
        def lock_strand_on_create
          if adapter_name.nil?
            self.class.adapter_name = connection.adapter_name
          end
          if strand.present? && adapter_name == 'PostgreSQL'
            connection.execute("LOCK delayed_jobs IN SHARE ROW EXCLUSIVE MODE")
          end
        end

        cattr_accessor :default_priority
        self.default_priority = Delayed::NORMAL_PRIORITY

        named_scope :current, lambda {
          { :conditions => ["run_at <= ?", db_time_now] }
        }

        named_scope :future, lambda {
          { :conditions => ["run_at > ?", db_time_now] }
        }

        named_scope :failed, :conditions => ["failed_at IS NOT NULL"]

        named_scope :running, :conditions => ["locked_at is NOT NULL AND locked_by <> 'on hold'"]

        # a nice stress test:
        # 10_000.times { |i| Kernel.send_later_enqueue_args(:system, { :strand => 's1', :run_at => (24.hours.ago + (rand(24.hours.to_i))) }, "echo #{i} >> test1.txt") }
        # 500.times { |i| "ohai".send_later_enqueue_args(:reverse, { :run_at => (12.hours.ago + (rand(24.hours.to_i))) }) }
        # then fire up your workers
        # you can check out strand correctness: diff test1.txt <(sort -n test1.txt)
        named_scope :ready_to_run, lambda {|worker_name, max_run_time|
          { :conditions => ["run_at <= ? AND locked_at IS NULL AND next_in_strand = ?", db_time_now, true] }
        }
        named_scope :by_priority, :order => 'priority ASC, run_at ASC'

        # When a worker is exiting, make sure we don't have any locked jobs.
        def self.clear_locks!(worker_name)
          update_all("locked_by = null, locked_at = null", ["locked_by = ?", worker_name])
        end

        def self.unlock_expired_jobs(max_run_time = Delayed::Worker.max_run_time)
          update_all("locked_by = null, locked_at = null", ["locked_by <> 'on hold' AND locked_at < ?", db_time_now - max_run_time])
        end

        def self.get_and_lock_next_available(worker_name,
                                             max_run_time,
                                             queue = nil,
                                             min_priority = nil,
                                             max_priority = nil)

          min_priority ||= Delayed::MIN_PRIORITY
          max_priority ||= Delayed::MAX_PRIORITY

          if connection.adapter_name == 'PostgreSQL' && Setting.get_cached('delayed_jobs_db_fn_pop', 'false') == 'true'
            # note postgresql is optimized to use this db function, and doesn't
            # use find_available, lock_exclusively and friends
            #
            # because pop_from_delayed_jobs returns a delayed_jobs row, we have
            # to re-create the function every time we change the schema of
            # delayed_jobs. fortunately that doesn't happen often. see the
            # define_pg_pop_function method below, and the
            # PopJobsWithDbFunctionOnPostgresql migration.
            now = db_time_now
            job = self.find_by_sql(sanitize_sql_array(["select * from pop_from_delayed_jobs(?, ?, ?, ?, ?)", worker_name, queue, min_priority, max_priority, now])).first
            job = nil if !job.id
            return job
          else
            @batch_size ||= Setting.get_cached('jobs_get_next_batch_size', '5').to_i
            loop do
              jobs = find_available(worker_name, @batch_size, max_run_time, queue, min_priority, max_priority)
              return nil if jobs.empty?
{'fraction_non_alphanumeric': 0.08018116245911264, 'fraction_numerical': 0.006877463725572423, 'mean_word_length': 2.2481612639607738, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5935568', 'n_tokens_mistral': 3542, 'n_tokens_neox': 3326, 'n_words': 1051}