2024-05-03 03:04:43 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module E621StatusUpdater
|
2024-05-03 05:54:36 +00:00
|
|
|
def self.run(force: E621Status.count == 0)
|
2024-05-03 03:04:43 +00:00
|
|
|
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?
|
2024-05-03 05:54:36 +00:00
|
|
|
if previous_status == status && !force
|
2024-05-03 03:04:43 +00:00
|
|
|
Cache.redis.set("e621_status:last_change_time", (Time.now - 2.seconds).to_s)
|
2024-05-03 05:54:36 +00:00
|
|
|
elsif last_change_time.nil? || (Time.now - last_change_time) >= 3.minutes || force
|
2024-05-03 03:04:43 +00:00
|
|
|
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
|