force_doc_flush.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. const { ObjectId } = require('mongodb-legacy')
  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: new ObjectId(DOC_ID), project_id: new 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 (
  37. result.matchedCount !== 1 ||
  38. result.modifiedCount !== 1 ||
  39. !result.acknowledged
  40. ) {
  41. throw new Error('unexpected result from mongo update')
  42. }
  43. console.log(`deleting doc ${DOC_ID} from redis for project ${PROJECT_ID}`)
  44. await DocumentUpdaterHandler.promises.deleteDoc(PROJECT_ID, DOC_ID, true)
  45. }
  46. }
  47. function getDocument() {
  48. return new Promise((resolve, reject) => {
  49. DocumentUpdaterHandler.getDocument(
  50. PROJECT_ID,
  51. DOC_ID,
  52. -1,
  53. (error, lines, version, ranges) => {
  54. if (error) {
  55. reject(error)
  56. } else {
  57. resolve({ lines, version, ranges })
  58. }
  59. }
  60. )
  61. })
  62. }
  63. main()
  64. .then(() => {
  65. console.error('Done.')
  66. process.exit(0)
  67. })
  68. .catch(error => {
  69. console.error({ error })
  70. process.exit(1)
  71. })