Websites/app/logical/e621_status_updater.rb

20 lines
884 B
Ruby

# frozen_string_literal: true
module E621StatusUpdater
def self.run(force: E621Status.count == 0)
return if Rails.env.development?
status = Requests::E621.status
previous_status = Cache.redis.get("e621_status:current").to_i
last_change_time = Cache.redis.get("e621_status:last_change_time")
last_change_time = Time.parse(last_change_time) if last_change_time.present?
if previous_status == status && !force
Cache.redis.set("e621_status:last_change_time", (Time.now - 2.seconds).to_s)
elsif last_change_time.nil? || (Time.now - last_change_time) >= 3.minutes || force
Rails.logger.debug { "Updating status: #{previous_status} -> #{status}" }
Cache.redis.set("e621_status:current", status.to_s)
Cache.redis.set("e621_status:last_change_time", (Time.now - 2.seconds).to_s)
E621Status.create!(status: status)
end
end
end