force_doc_flush.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. const { ObjectId } = require('mongodb')
  2. const { db, waitForDb } = require('../app/src/infrastructure/mongodb')
  3. const DocumentUpdaterHandler = require('../app/src/Features/DocumentUpdater/DocumentUpdaterHandler')
  4. const PROJECT_ID = process.env.PROJECT_ID
  5. const DOC_ID = process.env.DOC_ID
  6. const VERBOSE_LOGGING = process.env.VERBOSE_LOGGING === 'true'
  7. const DRY_RUN = process.env.DRY_RUN !== 'false'
  8. console.log({
  9. PROJECT_ID,
  10. DOC_ID,
  11. VERBOSE_LOGGING,
  12. DRY_RUN,
  13. })
  14. async function main() {
  15. await waitForDb()
  16. const { lines, version, ranges } = await getDocument()
  17. const size = lines.reduce((size, line) => size + line.length + 1, 0)
  18. console.log('doc stats:', {
  19. lineCount: lines.length,
  20. size,
  21. version,
  22. })
  23. if (!DRY_RUN) {
  24. console.log(`updating doc ${DOC_ID} in mongo for project ${PROJECT_ID}`)
  25. const { result } = await db.docs.updateOne(
  26. { _id: ObjectId(DOC_ID), project_id: ObjectId(PROJECT_ID) },
  27. {
  28. $set: { lines, version, ranges },
  29. $inc: { rev: 1 }, // maintain same behaviour as Docstore upsertIntoDocCollection
  30. $unset: {
  31. inS3: true,
  32. },
  33. }
  34. )
  35. console.log('mongo result', result)
  36. if (result.n !== 1 || result.nModified !== 1 || result.ok !== 1) {
  37. throw new Error('unexpected result from mongo update')
  38. }
  39. console.log(`deleting doc ${DOC_ID} from redis for project ${PROJECT_ID}`)
  40. await DocumentUpdaterHandler.promises.deleteDoc(PROJECT_ID, DOC_ID, true)
  41. }
  42. }
  43. function getDocument() {
  44. return new Promise((resolve, reject) => {
  45. DocumentUpdaterHandler.getDocument(
  46. PROJECT_ID,
  47. DOC_ID,
  48. -1,
  49. (error, lines, version, ranges) => {
  50. if (error) {
  51. reject(error)
  52. } else {
  53. resolve({ lines, version, ranges })
  54. }
  55. }
  56. )
  57. })
  58. }
  59. main()
  60. .then(() => {
  61. console.error('Done.')
  62. process.exit(0)
  63. })
  64. .catch(error => {
  65. console.error({ error })
  66. process.exit(1)
  67. })