# frozen_string_literal: true module E621StatusUpdater def self.run 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 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 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