<commit_before>require "travis/lock"
require "redlock"
require "metriks"

class Travis::Api::App
  module Services
    class ScheduleCronJobs < Travis::Services::Base
      register :schedule_cron_jobs

      def self.run
        loop do
          begin
            Travis::Lock.exclusive("enqueue_cron_jobs", options) do
              Metriks.timer("rake.crons_jobs_enqueue.enqueue").time { enqueue }
            end
          rescue Travis::Lock::Redis::LockError => e
            Travis.logger.error e.message
          end
          sleep(Travis::API::V3::Models::Cron::SCHEDULER_INTERVAL)
        end
      end

      def self.options
        @options ||= {
          strategy:      :redis,
          url:           Travis.config.redis.url,
          retries:       0
        }
      end

      def self.enqueue
        scheduled = Travis::API::V3::Models::Cron.scheduled
        Travis.logger.info "Going to enqueue #{scheduled.count} cron jobs"

        scheduled.each do |cron|
          begin
            cron.needs_new_build? ? cron.enqueue : cron.skip_and_schedule_next_build
          rescue => e
            Metriks.meter("rake.cron_jobs_enqueue.enqueue.error").mark
            Raven.capture_exception(e, tags: { 'cron_id' => cron.try(:id) })
            next
          end
        end
      end
    end
  end
end<commit_msg>Improve metrics for crons<commit_after>require "travis/lock"
require "redlock"
require "metriks"

class Travis::Api::App
  module Services
    class ScheduleCronJobs < Travis::Services::Base
      register :schedule_cron_jobs

      def self.run
        loop do
          begin
            Travis::Lock.exclusive("enqueue_cron_jobs", options) do
              Metriks.timer("schedule_cron_jobs_enqueue").time { enqueue }
            end
          rescue Travis::Lock::Redis::LockError => e
            Travis.logger.error e.message
          end
          sleep(Travis::API::V3::Models::Cron::SCHEDULER_INTERVAL)
        end
      end

      def self.options
        @options ||= {
          strategy: :redis,
          url:      Travis.config.redis.url,
          retries:  0
        }
      end

      def self.enqueue
        scheduled = Travis::API::V3::Models::Cron.scheduled
        count = scheduled.count

        Travis.logger.info "Found #{count} cron jobs to enqueue"

        Metriks.gauge("cron_jobs_to_be_scheduled", count)

        scheduled.each do |cron|
          begin
            cron.needs_new_build? ? cron.enqueue : cron.skip_and_schedule_next_build
          rescue => e
            Metriks.meter("schedule_cron_jobs_error").mark
            Raven.capture_exception(e, tags: { 'cron_id' => cron.try(:id) })
            next
          end
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.09708029197080292, 'fraction_numerical': 0.0021897810218978104, 'mean_word_length': 1.9409871244635193, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '4146548', 'n_tokens_mistral': 866, 'n_tokens_neox': 845, 'n_words': 160}