increase_compile_timeouts.js 2.2 KB

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