# frozen_string_literal: true require "net/http" require "time" module Neetodeploy class Worker attr_reader :mutex, :queue, :thread def initialize @mutex = Mutex.new @queue = Queue.new end def push(msg) start && queue.push(msg) end def start mutex.synchronize do return true if thread&.alive? @thread = Thread.new { run } end true end private def run puts "neetodeploy-autoscale: reporter worker started" until queue.empty? queue_time = queue.pop work(queue_time) end puts "neetodeploy-autoscale: reporter stopping worker" end def work(payload) url = URI.parse("http://nd-queue-time-exporter-web-deployment:3000/metrics") url.query = payload post = Net::HTTP::Post.new(url) post["AuthToken"] = "K0An3O3MSyEEMTCnRd1IHgGjdGQkzy" begin Net::HTTP.start(url.host, url.port, use_ssl: false) do |http| response = http.request(post) end rescue StandardError => e puts "Exception in sending post request to exporter from Rails process: #{e.message}" end end end end