Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

misc: refresh subscription with limit #71

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions src/refreshSubscriptionCron.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,45 @@ const { GoogleClient } = require('./server/lib/GoogleClient');
const { checkAndRefreshAccessToken } = require('./server/lib/oauth');
const { Subscription } = require('./server/models/subscriptionModel');
const { User } = require('./server/models/userModel');
const { errorLogger } = require('./server/lib/logger');

async function refreshSubscription() {
const currentTime = new Date();
const expiredIn3Day = new Date(currentTime);
expiredIn3Day.setDate(currentTime.getDate() + 3);
const subscriptions = await Subscription.findAll(); // TODO: add lastKey
let lastKey = null;
const lastKeyRecord = await Subscription.findByPk('SYSTEM_LAST_KEY');
if (lastKeyRecord && lastKeyRecord.formId) {
lastKey = lastKeyRecord.formId;
}
const findAllQuery = {
limit: 100,
};
if (lastKey) {
findAllQuery.lastKey = {
id: lastKey,
};
}
const subscriptions = await Subscription.findAll(findAllQuery);
if (subscriptions.lastKey) {
if (!lastKeyRecord) {
await Subscription.create({
id: 'SYSTEM_LAST_KEY',
formId: subscriptions.lastKey.id,
});
} else {
lastKeyRecord.formId = subscriptions.lastKey.id;
await lastKeyRecord.save();
}
} else if (lastKeyRecord) {
lastKeyRecord.formId = '';
await lastKeyRecord.save();
}
const users = {};
for (const subscription of subscriptions) {
if (subscription.id === 'SYSTEM_LAST_KEY') {
continue;
}
if (subscription.watchExpiredAt < currentTime) {
continue;
}
Expand Down Expand Up @@ -46,7 +77,7 @@ async function refreshSubscription() {
}
} catch (e) {
console.error('refreshing error subscription: ', subscription.id);
console.error(e && e.message);
errorLogger(e);
}
}
}
Expand Down
Loading