increase_compile_timeouts.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. const mongojs = require('../app/src/infrastructure/mongojs')
  2. const { db } = mongojs
  3. const async = require('async')
  4. const minilist = require('minimist')
  5. const newTimeout = 240
  6. const oldTimeoutLimits = { $gt: 60, $lt: 240 }
  7. const updateUser = function(user, callback) {
  8. console.log(`Updating user ${user._id}`)
  9. const update = {
  10. $set: {
  11. 'features.compileTimeout': newTimeout
  12. }
  13. }
  14. db.users.update(
  15. {
  16. _id: user._id,
  17. 'features.compileTimeout': oldTimeoutLimits
  18. },
  19. update,
  20. callback
  21. )
  22. }
  23. const updateUsers = (users, callback) =>
  24. async.eachLimit(users, ASYNC_LIMIT, updateUser, function(error) {
  25. if (error) {
  26. callback(error)
  27. return
  28. }
  29. counter += users.length
  30. console.log(`${counter} users updated`)
  31. if (DO_ALL) {
  32. return loopForUsers(callback)
  33. } else {
  34. console.log('*** run again to continue updating ***')
  35. return callback()
  36. }
  37. })
  38. var loopForUsers = callback =>
  39. db.users
  40. .find(
  41. { 'features.compileTimeout': oldTimeoutLimits },
  42. { 'features.compileTimeout': 1 }
  43. )
  44. .limit(FETCH_LIMIT, function(error, users) {
  45. if (error) {
  46. callback(error)
  47. return
  48. }
  49. if (users.length === 0) {
  50. console.log(`DONE (${counter} users updated)`)
  51. return callback()
  52. }
  53. updateUsers(users, callback)
  54. })
  55. var counter = 0
  56. var run = () =>
  57. loopForUsers(function(error) {
  58. if (error) {
  59. throw error
  60. }
  61. process.exit()
  62. })
  63. let FETCH_LIMIT, ASYNC_LIMIT, DO_ALL
  64. var setup = function() {
  65. let args = minilist(process.argv.slice(2))
  66. // --fetch N get N users each time
  67. FETCH_LIMIT = args.fetch ? args.fetch : 100
  68. // --async M run M updates in parallel
  69. ASYNC_LIMIT = args.async ? args.async : 10
  70. // --all means run to completion
  71. if (args.all) {
  72. if (args.fetch) {
  73. console.error('error: do not use --fetch with --all')
  74. process.exit(1)
  75. } else {
  76. DO_ALL = true
  77. // if we are updating for all users then ignore the fetch limit.
  78. FETCH_LIMIT = 0
  79. // A limit() value of 0 (i.e. .limit(0)) is equivalent to setting
  80. // no limit.
  81. // https://docs.mongodb.com/manual/reference/method/cursor.limit
  82. }
  83. }
  84. }
  85. setup()
  86. run()