Sha256: f386b1ca83d3213480efdd39e730fafbef0dfc2cfee136e421b7d317cce89079

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.0 lib/neetodeploy/autoscale/sidekiq_middleware.rb