Sha256: 82e2b350e314f374ac6cc39f30d5fcd9a43b1f324479ecaa0d5bade03cb3ff9b

Contents?: true

Size: 1.27 KB

Versions: 10

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

require 'erb'
require 'time'

# Exporter for the https://github.com/moove-it/sidekiq-scheduler
module Sidekiq
  module Prometheus
    module Exporter
      class Scheduler
        TEMPLATE = ERB.new(File.read(File.expand_path('templates/scheduler.erb', __dir__)))
        SECONDS_IN_MINUTE = 60
        Stats = Struct.new(:jobs_count, :enabled_jobs_count, :last_runs)

        def self.available?
          defined?(Sidekiq::Scheduler)
        end

        def initialize
          @stats = Stats.new(recurring_jobs.count, enabled_recurring_jobs.count, last_runs)
        end

        def to_s
          TEMPLATE.result(binding).chomp!
        end

        private

        def last_runs
          enabled_recurring_jobs.each_with_object({}) do |name, memo|
            execution_time = SidekiqScheduler::RedisManager.get_job_last_time(name)
            next unless execution_time

            memo[name] = (Time.now.to_i - Time.parse(execution_time).to_i) / SECONDS_IN_MINUTE
          end
        end

        def enabled_recurring_jobs
          recurring_jobs.select { |name| Sidekiq::Scheduler.job_enabled?(name) }
        end

        def recurring_jobs
          @recurring_jobs ||= (Sidekiq.schedule! || {}).keys.sort
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
sidekiq-prometheus-exporter-0.2.1 lib/sidekiq/prometheus/exporter/scheduler.rb
sidekiq-prometheus-exporter-0.2.0 lib/sidekiq/prometheus/exporter/scheduler.rb
sidekiq-prometheus-exporter-0.1.17 lib/sidekiq/prometheus/exporter/scheduler.rb
sidekiq-prometheus-exporter-0.1.16 lib/sidekiq/prometheus/exporter/scheduler.rb
sidekiq-prometheus-exporter-0.1.15 lib/sidekiq/prometheus/exporter/scheduler.rb
sidekiq-prometheus-exporter-0.1.14 lib/sidekiq/prometheus/exporter/scheduler.rb
sidekiq-prometheus-exporter-0.1.13 lib/sidekiq/prometheus/exporter/scheduler.rb
sidekiq-prometheus-exporter-0.1.12 lib/sidekiq/prometheus/exporter/scheduler.rb
sidekiq-prometheus-exporter-0.1.11 lib/sidekiq/prometheus/exporter/scheduler.rb
sidekiq-prometheus-exporter-0.1.10 lib/sidekiq/prometheus/exporter/scheduler.rb