disconnect_all_users.mjs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { promisify } from 'node:util'
  2. import Settings from '@overleaf/settings'
  3. import AdminController from '../app/src/Features/ServerAdmin/AdminController.js'
  4. import minimist from 'minimist'
  5. import { fileURLToPath } from 'node:url'
  6. const args = minimist(process.argv.slice(2), {
  7. string: ['confirm-site-url', 'delay-in-seconds'],
  8. default: {
  9. 'delay-in-seconds': 10,
  10. 'confirm-site-url': '',
  11. },
  12. })
  13. const sleep = promisify(setTimeout)
  14. async function main() {
  15. if (args.help) {
  16. console.error()
  17. console.error(
  18. ' usage: node disconnect_all_users.mjs [--delay-in-seconds=10] --confirm-site-url=https://www....\n'
  19. )
  20. process.exit(1)
  21. }
  22. const isSaaS = Boolean(Settings.overleaf)
  23. if (isSaaS && args['confirm-site-url'] !== Settings.siteUrl) {
  24. console.error()
  25. console.error(
  26. 'Please confirm the environment you want to disconnect ALL USERS from by specifying the site URL aka PUBLIC_URL, e.g. --confirm-site-url=https://www.dev-overleaf.com for the dev-env'
  27. )
  28. console.error()
  29. console.error(
  30. `!!! --confirm-site-url=${
  31. args['confirm-site-url'] || "''"
  32. } does not match the PUBLIC_URL in this environment.`
  33. )
  34. console.error()
  35. console.error(' Are you running this script in the correct environment?')
  36. process.exit(1)
  37. }
  38. const delay = parseInt(args['delay-in-seconds'] || '10', 10)
  39. if (!(delay >= 0)) {
  40. console.error(
  41. `--delay-in-seconds='${args['delay-in-seconds']}' should be a number >=0`
  42. )
  43. process.exit(1)
  44. }
  45. console.log()
  46. console.log(
  47. `Disconnect all users from ${args['confirm-site-url']}, with delay ${delay}`
  48. )
  49. if (isSaaS) {
  50. console.error(' Use CTRL+C in the next 5s to abort.')
  51. await sleep(5 * 1000)
  52. }
  53. await AdminController._sendDisconnectAllUsersMessage(delay)
  54. }
  55. if (fileURLToPath(import.meta.url) === process.argv[1]) {
  56. try {
  57. await main()
  58. console.error('Done.')
  59. process.exit(0)
  60. } catch (error) {
  61. console.error('Error', error)
  62. process.exit(1)
  63. }
  64. }