mirror of
https://github.com/iv-org/invidious.git
synced 2025-04-20 23:46:26 -04:00
Jobs: add hourly cleaning of expired nonces/cached videos
This commit is contained in:
parent
a7d9df5516
commit
d6a7df3adc
@ -172,6 +172,8 @@ end
|
||||
CONNECTION_CHANNEL = Channel({Bool, Channel(PQ::Notification)}).new(32)
|
||||
Invidious::Jobs.register Invidious::Jobs::NotificationJob.new(CONNECTION_CHANNEL, CONFIG.database_url)
|
||||
|
||||
Invidious::Jobs.register Invidious::Jobs::ClearExpiredItemsJob.new
|
||||
|
||||
Invidious::Jobs.start_all
|
||||
|
||||
def popular_videos
|
||||
|
@ -4,7 +4,7 @@ module Invidious::Database::Nonces
|
||||
extend self
|
||||
|
||||
# -------------------
|
||||
# Insert
|
||||
# Insert / Delete
|
||||
# -------------------
|
||||
|
||||
def insert(nonce : String, expire : Time)
|
||||
@ -17,6 +17,15 @@ module Invidious::Database::Nonces
|
||||
PG_DB.exec(request, nonce, expire)
|
||||
end
|
||||
|
||||
def delete_expired
|
||||
request = <<-SQL
|
||||
DELETE FROM nonces *
|
||||
WHERE expire < now()
|
||||
SQL
|
||||
|
||||
PG_DB.exec(request)
|
||||
end
|
||||
|
||||
# -------------------
|
||||
# Update
|
||||
# -------------------
|
||||
|
@ -22,6 +22,15 @@ module Invidious::Database::Videos
|
||||
PG_DB.exec(request, id)
|
||||
end
|
||||
|
||||
def delete_expired
|
||||
request = <<-SQL
|
||||
DELETE FROM videos *
|
||||
WHERE updated < (now() - interval '6 hours')
|
||||
SQL
|
||||
|
||||
PG_DB.exec(request)
|
||||
end
|
||||
|
||||
def update(video : Video)
|
||||
request = <<-SQL
|
||||
UPDATE videos
|
||||
|
23
src/invidious/jobs/clear_expired_items_job.cr
Normal file
23
src/invidious/jobs/clear_expired_items_job.cr
Normal file
@ -0,0 +1,23 @@
|
||||
class Invidious::Jobs::ClearExpiredItemsJob < Invidious::Jobs::BaseJob
|
||||
# Remove items (videos, nonces, etc..) whose cache is outdated every hour.
|
||||
# Removes the need for a cron job.
|
||||
def begin
|
||||
loop do
|
||||
failed = false
|
||||
|
||||
begin
|
||||
Invidious::Database::Videos.delete_expired
|
||||
Invidious::Database::Nonces.delete_expired
|
||||
rescue DB::Error
|
||||
failed = true
|
||||
end
|
||||
|
||||
# Retry earlier than scheduled on DB error
|
||||
if failed
|
||||
sleep 10.minutes
|
||||
else
|
||||
sleep 1.hour
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user