Sha256: 8d749959b0e9b2dac9be1cd6629717f08a1d0b3d652e23306f749654e6c71181

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

require "net/http"
require "time"

module Neetodeploy
  class SidekiqMiddleware
    def call(worker, job, queue)
      url = URI.parse("https://nd-queue-time-exporter.neetodeployapp.com/metrics")

      queues_by_name = ::Sidekiq::Queue.all.each_with_object({}) do |queue_name, obj|
        obj[queue_name.name] = queue_name
      end

      queues = queues_by_name.keys

      queues.each do |queue_obj|
        queue = queues_by_name.fetch(queue_obj) { |name| ::Sidekiq::Queue.new(name) }

        query_params = URI.encode_www_form(app_name: ENV.to_h["NEETODEPLOY_APP_NAME"], process_type: "worker", queue_obj: queue_obj, queue_time: queue_time(queue))
        url.query = query_params
        post = Net::HTTP::Post.new(url)
        post["AuthToken"] = "K0An3O3MSyEEMTCnRd1IHgGjdGQkzy"

        begin
          Net::HTTP.start(url.host, url.port, use_ssl: true) do |http|
            http.request(post)
          end
        rescue => e
          puts "Exception in sending post request to exporter from Sidekiq process: #{e.message}"
        end
      end

      yield
    end

    private

    def queues
      ::Sidekiq::Queue.all
    end

    def queue_time(queue_obj)
      queue = ::Sidekiq::Queue.new(queue_obj.name)

      (queue.latency * 1000).ceil
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
neetodeploy-autoscale-1.0.1 lib/neetodeploy/autoscale/sidekiq_middleware.rb