2020-10-15 14:22:41 +00:00
|
|
|
class Invidious::Jobs::RefreshChannelsJob < Invidious::Jobs::BaseJob
|
|
|
|
private getter db : DB::Database
|
|
|
|
|
2021-01-23 18:39:04 +00:00
|
|
|
def initialize(@db)
|
2020-10-15 14:22:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def begin
|
2021-01-23 18:39:04 +00:00
|
|
|
max_fibers = CONFIG.channel_threads
|
2020-12-27 04:14:33 +00:00
|
|
|
lim_fibers = max_fibers
|
|
|
|
active_fibers = 0
|
2020-10-15 14:22:41 +00:00
|
|
|
active_channel = Channel(Bool).new
|
|
|
|
backoff = 1.seconds
|
|
|
|
|
|
|
|
loop do
|
2021-01-04 15:51:06 +00:00
|
|
|
LOGGER.debug("RefreshChannelsJob: Refreshing all channels")
|
2020-10-15 14:22:41 +00:00
|
|
|
db.query("SELECT id FROM channels ORDER BY updated") do |rs|
|
|
|
|
rs.each do
|
|
|
|
id = rs.read(String)
|
|
|
|
|
2020-12-27 04:14:33 +00:00
|
|
|
if active_fibers >= lim_fibers
|
2021-01-04 15:51:06 +00:00
|
|
|
LOGGER.trace("RefreshChannelsJob: Fiber limit reached, waiting...")
|
2020-10-15 14:22:41 +00:00
|
|
|
if active_channel.receive
|
2021-01-04 15:51:06 +00:00
|
|
|
LOGGER.trace("RefreshChannelsJob: Fiber limit ok, continuing")
|
2020-12-27 04:14:33 +00:00
|
|
|
active_fibers -= 1
|
2020-10-15 14:22:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-04 16:05:45 +00:00
|
|
|
LOGGER.debug("RefreshChannelsJob: #{id} : Spawning fiber")
|
2020-12-27 04:14:33 +00:00
|
|
|
active_fibers += 1
|
2020-10-15 14:22:41 +00:00
|
|
|
spawn do
|
|
|
|
begin
|
2021-01-04 15:51:06 +00:00
|
|
|
LOGGER.trace("RefreshChannelsJob: #{id} fiber : Fetching channel")
|
2021-01-23 18:39:04 +00:00
|
|
|
channel = fetch_channel(id, db, CONFIG.full_refresh)
|
2020-10-15 14:22:41 +00:00
|
|
|
|
2020-12-27 04:14:33 +00:00
|
|
|
lim_fibers = max_fibers
|
2020-12-27 04:20:33 +00:00
|
|
|
|
2021-01-04 15:51:06 +00:00
|
|
|
LOGGER.trace("RefreshChannelsJob: #{id} fiber : Updating DB")
|
2020-10-15 14:22:41 +00:00
|
|
|
db.exec("UPDATE channels SET updated = $1, author = $2, deleted = false WHERE id = $3", Time.utc, channel.author, id)
|
|
|
|
rescue ex
|
2021-01-04 15:51:06 +00:00
|
|
|
LOGGER.error("RefreshChannelsJob: #{id} : #{ex.message}")
|
2020-10-15 14:22:41 +00:00
|
|
|
if ex.message == "Deleted or invalid channel"
|
|
|
|
db.exec("UPDATE channels SET updated = $1, deleted = true WHERE id = $2", Time.utc, id)
|
|
|
|
else
|
2020-12-27 04:14:33 +00:00
|
|
|
lim_fibers = 1
|
2021-01-04 15:51:06 +00:00
|
|
|
LOGGER.error("RefreshChannelsJob: #{id} fiber : backing off for #{backoff}s")
|
2020-10-15 14:22:41 +00:00
|
|
|
sleep backoff
|
|
|
|
if backoff < 1.days
|
|
|
|
backoff += backoff
|
|
|
|
else
|
|
|
|
backoff = 1.days
|
|
|
|
end
|
|
|
|
end
|
2020-12-27 04:20:33 +00:00
|
|
|
ensure
|
2021-01-04 16:05:45 +00:00
|
|
|
LOGGER.debug("RefreshChannelsJob: #{id} fiber : Done")
|
2020-12-27 04:20:33 +00:00
|
|
|
active_channel.send(true)
|
2020-10-15 14:22:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-04 15:51:06 +00:00
|
|
|
LOGGER.debug("RefreshChannelsJob: Done, sleeping for one minute")
|
2020-10-15 14:22:41 +00:00
|
|
|
sleep 1.minute
|
|
|
|
Fiber.yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|