<commit_before>require 'sequel'
module Delayed
  module Backend
    module Sequel
      # A job object that is persisted to the database.
      # Contains the work object as a YAML field.
      class Job < ::Sequel::Model(::DelayedJobSequel.table_name)
        include Delayed::Backend::Base
        plugin :timestamps

        def before_save
          super
          set_default_run_at
        end

        def_dataset_method :ready_to_run do |worker_name|
          db_time_now = model.db_time_now
          filter do
            (
              (run_at <= db_time_now) &
              ::Sequel.expr(:locked_at => nil) |
              {:locked_by => worker_name}
            ) & {:failed_at => nil}
          end
        end

        def_dataset_method :by_priority do
          order(::Sequel.expr(:run_at).asc)
        end

        def self.before_fork
          ::Sequel::Model.db.disconnect
        end

        # When a worker is exiting, make sure we don't have any locked jobs.
        def self.clear_locks!(worker_name)
          filter(:locked_by => worker_name).update(:locked_by => nil, :locked_at => nil)
        end

        # Find a few candidate jobs to run (in case some immediately get locked by others).
        def self.find_available(worker_name, limit = 5, max_run_time = Worker.max_run_time)
          ds = ready_to_run(worker_name)
          ds = ds.filter("priority >= ?", Worker.min_priority) if Worker.min_priority
          ds = ds.filter("priority <= ?", Worker.max_priority) if Worker.max_priority
          ds = ds.filter(:queue => Worker.queues) if Worker.queues.any?
          ds.by_priority.limit(limit).all
        end

        # Lock this job for this worker.
        # Returns true if we have the lock, false otherwise.
        def lock_exclusively!(max_run_time, worker)
          now           = self.class.db_time_now
          affected_rows = self.class.ready_to_run(worker).filter({ :id => pk }).update(:locked_at => now, :locked_by => worker)
          if affected_rows == 1
            self.locked_at = now
            self.locked_by = worker
            return true<commit_msg>Order jobs by_priority more like original gem<commit_after>require 'sequel'
module Delayed
  module Backend
    module Sequel
      # A job object that is persisted to the database.
      # Contains the work object as a YAML field.
      class Job < ::Sequel::Model(::DelayedJobSequel.table_name)
        include Delayed::Backend::Base
        plugin :timestamps

        def before_save
          super
          set_default_run_at
        end

        def_dataset_method :ready_to_run do |worker_name|
          db_time_now = model.db_time_now
          filter do
            (
              (run_at <= db_time_now) &
              ::Sequel.expr(:locked_at => nil) |
              {:locked_by => worker_name}
            ) & {:failed_at => nil}
          end
        end

        def_dataset_method :by_priority do
          order(::Sequel.expr(:priority).asc, ::Sequel.expr(:run_at).asc)
        end

        def self.before_fork
          ::Sequel::Model.db.disconnect
        end

        # When a worker is exiting, make sure we don't have any locked jobs.
        def self.clear_locks!(worker_name)
          filter(:locked_by => worker_name).update(:locked_by => nil, :locked_at => nil)
        end

        # Find a few candidate jobs to run (in case some immediately get locked by others).
        def self.find_available(worker_name, limit = 5, max_run_time = Worker.max_run_time)
          ds = ready_to_run(worker_name)
          ds = ds.filter("priority >= ?", Worker.min_priority) if Worker.min_priority
          ds = ds.filter("priority <= ?", Worker.max_priority) if Worker.max_priority
          ds = ds.filter(:queue => Worker.queues) if Worker.queues.any?
          ds.by_priority.limit(limit).all
        end

        # Lock this job for this worker.
        # Returns true if we have the lock, false otherwise.
        def lock_exclusively!(max_run_time, worker)
          now           = self.class.db_time_now
          affected_rows = self.class.ready_to_run(worker).filter({ :id => pk }).update(:locked_at => now, :locked_by => worker)
          if affected_rows == 1
            self.locked_at = now
            self.locked_by = worker
            return true
{'fraction_non_alphanumeric': 0.09090909090909091, 'fraction_numerical': 0.0009276437847866419, 'mean_word_length': 2.0851216022889845, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 25, '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': '1980245', 'n_tokens_mistral': 1283, 'n_tokens_neox': 1244, 'n_words': 330}