persist_redis_chunks.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. const logger = require('@overleaf/logger')
  2. const commandLineArgs = require('command-line-args')
  3. const redis = require('../lib/redis')
  4. const knex = require('../lib/knex.js')
  5. const knexReadOnly = require('../lib/knex_read_only.js')
  6. const { client } = require('../lib/mongodb.js')
  7. const { scanAndProcessDueItems } = require('../lib/scan')
  8. const persistBuffer = require('../lib/persist_buffer')
  9. const { claimPersistJob } = require('../lib/chunk_store/redis')
  10. const rclient = redis.rclientHistory
  11. const optionDefinitions = [{ name: 'dry-run', alias: 'd', type: Boolean }]
  12. const options = commandLineArgs(optionDefinitions)
  13. const DRY_RUN = options['dry-run'] || false
  14. logger.initialize('persist-redis-chunks')
  15. async function persistProjectAction(projectId) {
  16. const job = await claimPersistJob(projectId)
  17. // Set limits to force us to persist all of the changes.
  18. const farFuture = new Date()
  19. farFuture.setTime(farFuture.getTime() + 7 * 24 * 3600 * 1000)
  20. const limits = {
  21. maxChanges: 0,
  22. minChangeTimestamp: farFuture,
  23. maxChangeTimestamp: farFuture,
  24. }
  25. await persistBuffer(projectId, limits)
  26. if (job && job.close) {
  27. await job.close()
  28. }
  29. }
  30. async function runPersistChunks() {
  31. await scanAndProcessDueItems(
  32. rclient,
  33. 'persistChunks',
  34. 'persist-time',
  35. persistProjectAction,
  36. DRY_RUN
  37. )
  38. }
  39. if (require.main === module) {
  40. runPersistChunks()
  41. .catch(err => {
  42. logger.fatal(
  43. { err, taskName: 'persistChunks' },
  44. 'Unhandled error in runPersistChunks'
  45. )
  46. process.exit(1)
  47. })
  48. .finally(async () => {
  49. await redis.disconnect()
  50. await client.close()
  51. await knex.destroy()
  52. await knexReadOnly.destroy()
  53. })
  54. } else {
  55. module.exports = {
  56. runPersistChunks,
  57. }
  58. }