34 lines
836 B
Ruby
34 lines
836 B
Ruby
# frozen_string_literal: true
|
|
|
|
module Streams
|
|
class NightCloudSweeperJob
|
|
include Sidekiq::Job
|
|
|
|
sidekiq_options retry: 1, queue: "default"
|
|
|
|
INTERVAL_SECS = ENV.fetch("STREAM_NIGHT_SWEEP_INTERVAL_SECS", "900").to_i
|
|
REDIS_CHAIN_KEY = "streams:night_sweep:chain"
|
|
|
|
def self.ensure_chain
|
|
return unless redis
|
|
return if redis.get(REDIS_CHAIN_KEY)
|
|
|
|
redis.set(REDIS_CHAIN_KEY, "1", ex: INTERVAL_SECS * 2)
|
|
perform_in(INTERVAL_SECS)
|
|
end
|
|
|
|
def self.redis
|
|
@redis ||= Redis.new(url: ENV.fetch("REDIS_URL", "redis://localhost:6379/0"))
|
|
rescue Redis::CannotConnectError
|
|
nil
|
|
end
|
|
|
|
def perform
|
|
Streams::NightCloudSweeper.sweep!
|
|
ensure
|
|
self.class.redis&.set(REDIS_CHAIN_KEY, "1", ex: INTERVAL_SECS * 2)
|
|
self.class.perform_in(INTERVAL_SECS)
|
|
end
|
|
end
|
|
end
|