fix: 无限循环处理

pull/635/head
bigfengyu 5 years ago
parent 886b16f037
commit e70f54687d

@ -1,44 +1,50 @@
// 补数据 token // 补数据 token
import nanoid = require("nanoid"); import nanoid = require("nanoid")
import sequelize from "../models/sequelize"; import sequelize from "../models/sequelize"
import { Repository } from "../models"; import { Repository } from "../models"
const chalk = require("chalk"); const chalk = require("chalk");
(async () => { (async () => {
sequelize.model(Repository); sequelize.model(Repository)
const cnt = await Repository.count({ const cnt = await Repository.count({
where: { where: {
// tslint:disable-next-line: no-null-keyword
token: null token: null
} }
}); })
const limit = 500
const iteration = 1
const limit = 500; console.log(`共有${cnt}条数据`)
console.log(`共有${cnt}条数据`); while (true) {
for (let offset = 0; offset < cnt; offset += limit) {
console.log(`正在处理offset ${offset}, limit ${limit}`);
const rows = await Repository.findAll({ const rows = await Repository.findAll({
where: { where: {
// tslint:disable-next-line: no-null-keyword
token: null token: null
}, },
limit, limit
offset })
}); console.log(`正在处理第 ${iteration} 轮, length ${rows.length}`)
if (rows.length === 0) {
console.log('全部处理完成')
break
}
await Promise.all( await Promise.all(
rows.map(async repo => { rows.map(async repo => {
if (!repo.token) { if (!repo.token) {
repo.token = nanoid(32); repo.token = nanoid(32)
await repo.save(); await repo.save()
console.log( console.log(
chalk.green(repo.name + "添加了默认token" + repo.token) chalk.green(repo.name + "添加了默认token" + repo.token)
); )
} }
}) })
); )
} }
process.exit(0) process.exit(0)
})(); })()

Loading…
Cancel
Save