2020-03-11 10:31:21 -04:00
|
|
|
const { Subscription } = require('../../app/src/models/Subscription')
|
|
|
|
const RecurlyWrapper = require('../../app/src/Features/Subscription/RecurlyWrapper')
|
|
|
|
const SubscriptionUpdater = require('../../app/src/Features/Subscription/SubscriptionUpdater')
|
|
|
|
const minimist = require('minimist')
|
2024-06-25 11:30:18 -04:00
|
|
|
const { setTimeout } = require('node:timers/promises')
|
2020-03-11 10:31:21 -04:00
|
|
|
|
2020-03-16 07:30:38 -04:00
|
|
|
// make sure all `allMismatchReasons` are displayed in the output
|
|
|
|
const util = require('util')
|
2024-06-25 11:30:18 -04:00
|
|
|
const pLimit = require('p-limit')
|
2024-06-27 04:11:09 -04:00
|
|
|
const { waitForDb } = require('../../app/src/infrastructure/mongodb')
|
|
|
|
|
2020-03-16 07:30:38 -04:00
|
|
|
util.inspect.defaultOptions.maxArrayLength = null
|
|
|
|
|
2020-03-11 10:31:21 -04:00
|
|
|
const ScriptLogger = {
|
|
|
|
checkedSubscriptionsCount: 0,
|
|
|
|
mismatchSubscriptionsCount: 0,
|
|
|
|
allMismatchReasons: {},
|
|
|
|
|
|
|
|
recordMismatch: (subscription, recurlySubscription) => {
|
|
|
|
const mismatchReasons = {}
|
|
|
|
if (subscription.planCode !== recurlySubscription.plan.plan_code) {
|
2020-12-16 05:37:00 -05:00
|
|
|
mismatchReasons.recurlyPlan = recurlySubscription.plan.plan_code
|
|
|
|
mismatchReasons.olPlan = subscription.planCode
|
2020-03-11 10:31:21 -04:00
|
|
|
}
|
|
|
|
if (recurlySubscription.state === 'expired') {
|
2020-12-16 05:37:00 -05:00
|
|
|
mismatchReasons.state = 'expired'
|
2020-03-11 10:31:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!Object.keys(mismatchReasons).length) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ScriptLogger.mismatchSubscriptionsCount += 1
|
|
|
|
const mismatchReasonsString = JSON.stringify(mismatchReasons)
|
|
|
|
if (ScriptLogger.allMismatchReasons[mismatchReasonsString]) {
|
2023-07-27 07:35:36 -04:00
|
|
|
ScriptLogger.allMismatchReasons[mismatchReasonsString].push({
|
|
|
|
id: subscription._id,
|
|
|
|
name: subscription.planCode,
|
|
|
|
})
|
2020-03-11 10:31:21 -04:00
|
|
|
} else {
|
|
|
|
ScriptLogger.allMismatchReasons[mismatchReasonsString] = [
|
2023-07-27 07:35:36 -04:00
|
|
|
{
|
|
|
|
id: subscription._id,
|
|
|
|
name: subscription.planCode,
|
|
|
|
},
|
2020-03-11 10:31:21 -04:00
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
printProgress: () => {
|
|
|
|
console.warn(
|
2020-12-15 05:23:54 -05:00
|
|
|
`Subscriptions checked: ${ScriptLogger.checkedSubscriptionsCount}. Mismatches: ${ScriptLogger.mismatchSubscriptionsCount}`
|
2020-03-11 10:31:21 -04:00
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
printSummary: () => {
|
|
|
|
console.log('All Mismatch Reasons:', ScriptLogger.allMismatchReasons)
|
|
|
|
console.log(
|
|
|
|
'Mismatch Subscriptions Count',
|
|
|
|
ScriptLogger.mismatchSubscriptionsCount
|
|
|
|
)
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2020-03-11 10:31:21 -04:00
|
|
|
}
|
|
|
|
|
2024-06-25 11:30:18 -04:00
|
|
|
const handleSyncSubscriptionError = async (subscription, error) => {
|
2020-03-16 07:30:38 -04:00
|
|
|
console.warn(`Errors with subscription id=${subscription._id}:`, error)
|
2020-03-11 10:31:21 -04:00
|
|
|
if (typeof error === 'string' && error.match(/429$/)) {
|
2024-06-25 11:30:18 -04:00
|
|
|
await setTimeout(1000 * 60 * 5)
|
|
|
|
return
|
2020-03-11 10:31:21 -04:00
|
|
|
}
|
2020-03-16 07:30:38 -04:00
|
|
|
if (typeof error === 'string' && error.match(/5\d\d$/)) {
|
2024-06-25 11:30:18 -04:00
|
|
|
await setTimeout(1000 * 60)
|
|
|
|
await syncSubscription(subscription)
|
|
|
|
return
|
2020-03-16 07:30:38 -04:00
|
|
|
}
|
2024-06-25 11:30:18 -04:00
|
|
|
await setTimeout(80)
|
2020-03-11 10:31:21 -04:00
|
|
|
}
|
|
|
|
|
2024-06-25 11:30:18 -04:00
|
|
|
const syncSubscription = async subscription => {
|
|
|
|
let recurlySubscription
|
|
|
|
try {
|
|
|
|
recurlySubscription = await RecurlyWrapper.promises.getSubscription(
|
|
|
|
subscription.recurlySubscription_id
|
|
|
|
)
|
|
|
|
} catch (error) {
|
|
|
|
await handleSyncSubscriptionError(subscription, error)
|
|
|
|
return
|
|
|
|
}
|
2020-03-11 10:31:21 -04:00
|
|
|
|
2024-06-25 11:30:18 -04:00
|
|
|
ScriptLogger.recordMismatch(subscription, recurlySubscription)
|
2020-03-11 10:31:21 -04:00
|
|
|
|
2024-06-25 11:30:18 -04:00
|
|
|
if (!COMMIT) {
|
|
|
|
return
|
|
|
|
}
|
2020-03-11 10:31:21 -04:00
|
|
|
|
2024-06-25 11:30:18 -04:00
|
|
|
try {
|
|
|
|
await SubscriptionUpdater.promises.updateSubscriptionFromRecurly(
|
|
|
|
recurlySubscription,
|
|
|
|
subscription,
|
|
|
|
{}
|
|
|
|
)
|
|
|
|
} catch (error) {
|
|
|
|
await handleSyncSubscriptionError(subscription, error)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
await setTimeout(80)
|
2020-03-11 10:31:21 -04:00
|
|
|
}
|
|
|
|
|
2024-06-25 11:30:18 -04:00
|
|
|
const syncSubscriptions = async subscriptions => {
|
|
|
|
const limit = pLimit(ASYNC_LIMIT)
|
|
|
|
return await Promise.all(
|
|
|
|
subscriptions.map(subscription =>
|
|
|
|
limit(() => syncSubscription(subscription))
|
|
|
|
)
|
|
|
|
)
|
2020-03-11 10:31:21 -04:00
|
|
|
}
|
|
|
|
|
2024-06-25 11:30:18 -04:00
|
|
|
const loopForSubscriptions = async skipInitial => {
|
|
|
|
let skip = skipInitial
|
2020-03-11 10:31:21 -04:00
|
|
|
|
2024-06-25 11:30:18 -04:00
|
|
|
// iterate while there are more subscriptions to fetch
|
|
|
|
while (true) {
|
|
|
|
const subscriptions = await Subscription.find({
|
|
|
|
recurlySubscription_id: { $exists: true, $ne: '' },
|
2020-03-11 10:31:21 -04:00
|
|
|
})
|
2024-06-25 11:30:18 -04:00
|
|
|
.sort('_id')
|
|
|
|
.skip(skip)
|
|
|
|
.limit(FETCH_LIMIT)
|
|
|
|
.exec()
|
|
|
|
|
|
|
|
if (subscriptions.length === 0) {
|
|
|
|
console.warn('DONE')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
await syncSubscriptions(subscriptions)
|
|
|
|
|
|
|
|
ScriptLogger.checkedSubscriptionsCount += subscriptions.length
|
|
|
|
retryCounter = 0
|
|
|
|
ScriptLogger.printProgress()
|
|
|
|
ScriptLogger.printSummary()
|
|
|
|
|
|
|
|
skip += FETCH_LIMIT
|
|
|
|
}
|
2020-03-11 10:31:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
let retryCounter = 0
|
2024-06-25 11:30:18 -04:00
|
|
|
const run = async () => {
|
2024-06-27 04:11:09 -04:00
|
|
|
await waitForDb()
|
|
|
|
|
2024-06-25 11:30:18 -04:00
|
|
|
while (true) {
|
|
|
|
try {
|
|
|
|
await loopForSubscriptions(
|
|
|
|
MONGO_SKIP + ScriptLogger.checkedSubscriptionsCount
|
|
|
|
)
|
|
|
|
break
|
|
|
|
} catch (error) {
|
|
|
|
if (retryCounter < 3) {
|
|
|
|
console.error(error)
|
|
|
|
retryCounter += 1
|
|
|
|
console.warn(`RETRYING IN 60 SECONDS. (${retryCounter}/3)`)
|
|
|
|
await setTimeout(60000)
|
|
|
|
} else {
|
|
|
|
console.error('Failed after 3 retries')
|
2020-03-11 10:31:21 -04:00
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
2024-06-25 11:30:18 -04:00
|
|
|
}
|
|
|
|
}
|
2020-03-11 10:31:21 -04:00
|
|
|
|
|
|
|
let FETCH_LIMIT, ASYNC_LIMIT, COMMIT, MONGO_SKIP
|
|
|
|
const setup = () => {
|
|
|
|
const argv = minimist(process.argv.slice(2))
|
|
|
|
FETCH_LIMIT = argv.fetch ? argv.fetch : 100
|
|
|
|
ASYNC_LIMIT = argv.async ? argv.async : 10
|
|
|
|
MONGO_SKIP = argv.skip ? argv.skip : 0
|
|
|
|
COMMIT = argv.commit !== undefined
|
|
|
|
if (!COMMIT) {
|
|
|
|
console.warn('Doing dry run without --commit')
|
|
|
|
}
|
|
|
|
if (MONGO_SKIP) {
|
|
|
|
console.warn(`Skipping first ${MONGO_SKIP} records`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setup()
|
2024-06-25 11:30:18 -04:00
|
|
|
run().then(() => {
|
|
|
|
process.exit()
|
|
|
|
})
|