2020-10-15 23:22:41 +09:00
|
|
|
class Invidious::Jobs::RefreshChannelsJob < Invidious::Jobs::BaseJob
|
|
|
|
private getter db : DB::Database
|
|
|
|
|
2021-01-24 03:39:04 +09:00
|
|
|
def initialize(@db)
|
2020-10-15 23:22:41 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def begin
|
2021-01-24 03:39:04 +09:00
|
|
|
max_fibers = CONFIG.channel_threads
|
2020-12-27 13:14:33 +09:00
|
|
|
lim_fibers = max_fibers
|
|
|
|
active_fibers = 0
|
2020-10-15 23:22:41 +09:00
|
|
|
active_channel = Channel(Bool).new
|
2021-10-09 01:39:02 +09:00
|
|
|
backoff = 2.minutes
|
2020-10-15 23:22:41 +09:00
|
|
|
|
|
|
|
loop do
|
2021-01-05 00:51:06 +09:00
|
|
|
LOGGER.debug("RefreshChannelsJob: Refreshing all channels")
|
2022-01-10 06:04:10 +09:00
|
|
|
PG_DB.query("SELECT id FROM channels ORDER BY updated") do |rs|
|
2020-10-15 23:22:41 +09:00
|
|
|
rs.each do
|
|
|
|
id = rs.read(String)
|
|
|
|
|
2020-12-27 13:14:33 +09:00
|
|
|
if active_fibers >= lim_fibers
|
2021-01-05 00:51:06 +09:00
|
|
|
LOGGER.trace("RefreshChannelsJob: Fiber limit reached, waiting...")
|
2020-10-15 23:22:41 +09:00
|
|
|
if active_channel.receive
|
2021-01-05 00:51:06 +09:00
|
|
|
LOGGER.trace("RefreshChannelsJob: Fiber limit ok, continuing")
|
2020-12-27 13:14:33 +09:00
|
|
|
active_fibers -= 1
|
2020-10-15 23:22:41 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-05 01:05:45 +09:00
|
|
|
LOGGER.debug("RefreshChannelsJob: #{id} : Spawning fiber")
|
2020-12-27 13:14:33 +09:00
|
|
|
active_fibers += 1
|
2020-10-15 23:22:41 +09:00
|
|
|
spawn do
|
|
|
|
begin
|
2021-01-05 00:51:06 +09:00
|
|
|
LOGGER.trace("RefreshChannelsJob: #{id} fiber : Fetching channel")
|
2022-01-22 12:27:50 +09:00
|
|
|
channel = fetch_channel(id, pull_all_videos: CONFIG.full_refresh)
|
2020-10-15 23:22:41 +09:00
|
|
|
|
2020-12-27 13:14:33 +09:00
|
|
|
lim_fibers = max_fibers
|
2020-12-27 13:20:33 +09:00
|
|
|
|
2021-01-05 00:51:06 +09:00
|
|
|
LOGGER.trace("RefreshChannelsJob: #{id} fiber : Updating DB")
|
2021-12-03 03:16:41 +09:00
|
|
|
Invidious::Database::Channels.update_author(id, channel.author)
|
2020-10-15 23:22:41 +09:00
|
|
|
rescue ex
|
2021-01-05 00:51:06 +09:00
|
|
|
LOGGER.error("RefreshChannelsJob: #{id} : #{ex.message}")
|
2020-10-15 23:22:41 +09:00
|
|
|
if ex.message == "Deleted or invalid channel"
|
2021-12-03 03:16:41 +09:00
|
|
|
Invidious::Database::Channels.update_mark_deleted(id)
|
2020-10-15 23:22:41 +09:00
|
|
|
else
|
2020-12-27 13:14:33 +09:00
|
|
|
lim_fibers = 1
|
2021-01-05 00:51:06 +09:00
|
|
|
LOGGER.error("RefreshChannelsJob: #{id} fiber : backing off for #{backoff}s")
|
2020-10-15 23:22:41 +09:00
|
|
|
sleep backoff
|
|
|
|
if backoff < 1.days
|
|
|
|
backoff += backoff
|
|
|
|
else
|
|
|
|
backoff = 1.days
|
|
|
|
end
|
|
|
|
end
|
2020-12-27 13:20:33 +09:00
|
|
|
ensure
|
2021-01-05 01:05:45 +09:00
|
|
|
LOGGER.debug("RefreshChannelsJob: #{id} fiber : Done")
|
2020-12-27 13:20:33 +09:00
|
|
|
active_channel.send(true)
|
2020-10-15 23:22:41 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-02-22 08:46:26 +09:00
|
|
|
LOGGER.debug("RefreshChannelsJob: Done, sleeping for #{CONFIG.channel_refresh_interval}")
|
|
|
|
sleep CONFIG.channel_refresh_interval
|
2020-10-15 23:22:41 +09:00
|
|
|
Fiber.yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|