Last active
September 1, 2021 18:11
-
-
Save haarts/baf6ab7d391f0b6a36bf566984e24c6e to your computer and use it in GitHub Desktop.
Final version of the producer. It can (and does) crash but it's under management of Systemd which restarts it after 5 minutes. The only fanciness is the variable sleep time.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "dotenv" | |
require "redis" | |
require "db" | |
require "pg" | |
require "log" | |
module Producer | |
VERSION = "0.1.0" | |
Dotenv.load? "env" | |
Log.setup_from_env(default_level: :debug) | |
Log.info { "Producer started" } | |
redis = Redis.new(url: ENV["REDIS"]) | |
pg = DB.open(ENV["POSTGRES"]) | |
begin | |
loop do | |
server_ids = [] of Int64 | |
pg.query("SELECT id FROM servers WHERE last_checked_at < now() - interval '1 hour' OR last_checked_at IS NULL") do |rs| | |
rs.each do | |
server_ids.push(rs.read(Int64)) | |
end | |
end | |
Log.debug &.emit("server_ids", server_ids: server_ids) | |
redis.rpush("hosts", server_ids) | |
closest_to_due = pg.query_one?("SELECT last_checked_at FROM servers WHERE last_checked_at > now() - interval '1 hour' ORDER BY last_ch | |
if closest_to_due.nil? | |
closest_to_due = Time.utc - 1.hour | |
end | |
period_to_sleep = Time.utc - (closest_to_due - 10.seconds) | |
Log.info &.emit("Sleeping", sleep: period_to_sleep.to_s) | |
sleep period_to_sleep | |
end | |
ensure | |
redis.close | |
pg.close | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment