travis-ci/job-board

View on GitHub
bin/job-board-reconcile-jobs

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby
# frozen_string_literal: true

libdir = File.expand_path('../lib', __dir__)
$LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)

require 'json'
require 'time'

require_relative '../lib/job_board'

$stdout.sync = true
$stderr.sync = true

def main(argv: ARGV)
  reconciler = JobBoard::JobQueueReconciler.new
  JobBoard.logger.info('starting reconciliation loop')
  n = 0

  loop do
    stats = reconciler.reconcile!(
      purge_unknown: JobBoard.config.reconcile_purge_unknown_always? ||
                     (n % JobBoard.config.reconcile_purge_unknown_every).zero?
    )
    stats.fetch(:sites, []).each do |site_def|
      JobBoard.logger.info(
        'reconciled',
        site: site_def[:site],
        reclaimed: site_def.fetch(:reclaimed, '???'),
        reclaimed_ids: site_def.fetch(
          :reclaimed_ids, [:notset]
        ).map(&:to_s).join(',')
      )
      site_def.fetch(:queues, []).each do |queue_def|
        JobBoard.logger.info(
          'queue stats',
          site: site_def[:site], queue: queue_def[:name],
          queued: queue_def[:queued], claimed: queue_def[:claimed]
        )
      end

      n += 1
    end

    JobBoard.redis_pool.with do |conn|
      conn.set(
        'latest-stats',
        JSON.dump(
          stats.merge(generated: Time.now.utc.iso8601)
        )
      )
    end

    break if argv.include?('--once')

    sleep(Integer(ENV['JOB_BOARD_RECONCILIATION_SLEEP_INTERVAL'] || 1))
  end

  0
end

exit main