From dab22d5fd006ea1e25d3b2bb8fc98abea67e3c22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Fri, 18 Mar 2022 12:40:13 -0400 Subject: [PATCH] perf: #10410, faster upgrade script --- src/upgrades/1.19.3/fix_user_uploads_zset.js | 39 +++++++++----------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/src/upgrades/1.19.3/fix_user_uploads_zset.js b/src/upgrades/1.19.3/fix_user_uploads_zset.js index e5989ba75d..268509ff8b 100644 --- a/src/upgrades/1.19.3/fix_user_uploads_zset.js +++ b/src/upgrades/1.19.3/fix_user_uploads_zset.js @@ -1,5 +1,3 @@ -/* eslint-disable no-await-in-loop */ - 'use strict'; const crypto = require('crypto'); @@ -16,30 +14,29 @@ module.exports = { const { progress } = this; await batch.processSortedSet('users:joindate', async (uids) => { - const keys = uids.map(uid => `uid:${uid}:uploads`); progress.incr(uids.length); - for (let idx = 0; idx < uids.length; idx++) { - const key = keys[idx]; + await Promise.all(uids.map(async (uid) => { + const key = `uid:${uid}:uploads`; // Rename the paths within let uploads = await db.getSortedSetRangeWithScores(key, 0, -1); - - // Don't process those that have already the right format - uploads = uploads.filter(upload => upload.value.startsWith('/files/')); - - await db.sortedSetRemove(key, uploads.map(upload => upload.value)); - await db.sortedSetAdd( - key, - uploads.map(upload => upload.score), - uploads.map(upload => upload.value.slice(1)) - ); - - // Add uid to the upload's hash object - uploads = await db.getSortedSetMembers(key); - await db.setObjectBulk(uploads.map(relativePath => [`upload:${md5(relativePath)}`, { uid: uids[idx] }])); - } + if (uploads.length) { + // Don't process those that have already the right format + uploads = uploads.filter(upload => upload.value.startsWith('/files/')); + + await db.sortedSetRemove(key, uploads.map(upload => upload.value)); + await db.sortedSetAdd( + key, + uploads.map(upload => upload.score), + uploads.map(upload => upload.value.slice(1)) + ); + // Add uid to the upload's hash object + uploads = await db.getSortedSetMembers(key); + await db.setObjectBulk(uploads.map(relativePath => [`upload:${md5(relativePath)}`, { uid: uids[idx] }])); + } + })); }, { - batch: 100, + batch: 500, progress: progress, }); },