2022-03-25 10:00:07 -04:00
|
|
|
const READ_CONCURRENCY_SECONDARY =
|
|
|
|
parseInt(process.env.READ_CONCURRENCY_SECONDARY, 10) || 1000
|
|
|
|
const READ_CONCURRENCY_PRIMARY =
|
|
|
|
parseInt(process.env.READ_CONCURRENCY_PRIMARY, 10) || 500
|
|
|
|
const WRITE_CONCURRENCY = parseInt(process.env.WRITE_CONCURRENCY, 10) || 10
|
|
|
|
const BATCH_SIZE = parseInt(process.env.BATCH_SIZE, 10) || 100
|
|
|
|
const DRY_RUN = process.env.DRY_RUN !== 'false'
|
|
|
|
const MAX_CHATS_TO_DESTROY =
|
|
|
|
parseInt(process.env.MAX_CHATS_TO_DESTROY, 10) || false
|
|
|
|
// persist fallback in order to keep batchedUpdate in-sync
|
|
|
|
process.env.BATCH_SIZE = BATCH_SIZE
|
|
|
|
// raise mongo timeout to 10mins if otherwise unspecified
|
|
|
|
process.env.MONGO_SOCKET_TIMEOUT =
|
|
|
|
parseInt(process.env.MONGO_SOCKET_TIMEOUT, 10) || 600000
|
|
|
|
|
2024-08-02 12:27:31 -04:00
|
|
|
const { ObjectId } = require('mongodb-legacy')
|
2023-10-19 07:38:17 -04:00
|
|
|
const { promiseMapWithLimit } = require('@overleaf/promise-utils')
|
2022-03-25 10:00:07 -04:00
|
|
|
const { batchedUpdate } = require('./helpers/batchedUpdate')
|
|
|
|
const ChatApiHandler = require('../app/src/Features/Chat/ChatApiHandler')
|
2022-04-25 05:18:05 -04:00
|
|
|
const { getHardDeletedProjectIds } = require('./delete_orphaned_data_helper')
|
2022-03-25 10:00:07 -04:00
|
|
|
|
|
|
|
console.log({
|
|
|
|
DRY_RUN,
|
|
|
|
WRITE_CONCURRENCY,
|
|
|
|
BATCH_SIZE,
|
|
|
|
MAX_CHATS_TO_DESTROY,
|
|
|
|
})
|
|
|
|
|
|
|
|
const RESULT = {
|
|
|
|
DRY_RUN,
|
|
|
|
projectChatsDestroyed: 0,
|
|
|
|
continueFrom: null,
|
|
|
|
}
|
|
|
|
|
2023-03-22 07:21:47 -04:00
|
|
|
async function processBatch(rooms) {
|
2022-03-25 10:00:07 -04:00
|
|
|
if (rooms.length && rooms[0]._id) {
|
|
|
|
RESULT.continueFrom = rooms[0]._id
|
|
|
|
}
|
|
|
|
const projectIds = Array.from(
|
|
|
|
new Set(rooms.map(room => room.project_id.toString()))
|
2023-12-18 05:54:01 -05:00
|
|
|
).map(id => new ObjectId(id))
|
2022-03-25 10:00:07 -04:00
|
|
|
console.log(
|
|
|
|
`Checking projects (${projectIds.length})`,
|
|
|
|
JSON.stringify(projectIds)
|
|
|
|
)
|
|
|
|
|
2022-04-25 05:18:05 -04:00
|
|
|
const projectsWithOrphanedChat = await getHardDeletedProjectIds({
|
2022-03-25 10:00:07 -04:00
|
|
|
projectIds,
|
|
|
|
READ_CONCURRENCY_PRIMARY,
|
2022-04-25 05:18:05 -04:00
|
|
|
READ_CONCURRENCY_SECONDARY,
|
|
|
|
})
|
2022-03-25 10:00:07 -04:00
|
|
|
|
|
|
|
console.log(
|
|
|
|
`Destroying chat for projects (${projectsWithOrphanedChat.length})`,
|
|
|
|
JSON.stringify(projectsWithOrphanedChat)
|
|
|
|
)
|
|
|
|
if (!DRY_RUN) {
|
|
|
|
await promiseMapWithLimit(
|
|
|
|
WRITE_CONCURRENCY,
|
|
|
|
projectsWithOrphanedChat,
|
|
|
|
ChatApiHandler.promises.destroyProject
|
|
|
|
)
|
|
|
|
}
|
|
|
|
RESULT.projectChatsDestroyed += projectsWithOrphanedChat.length
|
|
|
|
|
|
|
|
console.log(RESULT)
|
|
|
|
if (
|
|
|
|
MAX_CHATS_TO_DESTROY &&
|
|
|
|
RESULT.projectChatsDestroyed >= MAX_CHATS_TO_DESTROY
|
|
|
|
) {
|
|
|
|
console.log(
|
|
|
|
`MAX_CHATS_TO_DELETE limit (${MAX_CHATS_TO_DESTROY}) reached. Stopping.`
|
|
|
|
)
|
|
|
|
process.exit(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
const projection = {
|
|
|
|
_id: 1,
|
|
|
|
project_id: 1,
|
|
|
|
}
|
|
|
|
await batchedUpdate('rooms', {}, processBatch, projection)
|
|
|
|
console.log('Final')
|
|
|
|
console.log(RESULT)
|
|
|
|
}
|
|
|
|
|
|
|
|
main()
|
|
|
|
.then(() => {
|
|
|
|
console.log('Done.')
|
|
|
|
process.exit(0)
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error({ error })
|
|
|
|
process.exit(1)
|
|
|
|
})
|