2020-09-23 04:49:26 -04:00
|
|
|
const { db } = require('../app/src/infrastructure/mongojs')
|
2018-07-31 11:54:09 -04:00
|
|
|
const async = require('async')
|
|
|
|
const minilist = require('minimist')
|
|
|
|
|
|
|
|
const newTimeout = 240
|
2018-11-05 05:06:39 -05:00
|
|
|
const oldTimeoutLimits = { $gt: 60, $lt: 240 }
|
2018-07-31 11:54:09 -04:00
|
|
|
|
2018-11-05 05:06:39 -05:00
|
|
|
const updateUser = function(user, callback) {
|
2018-07-31 11:54:09 -04:00
|
|
|
console.log(`Updating user ${user._id}`)
|
|
|
|
const update = {
|
|
|
|
$set: {
|
|
|
|
'features.compileTimeout': newTimeout
|
|
|
|
}
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
db.users.update(
|
|
|
|
{
|
|
|
|
_id: user._id,
|
|
|
|
'features.compileTimeout': oldTimeoutLimits
|
|
|
|
},
|
|
|
|
update,
|
|
|
|
callback
|
|
|
|
)
|
2018-07-31 11:54:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const updateUsers = (users, callback) =>
|
2018-11-05 05:06:39 -05:00
|
|
|
async.eachLimit(users, ASYNC_LIMIT, updateUser, function(error) {
|
2018-07-31 11:54:09 -04:00
|
|
|
if (error) {
|
|
|
|
callback(error)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
counter += users.length
|
2018-08-02 09:14:23 -04:00
|
|
|
console.log(`${counter} users updated`)
|
2018-08-02 08:49:56 -04:00
|
|
|
if (DO_ALL) {
|
|
|
|
return loopForUsers(callback)
|
|
|
|
} else {
|
2018-08-02 09:06:51 -04:00
|
|
|
console.log('*** run again to continue updating ***')
|
2018-08-02 08:49:56 -04:00
|
|
|
return callback()
|
|
|
|
}
|
2018-07-31 11:54:09 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
var loopForUsers = callback =>
|
2018-11-05 05:06:39 -05:00
|
|
|
db.users
|
|
|
|
.find(
|
|
|
|
{ 'features.compileTimeout': oldTimeoutLimits },
|
|
|
|
{ 'features.compileTimeout': 1 }
|
|
|
|
)
|
|
|
|
.limit(FETCH_LIMIT, function(error, users) {
|
|
|
|
if (error) {
|
|
|
|
callback(error)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (users.length === 0) {
|
|
|
|
console.log(`DONE (${counter} users updated)`)
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
updateUsers(users, callback)
|
|
|
|
})
|
2018-07-31 11:54:09 -04:00
|
|
|
|
|
|
|
var counter = 0
|
|
|
|
var run = () =>
|
2018-11-05 05:06:39 -05:00
|
|
|
loopForUsers(function(error) {
|
|
|
|
if (error) {
|
|
|
|
throw error
|
|
|
|
}
|
2018-07-31 11:54:09 -04:00
|
|
|
process.exit()
|
|
|
|
})
|
|
|
|
|
2018-08-02 08:49:56 -04:00
|
|
|
let FETCH_LIMIT, ASYNC_LIMIT, DO_ALL
|
2018-11-05 05:06:39 -05:00
|
|
|
var setup = function() {
|
2018-07-31 11:54:09 -04:00
|
|
|
let args = minilist(process.argv.slice(2))
|
2018-08-02 08:49:56 -04:00
|
|
|
// --fetch N get N users each time
|
2018-11-05 05:06:39 -05:00
|
|
|
FETCH_LIMIT = args.fetch ? args.fetch : 100
|
2018-08-02 08:49:56 -04:00
|
|
|
// --async M run M updates in parallel
|
2018-11-05 05:06:39 -05:00
|
|
|
ASYNC_LIMIT = args.async ? args.async : 10
|
2018-08-02 08:49:56 -04:00
|
|
|
// --all means run to completion
|
2018-08-02 09:06:51 -04:00
|
|
|
if (args.all) {
|
|
|
|
if (args.fetch) {
|
|
|
|
console.error('error: do not use --fetch with --all')
|
|
|
|
process.exit(1)
|
|
|
|
} else {
|
|
|
|
DO_ALL = true
|
|
|
|
// if we are updating for all users then ignore the fetch limit.
|
|
|
|
FETCH_LIMIT = 0
|
|
|
|
// A limit() value of 0 (i.e. .limit(0)) is equivalent to setting
|
|
|
|
// no limit.
|
|
|
|
// https://docs.mongodb.com/manual/reference/method/cursor.limit
|
|
|
|
}
|
|
|
|
}
|
2018-07-31 11:54:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
setup()
|
|
|
|
run()
|