mirror of
https://github.com/iv-org/invidious.git
synced 2025-08-25 14:08:31 +00: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)
|
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::NotificationJob.new(CONNECTION_CHANNEL, CONFIG.database_url)
|
||||||
|
|
||||||
|
Invidious::Jobs.register Invidious::Jobs::ClearExpiredItemsJob.new
|
||||||
|
|
||||||
Invidious::Jobs.start_all
|
Invidious::Jobs.start_all
|
||||||
|
|
||||||
def popular_videos
|
def popular_videos
|
||||||
|
@ -4,7 +4,7 @@ module Invidious::Database::Nonces
|
|||||||
extend self
|
extend self
|
||||||
|
|
||||||
# -------------------
|
# -------------------
|
||||||
# Insert
|
# Insert / Delete
|
||||||
# -------------------
|
# -------------------
|
||||||
|
|
||||||
def insert(nonce : String, expire : Time)
|
def insert(nonce : String, expire : Time)
|
||||||
@ -17,6 +17,15 @@ module Invidious::Database::Nonces
|
|||||||
PG_DB.exec(request, nonce, expire)
|
PG_DB.exec(request, nonce, expire)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def delete_expired
|
||||||
|
request = <<-SQL
|
||||||
|
DELETE FROM nonces *
|
||||||
|
WHERE expire < now()
|
||||||
|
SQL
|
||||||
|
|
||||||
|
PG_DB.exec(request)
|
||||||
|
end
|
||||||
|
|
||||||
# -------------------
|
# -------------------
|
||||||
# Update
|
# Update
|
||||||
# -------------------
|
# -------------------
|
||||||
|
@ -22,6 +22,15 @@ module Invidious::Database::Videos
|
|||||||
PG_DB.exec(request, id)
|
PG_DB.exec(request, id)
|
||||||
end
|
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)
|
def update(video : Video)
|
||||||
request = <<-SQL
|
request = <<-SQL
|
||||||
UPDATE videos
|
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…
Reference in New Issue
Block a user