From 46a32b7dc7ab4c51584c27e7d92da74e8c6a7595 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Issue=E5=93=A5?= Date: Tue, 30 Jun 2020 11:43:39 +0800 Subject: [PATCH] log progress without new line --- copy | 2 +- src/gd.js | 65 ++++++++++++++++++++++++++++++++++--------------------- 2 files changed, 41 insertions(+), 26 deletions(-) diff --git a/copy b/copy index 4c25230..f93be78 100755 --- a/copy +++ b/copy @@ -42,7 +42,7 @@ if (validate_fid(source)) { copy({ source, target, name, min_size, update, not_teamdrive, service_account }).then(folder => { if (!folder) return const link = 'https://drive.google.com/drive/folders/' + folder.id - console.log('任务完成,新文件夹链接:\n', link) + console.log('\n任务完成,新文件夹链接:\n', link) }) } else { console.warn('目录ID缺失或格式错误') diff --git a/src/gd.js b/src/gd.js index 646b791..a2f1830 100644 --- a/src/gd.js +++ b/src/gd.js @@ -153,11 +153,10 @@ async function walk_and_save ({ fid, not_teamdrive, update, service_account }) { const limit = pLimit(PARALLEL_LIMIT) const loop = setInterval(() => { - console.log('================') - console.log('已获取的对象数量', result.length) - console.log('正在进行的网络请求', limit.activeCount) - console.log('排队等候的目录数量', limit.pendingCount) - }, LOG_DELAY) + const now = dayjs().format('HH:mm:ss') + const message = `${now} | 已获取对象 ${result.length} | 排队等候的网络请求 ${limit.pendingCount}` + print_progress(message) + }, 1000) async function recur (parent) { let files, should_save @@ -181,8 +180,12 @@ async function walk_and_save ({ fid, not_teamdrive, update, service_account }) { result.push(...files) return Promise.all(folders.map(v => recur(v.id))) } - await recur(fid) - console.log('信息获取完毕') + try { + await recur(fid) + } catch (e) { + console.error(e) + } + console.log('\n信息获取完毕') not_finished.length ? console.log('未读取完毕的目录ID:', JSON.stringify(not_finished)) : console.log('所有目录读取完毕') clearInterval(loop) 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 }) { - console.log('开始复制文件,总数:', files.length) + console.log('\n开始复制文件,总数:', files.length) const limit = pLimit(PARALLEL_LIMIT) let count = 0 const loop = setInterval(() => { - console.log('================') - console.log('已复制的文件数量', count) - console.log('正在进行的网络请求', limit.activeCount) - console.log('排队等候的文件数量', limit.pendingCount) - }, LOG_DELAY) + const now = dayjs().format('HH:mm:ss') + const message = `${now} | 已复制文件数 ${count} | 排队等候的网络请求 ${limit.pendingCount}` + print_progress(message) + }, 1000) await Promise.all(files.map(async file => { - const { id, parent } = file - const target = mapping[parent] || root - const new_file = await limit(() => copy_file(id, target)) - if (new_file) { - db.prepare('update task set status=?, copied = copied || ? where id=?').run('copying', id + '\n', task_id) + try { + const { id, parent } = file + const target = mapping[parent] || root + const new_file = await limit(() => copy_file(id, target)) + 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) } @@ -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) const loop = setInterval(() => { - console.log('================') - console.log('已创建的文件夹数量', count) - console.log('正在进行的网络请求', limit.activeCount) - console.log('排队等候的网络请求', limit.pendingCount) - }, LOG_DELAY) + const now = dayjs().format('HH:mm:ss') + const message = `${now} | 已创建目录数 ${count} | 排队等候的网络请求 ${limit.pendingCount}` + print_progress(message) + }, 1000) while (same_levels.length) { await Promise.all(same_levels.map(async v => { @@ -688,7 +694,16 @@ async function dedupe ({ fid, update, service_account }) { function handle_error (err) { 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 }