0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-16 01:42:47 +00:00

Process brew compression in batches to reduce server load.

This commit is contained in:
Trevor Buckner
2020-01-28 10:48:07 -05:00
parent e50f0a1f3b
commit 0018627f82

View File

@@ -13,6 +13,7 @@ const BrewCompress = createClass({
getInitialState() { getInitialState() {
return { return {
count : 0, count : 0,
batchRange : 0,
pending : false, pending : false,
primed : false, primed : false,
@@ -29,12 +30,26 @@ const BrewCompress = createClass({
.finally(()=>this.setState({ pending: false })); .finally(()=>this.setState({ pending: false }));
}, },
cleanup(){ cleanup(){
this.setState({ pending: true }); const brews = this.state.ids;
this.state.ids.forEach((id)=>{ 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}`) request.put(`/admin/compress/${id}`)
.catch((err)=>this.setState({ error: err })) .catch((err)=>this.setState({ error: err }));
.finally(()=>this.setState({ pending: false, primed: false }));
console.log(`compresed brew ${id}`);
}); });
setTimeout(compressBatches, 10000); //Wait 10 seconds between batches
};
this.setState({ pending: true });
compressBatches();
}, },
renderPrimed(){ renderPrimed(){
if(!this.state.primed) return; if(!this.state.primed) return;
@@ -49,7 +64,10 @@ const BrewCompress = createClass({
: <span><i className='fa fa-compress' /> compress </span> : <span><i className='fa fa-compress' /> compress </span>
} }
</button> </button>
<span>Found {this.state.count} Brews that could be compressed. </span> {this.state.pending
? <span>Compressing {this.state.batchRange} brews. </span>
: <span>Found {this.state.count} Brews that could be compressed. </span>
}
</div>; </div>;
}, },
render(){ render(){