<commit_before># frozen_string_literal: true

require 'rails_autoscale_agent/logger'

module RailsAutoscaleAgent
  module WorkerAdapters
    class DelayedJob
      include RailsAutoscaleAgent::Logger
      include Singleton

      class << self
        attr_accessor :queues
      end

      def initialize
        # Track the known queues so we can continue reporting on queues that don't
        # currently have enqueued jobs.
        self.class.queues = Set.new

        install if enabled?
      end

      def enabled?
        defined? ::Delayed
      end

      def collect!(store)
        log_msg = String.new
        t = Time.now.utc
        db_time = t.strftime('%Y-%m-%d %H:%M:%S')

        # Ignore failed jobs (they skew latency measurement due to the original run_at)
        sql = "SELECT queue, min(run_at) FROM delayed_jobs WHERE attempts = 0 AND run_at < '#{db_time}' AND locked_at IS NULL GROUP BY queue"
        run_at_by_queue = Hash[ActiveRecord::Base.connection.select_rows(sql)]
        queues = self.class.queues | run_at_by_queue.keys

        queues.each do |queue|
          next if queue.nil? || queue.empty?
          run_at = run_at_by_queue[queue]
          run_at = Time.parse(run_at) if run_at.is_a?(String)
          latency_ms = run_at ? ((t - run_at)*1000).ceil : 0
          store.push latency_ms, t, queue
          log_msg << "dj.#{queue}=#{latency_ms} "
        end

        logger.debug log_msg unless log_msg.empty?
      end

      private

      def install
        plugin = Class.new(Delayed::Plugin) do
          require 'delayed_job'

          callbacks do |lifecycle|
            lifecycle.before(:enqueue) do |job, &block|
              queue = job.queue || 'default'
              WorkerAdapters::DelayedJob.queues.add queue
            end
          end
        end

        Delayed::Worker.plugins << plugin
      end
    end
  end
end
<commit_msg>Fix failed job condition in Delayed Job query<commit_after># frozen_string_literal: true

require 'rails_autoscale_agent/logger'

module RailsAutoscaleAgent
  module WorkerAdapters
    class DelayedJob
      include RailsAutoscaleAgent::Logger
      include Singleton

      class << self
        attr_accessor :queues
      end

      def initialize
        # Track the known queues so we can continue reporting on queues that don't
        # currently have enqueued jobs.
        self.class.queues = Set.new

        install if enabled?
      end

      def enabled?
        defined? ::Delayed
      end

      def collect!(store)
        log_msg = String.new
        t = Time.now.utc
        db_time = t.strftime('%Y-%m-%d %H:%M:%S')

        # Ignore failed jobs (they skew latency measurement due to the original run_at)
        sql = "SELECT queue, min(run_at) FROM delayed_jobs WHERE run_at < '#{db_time}' AND locked_at IS NULL AND failed_at IS NULL GROUP BY queue"
        run_at_by_queue = Hash[ActiveRecord::Base.connection.select_rows(sql)]
        queues = self.class.queues | run_at_by_queue.keys

        queues.each do |queue|
          next if queue.nil? || queue.empty?
          run_at = run_at_by_queue[queue]
          run_at = Time.parse(run_at) if run_at.is_a?(String)
          latency_ms = run_at ? ((t - run_at)*1000).ceil : 0
          store.push latency_ms, t, queue
          log_msg << "dj.#{queue}=#{latency_ms} "
        end

        logger.debug log_msg unless log_msg.empty?
      end

      private

      def install
        plugin = Class.new(Delayed::Plugin) do
          require 'delayed_job'

          callbacks do |lifecycle|
            lifecycle.before(:enqueue) do |job, &block|
              queue = job.queue || 'default'
              WorkerAdapters::DelayedJob.queues.add queue
            end
          end
        end

        Delayed::Worker.plugins << plugin
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.08357198646185889, 'fraction_numerical': 0.002863837542306691, 'mean_word_length': 2.1185064935064934, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 3, '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': '3746652', 'n_tokens_mistral': 1205, 'n_tokens_neox': 1142, 'n_words': 316}