change incrementation loop
This commit is contained in:
parent
f3a86ed544
commit
e6842dd262
|
@ -211,35 +211,45 @@ const incrementQueue = [];
|
||||||
let pushLoop = null;
|
let pushLoop = null;
|
||||||
const incrementLoop = async () => {
|
const incrementLoop = async () => {
|
||||||
if (!incrementQueue.length) {
|
if (!incrementQueue.length) {
|
||||||
clearInterval(pushLoop);
|
|
||||||
pushLoop = null;
|
pushLoop = null;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
sequelize.transaction(async (t) => {
|
await sequelize.transaction(async (t) => {
|
||||||
while (incrementQueue.length) {
|
while (incrementQueue.length) {
|
||||||
const [model, amount] = incrementQueue.pop();
|
const [model, amount] = incrementQueue.pop();
|
||||||
// eslint-disable-next-line no-await-in-loop
|
if (model) {
|
||||||
await model.increment(
|
// eslint-disable-next-line no-await-in-loop
|
||||||
['totalPixels', 'dailyTotalPixels'],
|
await model.increment(
|
||||||
{ by: amount, transaction: t },
|
['totalPixels', 'dailyTotalPixels'],
|
||||||
);
|
{ by: amount, transaction: t },
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.warn(`Error on batched incrementing pixelcounts: ${err.message}`);
|
logger.warn(`Error on batched incrementing pixelcounts: ${err.message}`);
|
||||||
}
|
}
|
||||||
|
pushLoop = setTimeout(incrementLoop, 50);
|
||||||
};
|
};
|
||||||
// TODO remove this after testing
|
// TODO remove this after testing
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.log('INCREMENTATION QUEUE SIZE', incrementQueue.length);
|
console.log('INCREMENTATION QUEUE SIZE', incrementQueue.length, pushLoop);
|
||||||
}, 300000);
|
}, 300000);
|
||||||
export async function incrementPixelcount(model, amount) {
|
export async function incrementPixelcount(model, amount) {
|
||||||
incrementQueue.push([model, amount]);
|
if (!model) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const exists = incrementQueue.find((q) => q[0] === model);
|
||||||
|
if (exists) {
|
||||||
|
exists[1] += amount;
|
||||||
|
} else {
|
||||||
|
incrementQueue.push([model, amount]);
|
||||||
|
}
|
||||||
if (!pushLoop) {
|
if (!pushLoop) {
|
||||||
pushLoop = setInterval(incrementLoop, 250);
|
pushLoop = setTimeout(incrementLoop, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user