mirror of
https://github.com/iv-org/invidious.git
synced 2024-11-30 02:32:56 +05:30
apply review suggestions
This commit is contained in:
parent
5d0149844f
commit
3850739d7f
@ -35,24 +35,34 @@ class Invidious::Jobs::NotificationJob < Invidious::Jobs::BaseJob
|
|||||||
PG.connect_listen(pg_url, "notifications") { |event| connections.each(&.send(event)) }
|
PG.connect_listen(pg_url, "notifications") { |event| connections.each(&.send(event)) }
|
||||||
|
|
||||||
# hash of channels to their videos (id+published) that need notifying
|
# hash of channels to their videos (id+published) that need notifying
|
||||||
to_notify = Hash(String, Set(VideoNotification)).new(->(hash : Hash(String, Set(VideoNotification)), key : String) { hash[key] = Set(VideoNotification).new })
|
to_notify = Hash(String, Set(VideoNotification)).new(
|
||||||
|
->(hash : Hash(String, Set(VideoNotification)), key : String) {
|
||||||
|
hash[key] = Set(VideoNotification).new
|
||||||
|
}
|
||||||
|
)
|
||||||
|
notify_mutex = Mutex.new()
|
||||||
|
|
||||||
# fiber to locally cache all incoming notifications (from pubsub webhooks and refresh channels job)
|
# fiber to locally cache all incoming notifications (from pubsub webhooks and refresh channels job)
|
||||||
spawn do
|
spawn do
|
||||||
begin
|
begin
|
||||||
loop do
|
loop do
|
||||||
notification = notification_channel.receive
|
notification = notification_channel.receive
|
||||||
|
notify_mutex.synchronize do
|
||||||
to_notify[notification.channel_id] << notification
|
to_notify[notification.channel_id] << notification
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
# fiber to regularly persist all cached notifications
|
# fiber to regularly persist all cached notifications
|
||||||
spawn do
|
spawn do
|
||||||
loop do
|
loop do
|
||||||
begin
|
begin
|
||||||
LOGGER.debug("NotificationJob: waking up")
|
LOGGER.debug("NotificationJob: waking up")
|
||||||
|
cloned = {} of String => Set(VideoNotification)
|
||||||
|
notify_mutex.synchronize do
|
||||||
cloned = to_notify.clone
|
cloned = to_notify.clone
|
||||||
to_notify.clear
|
to_notify.clear
|
||||||
|
end
|
||||||
|
|
||||||
cloned.each do |channel_id, notifications|
|
cloned.each do |channel_id, notifications|
|
||||||
if notifications.empty?
|
if notifications.empty?
|
||||||
@ -63,6 +73,7 @@ class Invidious::Jobs::NotificationJob < Invidious::Jobs::BaseJob
|
|||||||
if CONFIG.enable_user_notifications
|
if CONFIG.enable_user_notifications
|
||||||
video_ids = notifications.map { |n| n.video_id }
|
video_ids = notifications.map { |n| n.video_id }
|
||||||
Invidious::Database::Users.add_multiple_notifications(channel_id, video_ids)
|
Invidious::Database::Users.add_multiple_notifications(channel_id, video_ids)
|
||||||
|
PG_DB.using_connection do |conn|
|
||||||
notifications.each do |n|
|
notifications.each do |n|
|
||||||
# Deliver notifications to `/api/v1/auth/notifications`
|
# Deliver notifications to `/api/v1/auth/notifications`
|
||||||
payload = {
|
payload = {
|
||||||
@ -70,7 +81,8 @@ class Invidious::Jobs::NotificationJob < Invidious::Jobs::BaseJob
|
|||||||
"videoId" => n.video_id,
|
"videoId" => n.video_id,
|
||||||
"published" => n.published.to_unix,
|
"published" => n.published.to_unix,
|
||||||
}.to_json
|
}.to_json
|
||||||
PG_DB.exec("NOTIFY notifications, E'#{payload}'")
|
conn.exec("NOTIFY notifications, E'#{payload}'")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
Invidious::Database::Users.feed_needs_update(channel_id)
|
Invidious::Database::Users.feed_needs_update(channel_id)
|
||||||
|
Loading…
Reference in New Issue
Block a user