From a07509f770f297b6bb4a9a62ec88fea50c0463a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Wed, 24 Feb 2021 21:16:43 -0500 Subject: [PATCH] perf: make upgrade script faster --- src/upgrades/1.12.1/post_upload_sizes.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/upgrades/1.12.1/post_upload_sizes.js b/src/upgrades/1.12.1/post_upload_sizes.js index 1f91a16a79..37df4e70c8 100644 --- a/src/upgrades/1.12.1/post_upload_sizes.js +++ b/src/upgrades/1.12.1/post_upload_sizes.js @@ -1,24 +1,23 @@ 'use strict'; -const async = require('async'); - const batch = require('../../batch'); const posts = require('../../posts'); +const db = require('../../database'); module.exports = { name: 'Calculate image sizes of all uploaded images', timestamp: Date.UTC(2019, 2, 16), - method: function (callback) { + method: async function () { const { progress } = this; - batch.processSortedSet('posts:pid', (postData, next) => { - async.eachSeries(postData, async (pid) => { - const uploads = await posts.uploads.list(pid); - await posts.uploads.saveSize(uploads); - progress.incr(); - }, next); + await batch.processSortedSet('posts:pid', async (postData) => { + const keys = postData.map(p => `post:${p.pid}:uploads`); + const uploads = await db.getSortedSetRange(keys, 0, -1); + await posts.uploads.saveSize(uploads); + progress.incr(postData.length); }, { + batch: 100, progress: progress, - }, callback); + }); }, };