Skip to content

Commit

Permalink
Fixes issue where changing the subscription check interval would not …
Browse files Browse the repository at this point in the history
…affect the current check without restart (#854)
  • Loading branch information
Tzahi12345 committed Dec 1, 2023
1 parent eaefcc5 commit 240e87b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 63 deletions.
64 changes: 1 addition & 63 deletions backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,13 +534,7 @@ async function loadConfig() {
subscriptions.forEach(async sub => subscriptions_api.writeSubscriptionMetadata(sub));
subscriptions_api.updateSubscriptionPropertyMultiple(subscriptions, {downloading: false, child_process: null});
// runs initially, then runs every ${subscriptionCheckInterval} seconds
const watchSubscriptionsInterval = function() {
watchSubscriptions();
const subscriptionsCheckInterval = config_api.getConfigItem('ytdl_subscriptions_check_interval');
setTimeout(watchSubscriptionsInterval, subscriptionsCheckInterval*1000);
}

watchSubscriptionsInterval();
subscriptions_api.watchSubscriptionsInterval();
}

// start the server here
Expand Down Expand Up @@ -570,62 +564,6 @@ function loadConfigValues() {
utils.updateLoggerLevel(logger_level);
}

function calculateSubcriptionRetrievalDelay(subscriptions_amount) {
// frequency is once every 5 mins by default
const subscriptionsCheckInterval = config_api.getConfigItem('ytdl_subscriptions_check_interval');
let interval_in_ms = subscriptionsCheckInterval * 1000;
const subinterval_in_ms = interval_in_ms/subscriptions_amount;
return subinterval_in_ms;
}

async function watchSubscriptions() {
let subscriptions = await subscriptions_api.getAllSubscriptions();

if (!subscriptions) return;

// auto pause deprecated streamingOnly mode
const streaming_only_subs = subscriptions.filter(sub => sub.streamingOnly);
subscriptions_api.updateSubscriptionPropertyMultiple(streaming_only_subs, {paused: true});

const valid_subscriptions = subscriptions.filter(sub => !sub.paused && !sub.streamingOnly);

let subscriptions_amount = valid_subscriptions.length;
let delay_interval = calculateSubcriptionRetrievalDelay(subscriptions_amount);

let current_delay = 0;

const multiUserMode = config_api.getConfigItem('ytdl_multi_user_mode');
for (let i = 0; i < valid_subscriptions.length; i++) {
let sub = valid_subscriptions[i];

// don't check the sub if the last check for the same subscription has not completed
if (subscription_timeouts[sub.id]) {
logger.verbose(`Subscription: skipped checking ${sub.name} as the last check for ${sub.name} has not completed.`);
continue;
}

if (!sub.name) {
logger.verbose(`Subscription: skipped check for subscription with uid ${sub.id} as name has not been retrieved yet.`);
continue;
}

logger.verbose('Watching ' + sub.name + ' with delay interval of ' + delay_interval);
setTimeout(async () => {
const multiUserModeChanged = config_api.getConfigItem('ytdl_multi_user_mode') !== multiUserMode;
if (multiUserModeChanged) {
logger.verbose(`Skipping subscription ${sub.name} due to multi-user mode change.`);
return;
}
await subscriptions_api.getVideosForSub(sub.id);
subscription_timeouts[sub.id] = false;
}, current_delay);
subscription_timeouts[sub.id] = true;
current_delay += delay_interval;
const subscriptionsCheckInterval = config_api.getConfigItem('ytdl_subscriptions_check_interval');
if (current_delay >= subscriptionsCheckInterval * 1000) current_delay = 0;
}
}

function getOrigin() {
return url_domain.origin;
}
Expand Down
54 changes: 54 additions & 0 deletions backend/subscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,60 @@ exports.deleteSubscriptionFile = async (sub, file, deleteForever, file_uid = nul
}
}

let current_sub_index = 0; // To keep track of the current subscription
exports.watchSubscriptionsInterval = async () => {
const subscriptions_check_interval = config_api.getConfigItem('ytdl_subscriptions_check_interval');
let parent_interval = setInterval(() => watchSubscriptions(), subscriptions_check_interval*1000);
watchSubscriptions();
config_api.config_updated.subscribe(change => {
if (!change) return;
if (change['key'] === 'ytdl_subscriptions_check_interval' || change['key'] === 'ytdl_multi_user_mode') {
current_sub_index = 0; // TODO: start after the last sub check
clearInterval(parent_interval);
const new_interval = config_api.getConfigItem('ytdl_subscriptions_check_interval');
parent_interval = setInterval(() => watchSubscriptions(), new_interval*1000);
watchSubscriptions();
}
});
}

async function watchSubscriptions() {
const subscription_ids = await getValidSubscriptionsToCheck();
checkSubscription(subscription_ids[current_sub_index]);
current_sub_index = (current_sub_index + 1) % subscription_ids.length;
}

async function checkSubscription(sub_id) {
let sub = await exports.getSubscription(sub_id);

// don't check the sub if the last check for the same subscription has not completed
if (sub.downloading) {
logger.verbose(`Subscription: skipped checking ${sub.name} as it's downloading videos.`);
return;
}

if (!sub.name) {
logger.verbose(`Subscription: skipped check for subscription with uid ${sub.id} as name has not been retrieved yet.`);
return;
}

await exports.getVideosForSub(sub.id);
// TODO: make sure if multi user mode changes we are also cancelling this
}

async function getValidSubscriptionsToCheck() {
const subscriptions = await exports.getAllSubscriptions();

if (!subscriptions) return;

// auto pause deprecated streamingOnly mode
const streaming_only_subs = subscriptions.filter(sub => sub.streamingOnly);
exports.updateSubscriptionPropertyMultiple(streaming_only_subs, {paused: true});

const valid_subscription_ids = subscriptions.filter(sub => !sub.paused && !sub.streamingOnly).map(sub => sub.id);
return valid_subscription_ids;
}

exports.getVideosForSub = async (sub_id) => {
const sub = await exports.getSubscription(sub_id);
if (!sub || sub['downloading']) {
Expand Down

0 comments on commit 240e87b

Please sign in to comment.