2023-04-18 07:15:34 -04:00
|
|
|
const { ObjectId } = require('mongodb')
|
|
|
|
const {
|
|
|
|
db,
|
|
|
|
waitForDb,
|
|
|
|
READ_PREFERENCE_SECONDARY,
|
|
|
|
} = require('../../app/src/infrastructure/mongodb')
|
2020-08-17 10:14:11 -04:00
|
|
|
|
2023-03-21 05:09:06 -04:00
|
|
|
const ONE_MONTH_IN_MS = 1000 * 60 * 60 * 24 * 31
|
|
|
|
let ID_EDGE_PAST
|
|
|
|
const ID_EDGE_FUTURE = objectIdFromMs(Date.now() + 1000)
|
2023-02-16 05:41:58 -05:00
|
|
|
let BATCH_DESCENDING
|
|
|
|
let BATCH_SIZE
|
|
|
|
let VERBOSE_LOGGING
|
2023-03-21 05:09:06 -04:00
|
|
|
let BATCH_RANGE_START
|
2021-11-30 05:41:53 -05:00
|
|
|
let BATCH_RANGE_END
|
2023-03-21 05:09:06 -04:00
|
|
|
let BATCH_MAX_TIME_SPAN_IN_MS
|
2023-02-16 05:41:58 -05:00
|
|
|
|
|
|
|
function refreshGlobalOptionsForBatchedUpdate(options = {}) {
|
|
|
|
options = Object.assign({}, options, process.env)
|
|
|
|
|
|
|
|
BATCH_DESCENDING = options.BATCH_DESCENDING === 'true'
|
|
|
|
BATCH_SIZE = parseInt(options.BATCH_SIZE, 10) || 1000
|
|
|
|
VERBOSE_LOGGING = options.VERBOSE_LOGGING === 'true'
|
|
|
|
if (options.BATCH_LAST_ID) {
|
2023-03-21 05:09:06 -04:00
|
|
|
BATCH_RANGE_START = ObjectId(options.BATCH_LAST_ID)
|
2023-02-16 05:41:58 -05:00
|
|
|
} else if (options.BATCH_RANGE_START) {
|
2023-03-21 05:09:06 -04:00
|
|
|
BATCH_RANGE_START = ObjectId(options.BATCH_RANGE_START)
|
|
|
|
} else {
|
2021-11-11 10:10:01 -05:00
|
|
|
if (BATCH_DESCENDING) {
|
2023-03-21 05:09:06 -04:00
|
|
|
BATCH_RANGE_START = ID_EDGE_FUTURE
|
2021-11-30 05:41:53 -05:00
|
|
|
} else {
|
2023-03-21 05:09:06 -04:00
|
|
|
BATCH_RANGE_START = ID_EDGE_PAST
|
2021-11-30 05:41:53 -05:00
|
|
|
}
|
|
|
|
}
|
2023-03-21 05:09:06 -04:00
|
|
|
BATCH_MAX_TIME_SPAN_IN_MS =
|
|
|
|
parseInt(options.BATCH_MAX_TIME_SPAN_IN_MS, 10) || ONE_MONTH_IN_MS
|
|
|
|
if (options.BATCH_RANGE_END) {
|
|
|
|
BATCH_RANGE_END = ObjectId(options.BATCH_RANGE_END)
|
|
|
|
} else {
|
2021-11-30 05:41:53 -05:00
|
|
|
if (BATCH_DESCENDING) {
|
2023-03-21 05:09:06 -04:00
|
|
|
BATCH_RANGE_END = ID_EDGE_PAST
|
2021-11-11 10:10:01 -05:00
|
|
|
} else {
|
2023-03-21 05:09:06 -04:00
|
|
|
BATCH_RANGE_END = ID_EDGE_FUTURE
|
2021-11-11 10:10:01 -05:00
|
|
|
}
|
2020-08-17 10:14:11 -04:00
|
|
|
}
|
2023-03-21 05:09:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getNextBatch({
|
|
|
|
collection,
|
|
|
|
query,
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
projection,
|
|
|
|
findOptions,
|
|
|
|
}) {
|
|
|
|
if (BATCH_DESCENDING) {
|
|
|
|
query._id = {
|
|
|
|
$gt: end,
|
|
|
|
$lt: start,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
query._id = {
|
|
|
|
$gt: start,
|
|
|
|
$lt: end,
|
|
|
|
}
|
2021-11-30 05:41:53 -05:00
|
|
|
}
|
2023-03-21 05:09:06 -04:00
|
|
|
return await collection
|
|
|
|
.find(query, findOptions)
|
2020-10-07 09:17:49 -04:00
|
|
|
.project(projection)
|
2021-11-11 10:10:01 -05:00
|
|
|
.sort({ _id: BATCH_DESCENDING ? -1 : 1 })
|
2020-08-17 10:14:11 -04:00
|
|
|
.limit(BATCH_SIZE)
|
|
|
|
.toArray()
|
|
|
|
}
|
|
|
|
|
|
|
|
async function performUpdate(collection, nextBatch, update) {
|
2020-09-25 04:40:07 -04:00
|
|
|
return collection.updateMany(
|
|
|
|
{ _id: { $in: nextBatch.map(entry => entry._id) } },
|
|
|
|
update
|
|
|
|
)
|
2020-08-17 10:14:11 -04:00
|
|
|
}
|
|
|
|
|
2023-03-21 05:09:06 -04:00
|
|
|
function objectIdFromMs(ms) {
|
|
|
|
return ObjectId.createFromTime(ms / 1000)
|
|
|
|
}
|
|
|
|
|
|
|
|
function getMsFromObjectId(id) {
|
|
|
|
return id.getTimestamp().getTime()
|
|
|
|
}
|
|
|
|
|
|
|
|
function getNextEnd(start) {
|
|
|
|
let end
|
|
|
|
if (BATCH_DESCENDING) {
|
|
|
|
end = objectIdFromMs(getMsFromObjectId(start) - BATCH_MAX_TIME_SPAN_IN_MS)
|
|
|
|
if (getMsFromObjectId(end) <= getMsFromObjectId(BATCH_RANGE_END)) {
|
|
|
|
end = BATCH_RANGE_END
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
end = objectIdFromMs(getMsFromObjectId(start) + BATCH_MAX_TIME_SPAN_IN_MS)
|
|
|
|
if (getMsFromObjectId(end) >= getMsFromObjectId(BATCH_RANGE_END)) {
|
|
|
|
end = BATCH_RANGE_END
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return end
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getIdEdgePast(collection) {
|
|
|
|
const [first] = await collection
|
|
|
|
.find({})
|
|
|
|
.project({ _id: 1 })
|
|
|
|
.limit(1)
|
|
|
|
.toArray()
|
|
|
|
if (!first) return null
|
|
|
|
// Go 1s further into the past in order to include the first entry via
|
|
|
|
// first._id > ID_EDGE_PAST
|
|
|
|
return objectIdFromMs(Math.max(0, getMsFromObjectId(first._id) - 1000))
|
|
|
|
}
|
|
|
|
|
2021-08-04 09:09:06 -04:00
|
|
|
async function batchedUpdate(
|
|
|
|
collectionName,
|
|
|
|
query,
|
|
|
|
update,
|
|
|
|
projection,
|
2023-02-16 05:41:58 -05:00
|
|
|
findOptions,
|
|
|
|
batchedUpdateOptions
|
2021-08-04 09:09:06 -04:00
|
|
|
) {
|
2020-10-07 09:17:49 -04:00
|
|
|
await waitForDb()
|
|
|
|
const collection = db[collectionName]
|
2023-03-21 05:09:06 -04:00
|
|
|
ID_EDGE_PAST = await getIdEdgePast(collection)
|
|
|
|
if (!ID_EDGE_PAST) {
|
|
|
|
console.warn(`The collection ${collectionName} appears to be empty.`)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
refreshGlobalOptionsForBatchedUpdate(batchedUpdateOptions)
|
2020-08-17 10:14:11 -04:00
|
|
|
|
2023-02-16 05:41:58 -05:00
|
|
|
findOptions = findOptions || {}
|
2023-04-18 07:15:34 -04:00
|
|
|
findOptions.readPreference = READ_PREFERENCE_SECONDARY
|
2021-08-04 09:09:06 -04:00
|
|
|
|
2020-09-25 04:40:07 -04:00
|
|
|
projection = projection || { _id: 1 }
|
2020-08-17 10:14:11 -04:00
|
|
|
let nextBatch
|
|
|
|
let updated = 0
|
2023-03-21 05:09:06 -04:00
|
|
|
let start = BATCH_RANGE_START
|
|
|
|
|
|
|
|
while (start !== BATCH_RANGE_END) {
|
|
|
|
let end = getNextEnd(start)
|
|
|
|
nextBatch = await getNextBatch({
|
2021-08-04 09:09:06 -04:00
|
|
|
collection,
|
|
|
|
query,
|
2023-03-21 05:09:06 -04:00
|
|
|
start,
|
|
|
|
end,
|
2021-08-04 09:09:06 -04:00
|
|
|
projection,
|
2023-03-21 05:09:06 -04:00
|
|
|
findOptions,
|
|
|
|
})
|
|
|
|
if (nextBatch.length > 0) {
|
|
|
|
end = nextBatch[nextBatch.length - 1]._id
|
|
|
|
updated += nextBatch.length
|
2020-09-25 04:40:07 -04:00
|
|
|
|
2023-03-21 05:09:06 -04:00
|
|
|
if (VERBOSE_LOGGING) {
|
|
|
|
console.log(
|
|
|
|
`Running update on batch with ids ${JSON.stringify(
|
|
|
|
nextBatch.map(entry => entry._id)
|
|
|
|
)}`
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
console.error(`Running update on batch ending ${end}`)
|
|
|
|
}
|
2020-11-04 04:53:26 -05:00
|
|
|
|
2023-03-21 05:09:06 -04:00
|
|
|
if (typeof update === 'function') {
|
2023-03-22 07:21:47 -04:00
|
|
|
await update(nextBatch)
|
2023-03-21 05:09:06 -04:00
|
|
|
} else {
|
|
|
|
await performUpdate(collection, nextBatch, update)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.error(`Completed batch ending ${end}`)
|
|
|
|
start = end
|
2020-08-17 10:14:11 -04:00
|
|
|
}
|
|
|
|
return updated
|
|
|
|
}
|
|
|
|
|
2022-01-25 05:18:07 -05:00
|
|
|
function batchedUpdateWithResultHandling(
|
|
|
|
collection,
|
|
|
|
query,
|
|
|
|
update,
|
|
|
|
projection,
|
|
|
|
options
|
|
|
|
) {
|
|
|
|
batchedUpdate(collection, query, update, projection, options)
|
2023-03-21 05:09:06 -04:00
|
|
|
.then(processed => {
|
|
|
|
console.error({ processed })
|
2020-08-17 10:14:11 -04:00
|
|
|
process.exit(0)
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error({ error })
|
|
|
|
process.exit(1)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
batchedUpdate,
|
2021-04-27 03:52:58 -04:00
|
|
|
batchedUpdateWithResultHandling,
|
2020-08-17 10:14:11 -04:00
|
|
|
}
|