Revert "Added all db write methods to a queue."

This reverts commit 17f7e618ec.
This commit is contained in:
Ajay Ramachandran 2019-11-23 01:27:32 -05:00
parent 2a25e4a2d9
commit 9cf4a7ed55
2 changed files with 7 additions and 83 deletions

View file

@ -4,9 +4,6 @@ var http = require('http');
// Create a service (the app object is just a callback). // Create a service (the app object is just a callback).
var app = express(); var app = express();
//used to prevent database is busy errors
var writeQueue = require('./writeQueue');
//hashing service //hashing service
var crypto = require('crypto'); var crypto = require('crypto');
@ -199,8 +196,7 @@ app.get('/api/postVideoSponsorTimes', async function (req, res) {
if (row == null) { if (row == null) {
//not a duplicate, execute query //not a duplicate, execute query
let preparedStatement = db.prepare("INSERT INTO sponsorTimes VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)"); db.prepare("INSERT INTO sponsorTimes VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)").run(videoID, startTime, endTime, startingVotes, UUID, userID, timeSubmitted, 0, shadowBanned, function (err) {
let callback = function (err) {
if (err) { if (err) {
//a DB change probably occurred, respond as if it is a duplicate //a DB change probably occurred, respond as if it is a duplicate
res.sendStatus(409); res.sendStatus(409);
@ -208,14 +204,11 @@ app.get('/api/postVideoSponsorTimes', async function (req, res) {
console.log("Error when putting sponsorTime in the DB: " + videoID + ", " + startTime + ", " + "endTime" + ", " + userID); console.log("Error when putting sponsorTime in the DB: " + videoID + ", " + startTime + ", " + "endTime" + ", " + userID);
} else { } else {
//add to private db as well //add to private db as well
writeQueue.addToWriteQueue(new writeQueue.WriteQueue(privateDB.prepare("INSERT INTO sponsorTimes VALUES(?, ?, ?)")), [videoID, hashedIP, timeSubmitted]); privateDB.prepare("INSERT INTO sponsorTimes VALUES(?, ?, ?)").run(videoID, hashedIP, timeSubmitted);
res.sendStatus(200); res.sendStatus(200);
} }
}; });
writeQueue.addToWriteQueue(new writeQueue.WriteQueue(preparedStatement, [videoID, startTime, endTime, startingVotes, UUID, userID, timeSubmitted, 0, shadowBanned], callback));
} else { } else {
res.sendStatus(409); res.sendStatus(409);
} }
@ -311,7 +304,7 @@ app.get('/api/voteOnSponsorTime', function (req, res) {
if (votesRow != undefined) { if (votesRow != undefined) {
privateDB.prepare("UPDATE votes SET type = ? WHERE userID = ? AND UUID = ?").run(type, userID, UUID); privateDB.prepare("UPDATE votes SET type = ? WHERE userID = ? AND UUID = ?").run(type, userID, UUID);
} else { } else {
writeQueue.addToWriteQueue(new writeQueue.WriteQueue(privateDB.prepare("INSERT INTO votes VALUES(?, ?, ?, ?)")), [UUID, userID, hashedIP, type]); privateDB.prepare("INSERT INTO votes VALUES(?, ?, ?, ?)").run(UUID, userID, hashedIP, type);
} }
//update the vote count on this sponsorTime //update the vote count on this sponsorTime
@ -400,7 +393,7 @@ app.post('/api/setUsername', function (req, res) {
db.prepare("UPDATE userNames SET userName = ? WHERE userID = ?").run(userName, userID); db.prepare("UPDATE userNames SET userName = ? WHERE userID = ?").run(userName, userID);
} else { } else {
//add to the db //add to the db
writeQueue.addToWriteQueue(new writeQueue.WriteQueue(db.prepare("INSERT INTO userNames VALUES(?, ?)")), [userID, userName]); db.prepare("INSERT INTO userNames VALUES(?, ?)").run(userID, userName);
} }
res.sendStatus(200); res.sendStatus(200);
@ -480,7 +473,7 @@ app.post('/api/shadowBanUser', async function (req, res) {
//add them to the shadow ban list //add them to the shadow ban list
//add it to the table //add it to the table
writeQueue.addToWriteQueue(new writeQueue.WriteQueue(privateDB.prepare("INSERT INTO shadowBannedUsers VALUES(?)")), [userID]); privateDB.prepare("INSERT INTO shadowBannedUsers VALUES(?)").run(userID);
//find all previous submissions and hide them //find all previous submissions and hide them
db.prepare("UPDATE sponsorTimes SET shadowHidden = 1 WHERE userID = ?").run(userID); db.prepare("UPDATE sponsorTimes SET shadowHidden = 1 WHERE userID = ?").run(userID);
@ -531,7 +524,7 @@ app.post('/api/addUserAsVIP', async function (req, res) {
if (enabled && result.row.userCount == 0) { if (enabled && result.row.userCount == 0) {
//add them to the vip list //add them to the vip list
writeQueue.addToWriteQueue(new writeQueue.WriteQueue(db.prepare("INSERT INTO vipUsers VALUES(?)")), [userID]); db.prepare("INSERT INTO vipUsers VALUES(?)").run(userID);
} else if (!enabled && result.row.userCount > 0) { } else if (!enabled && result.row.userCount > 0) {
//remove them from the shadow ban list //remove them from the shadow ban list
db.prepare("DELETE FROM vipUsers WHERE userID = ?").run(userID); db.prepare("DELETE FROM vipUsers WHERE userID = ?").run(userID);

View file

@ -1,69 +0,0 @@
'use strict';
//used to queue objects to be written to the db
class WriteQueue {
/**
* @param {Statement} statement
* @param {Array} inputs
* @param {callback} callback
*/
constructor(statement, inputs, callback) {
this.statement = statement;
this.inputs = inputs;
this.callback = callback;
}
run() {
return new Promise((resolve, reject) => {
this.statement.run(this.inputs, (err) => this.end(err, resolve, reject));
});
}
end(err, resolve, reject) {
resolve();
if (this.callback) {
this.callback(err);
}
}
}
module.exports = {
WriteQueue,
addToWriteQueue
}
/**
* Array of class write queue
*
* @typedef WriteQueue[]
*/
var dbQueue = [];
//is a queue check currently running
var queueRunning = false;
/**
* Adds an item to the write queue and starts the run function if needed.
*
* @param {WriteQueue} item
*/
function addToWriteQueue(item) {
dbQueue.push(item);
if (!queueRunning) {
runThroughWriteQueue();
}
}
async function runThroughWriteQueue() {
queueRunning = true;
while (dbQueue.length > 0) {
await dbQueue[0].run();
dbQueue.splice(0, 1);
}
queueRunning = false;
}