From 0018627f82a6f4b8a392e119349be9dce4d6b3b8 Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Tue, 28 Jan 2020 10:48:07 -0500 Subject: [PATCH] Process brew compression in batches to reduce server load. --- client/admin/brewCompress/brewCompress.jsx | 32 +++++++++++++++++----- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/client/admin/brewCompress/brewCompress.jsx b/client/admin/brewCompress/brewCompress.jsx index a43fd52c2..d7c416405 100644 --- a/client/admin/brewCompress/brewCompress.jsx +++ b/client/admin/brewCompress/brewCompress.jsx @@ -12,7 +12,8 @@ const BrewCompress = createClass({ }, getInitialState() { return { - count : 0, + count : 0, + batchRange : 0, pending : false, primed : false, @@ -29,12 +30,26 @@ const BrewCompress = createClass({ .finally(()=>this.setState({ pending: false })); }, cleanup(){ + const brews = this.state.ids; + const compressBatches = ()=>{ + if(brews.length == 0){ + this.setState({ pending: false, primed: false }); + return; + } + const batch = brews.splice(0, 1000); // Process brews in batches of 1000 + this.setState({ batchRange: this.state.count - brews.length }); + batch.forEach((id, idx)=>{ + request.put(`/admin/compress/${id}`) + .catch((err)=>this.setState({ error: err })); + + console.log(`compresed brew ${id}`); + }); + setTimeout(compressBatches, 10000); //Wait 10 seconds between batches + }; + this.setState({ pending: true }); - this.state.ids.forEach((id)=>{ - request.put(`/admin/compress/${id}`) - .catch((err)=>this.setState({ error: err })) - .finally(()=>this.setState({ pending: false, primed: false })); - }); + + compressBatches(); }, renderPrimed(){ if(!this.state.primed) return; @@ -49,7 +64,10 @@ const BrewCompress = createClass({ : compress } - Found {this.state.count} Brews that could be compressed. + {this.state.pending + ? Compressing {this.state.batchRange} brews. + : Found {this.state.count} Brews that could be compressed. + } ; }, render(){