expire_redis_chunks.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. const logger = require('@overleaf/logger')
  2. const commandLineArgs = require('command-line-args')
  3. const redis = require('../lib/redis')
  4. const { scanAndProcessDueItems } = require('../lib/scan')
  5. const { expireProject, claimExpireJob } = require('../lib/chunk_store/redis')
  6. const config = require('config')
  7. const { fetchNothing } = require('@overleaf/fetch-utils')
  8. const rclient = redis.rclientHistory
  9. const optionDefinitions = [
  10. { name: 'dry-run', alias: 'd', type: Boolean },
  11. { name: 'post-request', type: Boolean },
  12. ]
  13. const options = commandLineArgs(optionDefinitions)
  14. const DRY_RUN = options['dry-run'] || false
  15. const POST_REQUEST = options['post-request'] || false
  16. const HISTORY_V1_URL = `http://${process.env.HISTORY_V1_HOST || 'localhost'}:${process.env.PORT || 3100}`
  17. logger.initialize('expire-redis-chunks')
  18. async function expireProjectAction(projectId) {
  19. const job = await claimExpireJob(projectId)
  20. if (POST_REQUEST) {
  21. await requestProjectExpiry(projectId)
  22. } else {
  23. await expireProject(projectId)
  24. }
  25. if (job && job.close) {
  26. await job.close()
  27. }
  28. }
  29. async function requestProjectExpiry(projectId) {
  30. logger.debug({ projectId }, 'sending project expire request')
  31. const url = `${HISTORY_V1_URL}/api/projects/${projectId}/expire`
  32. const credentials = Buffer.from(
  33. `staging:${config.get('basicHttpAuth.password')}`
  34. ).toString('base64')
  35. await fetchNothing(url, {
  36. method: 'POST',
  37. headers: {
  38. Authorization: `Basic ${credentials}`,
  39. },
  40. })
  41. }
  42. async function runExpireChunks() {
  43. await scanAndProcessDueItems(
  44. rclient,
  45. 'expireChunks',
  46. 'expire-time',
  47. expireProjectAction,
  48. DRY_RUN
  49. )
  50. }
  51. if (require.main === module) {
  52. runExpireChunks()
  53. .catch(err => {
  54. logger.fatal(
  55. { err, taskName: 'expireChunks' },
  56. 'Unhandled error in runExpireChunks'
  57. )
  58. process.exit(1)
  59. })
  60. .finally(async () => {
  61. await redis.disconnect()
  62. })
  63. } else {
  64. module.exports = {
  65. runExpireChunks,
  66. }
  67. }