log progress without new line

This commit is contained in:
Issue哥 2020-06-30 11:43:39 +08:00
parent 27aed0b80d
commit 46a32b7dc7
2 changed files with 41 additions and 26 deletions

2
copy
View File

@ -42,7 +42,7 @@ if (validate_fid(source)) {
copy({ source, target, name, min_size, update, not_teamdrive, service_account }).then(folder => { copy({ source, target, name, min_size, update, not_teamdrive, service_account }).then(folder => {
if (!folder) return if (!folder) return
const link = 'https://drive.google.com/drive/folders/' + folder.id const link = 'https://drive.google.com/drive/folders/' + folder.id
console.log('任务完成,新文件夹链接:\n', link) console.log('\n任务完成,新文件夹链接:\n', link)
}) })
} else { } else {
console.warn('目录ID缺失或格式错误') console.warn('目录ID缺失或格式错误')

View File

@ -153,11 +153,10 @@ async function walk_and_save ({ fid, not_teamdrive, update, service_account }) {
const limit = pLimit(PARALLEL_LIMIT) const limit = pLimit(PARALLEL_LIMIT)
const loop = setInterval(() => { const loop = setInterval(() => {
console.log('================') const now = dayjs().format('HH:mm:ss')
console.log('已获取的对象数量', result.length) const message = `${now} | 已获取对象 ${result.length} | 排队等候的网络请求 ${limit.pendingCount}`
console.log('正在进行的网络请求', limit.activeCount) print_progress(message)
console.log('排队等候的目录数量', limit.pendingCount) }, 1000)
}, LOG_DELAY)
async function recur (parent) { async function recur (parent) {
let files, should_save let files, should_save
@ -181,8 +180,12 @@ async function walk_and_save ({ fid, not_teamdrive, update, service_account }) {
result.push(...files) result.push(...files)
return Promise.all(folders.map(v => recur(v.id))) return Promise.all(folders.map(v => recur(v.id)))
} }
await recur(fid) try {
console.log('信息获取完毕') await recur(fid)
} catch (e) {
console.error(e)
}
console.log('\n信息获取完毕')
not_finished.length ? console.log('未读取完毕的目录ID', JSON.stringify(not_finished)) : console.log('所有目录读取完毕') not_finished.length ? console.log('未读取完毕的目录ID', JSON.stringify(not_finished)) : console.log('所有目录读取完毕')
clearInterval(loop) clearInterval(loop)
const smy = summary(result) const smy = summary(result)
@ -483,23 +486,27 @@ async function real_copy ({ source, target, name, min_size, update, not_teamdriv
} }
async function copy_files ({ files, mapping, root, task_id }) { async function copy_files ({ files, mapping, root, task_id }) {
console.log('开始复制文件,总数:', files.length) console.log('\n开始复制文件,总数:', files.length)
const limit = pLimit(PARALLEL_LIMIT) const limit = pLimit(PARALLEL_LIMIT)
let count = 0 let count = 0
const loop = setInterval(() => { const loop = setInterval(() => {
console.log('================') const now = dayjs().format('HH:mm:ss')
console.log('已复制的文件数量', count) const message = `${now} | 已复制文件数 ${count} | 排队等候的网络请求 ${limit.pendingCount}`
console.log('正在进行的网络请求', limit.activeCount) print_progress(message)
console.log('排队等候的文件数量', limit.pendingCount) }, 1000)
}, LOG_DELAY)
await Promise.all(files.map(async file => { await Promise.all(files.map(async file => {
const { id, parent } = file try {
const target = mapping[parent] || root const { id, parent } = file
const new_file = await limit(() => copy_file(id, target)) const target = mapping[parent] || root
if (new_file) { const new_file = await limit(() => copy_file(id, target))
db.prepare('update task set status=?, copied = copied || ? where id=?').run('copying', id + '\n', task_id) if (new_file) {
db.prepare('update task set status=?, copied = copied || ? where id=?')
.run('copying', id + '\n', task_id)
}
count++
} catch (e) {
console.error(e)
} }
count++
})) }))
clearInterval(loop) clearInterval(loop)
} }
@ -552,11 +559,10 @@ async function create_folders ({ source, old_mapping, folders, root, task_id, se
let same_levels = folders.filter(v => v.parent === folders[0].parent) let same_levels = folders.filter(v => v.parent === folders[0].parent)
const loop = setInterval(() => { const loop = setInterval(() => {
console.log('================') const now = dayjs().format('HH:mm:ss')
console.log('已创建的文件夹数量', count) const message = `${now} | 已创建目录数 ${count} | 排队等候的网络请求 ${limit.pendingCount}`
console.log('正在进行的网络请求', limit.activeCount) print_progress(message)
console.log('排队等候的网络请求', limit.pendingCount) }, 1000)
}, LOG_DELAY)
while (same_levels.length) { while (same_levels.length) {
await Promise.all(same_levels.map(async v => { await Promise.all(same_levels.map(async v => {
@ -688,7 +694,16 @@ async function dedupe ({ fid, update, service_account }) {
function handle_error (err) { function handle_error (err) {
const data = err && err.response && err.response.data const data = err && err.response && err.response.data
data ? console.error(JSON.stringify(data)) : console.error(err.message) if (data) {
console.error(JSON.stringify(data))
} else {
if (!err.message.includes('timeout')) console.error(err.message)
}
}
function print_progress (msg) {
process.stdout.cursorTo(0)
process.stdout.write(msg)
} }
module.exports = { ls_folder, count, validate_fid, copy, dedupe, copy_file, gen_count_body, real_copy } module.exports = { ls_folder, count, validate_fid, copy, dedupe, copy_file, gen_count_body, real_copy }