increase_compile_timeouts.js 2.1 KB

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