utility scripts: converted to use the Promise interface

This commit is contained in:
Ray Bellis 2019-01-26 23:52:02 +00:00
parent e7dc0766fd
commit 7709fd46e5
4 changed files with 202 additions and 318 deletions

View file

@ -7,128 +7,88 @@ if (process.argv.length != 2) {
process.exit(1);
}
// initialize the variables
var db, settings, padManager;
var npm = require('../src/node_modules/npm');
var async = require('../src/node_modules/async');
// load and initialize NPM
let npm = require('../src/node_modules/npm');
npm.load({}, async function() {
var Changeset = require('../src/static/js/Changeset');
async.series([
// load npm
function(callback) {
npm.load({}, callback);
},
try {
// initialize the database
let settings = require('../src/node/utils/Settings');
let db = require('../src/node/db/DB');
await db.init();
// load modules
function(callback) {
settings = require('../src/node/utils/Settings');
db = require('../src/node/db/DB');
let Changeset = require('../src/static/js/Changeset');
let padManager = require('../src/node/db/PadManager');
// initialize the database
db.init(callback);
},
// get all pads
let res = await padManager.listAllPads();
// load pads
function (callback) {
padManager = require('../src/node/db/PadManager');
for (let padId of res.padIDs) {
padManager.listAllPads(function(err, res) {
padIds = res.padIDs;
callback(err);
});
},
function (callback) {
async.forEach(padIds, function(padId, callback) {
padManager.getPad(padId, function(err, pad) {
if (err) {
callback(err);
}
let pad = await padManager.getPad(padId);
// check if the pad has a pool
if (pad.pool === undefined) {
console.error("[" + pad.id + "] Missing attribute pool");
callback();
return;
continue;
}
// create an array with key kevisions
// key revisions always save the full pad atext
var head = pad.getHeadRevisionNumber();
var keyRevisions = [];
for (var i = 0; i < head; i += 100) {
keyRevisions.push(i);
let head = pad.getHeadRevisionNumber();
let keyRevisions = [];
for (let rev = 0; rev < head; rev += 100) {
keyRevisions.push(rev);
}
// run through all key revisions
async.forEachSeries(keyRevisions, function(keyRev, callback) {
for (let keyRev of keyRevisions) {
// create an array of revisions we need till the next keyRevision or the End
var revisionsNeeded = [];
for(var i = keyRev; i <= keyRev + 100 && i <= head; i++) {
revisionsNeeded.push(i);
for (let rev = keyRev ; rev <= keyRev + 100 && rev <= head; rev++) {
revisionsNeeded.push(rev);
}
// this array will hold all revision changesets
var revisions = [];
// run through all needed revisions and get them from the database
async.forEach(revisionsNeeded, function(revNum, callback) {
db.db.get("pad:" + pad.id + ":revs:" + revNum, function(err, revision) {
for (let revNum of revisionsNeeded) {
let revision = await db.get("pad:" + pad.id + ":revs:" + revNum);
revisions[revNum] = revision;
callback(err);
});
},
function(err) {
if (err) {
callback(err);
return;
}
// check if the revision exists
if (revisions[keyRev] == null) {
console.error("[" + pad.id + "] Missing revision " + keyRev);
callback();
return;
continue;
}
// check if there is a atext in the keyRevisions
if (revisions[keyRev].meta === undefined || revisions[keyRev].meta.atext === undefined) {
console.error("[" + pad.id + "] Missing atext in revision " + keyRev);
callback();
return;
continue;
}
var apool = pad.pool;
var atext = revisions[keyRev].meta.atext;
let apool = pad.pool;
let atext = revisions[keyRev].meta.atext;
for(var i = keyRev + 1; i <= keyRev + 100 && i <= head; i++) {
for (let rev = keyRev + 1; rev <= keyRev + 100 && rev <= head; rev++) {
try {
// console.log("[" + pad.id + "] check revision " + i);
var cs = revisions[i].changeset;
let cs = revisions[rev].changeset;
atext = Changeset.applyToAText(cs, atext, apool);
} catch (e) {
console.error("[" + pad.id + "] Bad changeset at revision " + i + " - " + e.message);
callback();
return;
}
}
callback();
});
}, callback);
});
}, callback);
}
],
function (err) {
if (err) {
throw err;
} else {
console.log("finished");
process.exit(0);
}
} catch (err) {
console.trace(err);
process.exit(1);
}
});

View file

@ -8,80 +8,55 @@ if (process.argv.length != 3) {
}
// get the padID
var padId = process.argv[2];
const padId = process.argv[2];
// initialize the variables
var db, settings, padManager;
var npm = require('../src/node_modules/npm');
var async = require('../src/node_modules/async');
// load and initialize NPM;
let npm = require('../src/node_modules/npm');
npm.load({}, async function() {
var Changeset = require('ep_etherpad-lite/static/js/Changeset');
async.series([
// load npm
function(callback) {
npm.load({}, function(er) {
callback(er);
});
},
try {
// initialize database
let settings = require('../src/node/utils/Settings');
let db = require('../src/node/db/DB');
await db.init();
// load modules
function(callback) {
settings = require('../src/node/utils/Settings');
db = require('../src/node/db/DB');
let Changeset = require('ep_etherpad-lite/static/js/Changeset');
let padManager = require('../src/node/db/PadManager');
// initialize the database
db.init(callback);
},
// get the pad
function (callback) {
padManager = require('../src/node/db/PadManager');
padManager.doesPadExists(padId, function(err, exists) {
let exists = await padManager.doesPadExists(padId);
if (!exists) {
console.error("Pad does not exist");
process.exit(1);
}
padManager.getPad(padId, function(err, _pad) {
pad = _pad;
callback(err);
});
});
},
// get the pad
let pad = await padManager.getPad(padId);
function (callback) {
// create an array with key revisions
// key revisions always save the full pad atext
var head = pad.getHeadRevisionNumber();
var keyRevisions = [];
for (var i = 0; i < head; i += 100) {
keyRevisions.push(i);
let head = pad.getHeadRevisionNumber();
let keyRevisions = [];
for (let rev = 0; rev < head; rev += 100) {
keyRevisions.push(rev);
}
// run through all key revisions
async.forEachSeries(keyRevisions, function(keyRev, callback) {
for (let keyRev of keyRevisions) {
// create an array of revisions we need till the next keyRevision or the End
var revisionsNeeded = [];
for(var i = keyRev; i <= keyRev + 100 && i <= head; i++) {
revisionsNeeded.push(i);
let revisionsNeeded = [];
for (let rev = keyRev; rev <= keyRev + 100 && rev <= head; rev++) {
revisionsNeeded.push(rev);
}
// this array will hold all revision changesets
var revisions = [];
// run through all needed revisions and get them from the database
async.forEach(revisionsNeeded, function(revNum, callback) {
db.db.get("pad:" + padId + ":revs:" + revNum, function(err, revision) {
for (let revNum of revisionsNeeded) {
let revision = await db.get("pad:" + padId + ":revs:" + revNum);
revisions[revNum] = revision;
callback(err);
});
},
function(err) {
if (err) {
callback(err);
return;
}
// check if the pad has a pool
@ -93,35 +68,28 @@ async.series([
// check if there is an atext in the keyRevisions
if (revisions[keyRev] === undefined || revisions[keyRev].meta === undefined || revisions[keyRev].meta.atext === undefined) {
console.error("No atext in key revision " + keyRev);
callback();
return;
continue;
}
var apool = pad.pool;
var atext = revisions[keyRev].meta.atext;
let apool = pad.pool;
let atext = revisions[keyRev].meta.atext;
for (var i = keyRev + 1; i <= keyRev + 100 && i <= head; i++) {
for (let rev = keyRev + 1; rev <= keyRev + 100 && rev <= head; rev++) {
try {
// console.log("check revision " + i);
var cs = revisions[i].changeset;
// console.log("check revision " + rev);
let cs = revisions[rev].changeset;
atext = Changeset.applyToAText(cs, atext, apool);
} catch(e) {
console.error("Bad changeset at revision " + i + " - " + e.message);
callback();
return;
console.error("Bad changeset at revision " + rev + " - " + e.message);
continue;
}
}
callback();
});
}, callback);
}
],
function (err) {
if(err) {
throw err;
} else {
console.log("finished");
process.exit(0);
}
} catch (e) {
console.trace(e);
process.exit(1);
}
});

View file

@ -9,55 +9,33 @@ if (process.argv.length != 3) {
}
// get the padID
var padId = process.argv[2];
let padId = process.argv[2];
var db, padManager, pad, settings;
var neededDBValues = ["pad:"+padId];
let npm = require('../src/node_modules/npm');
var npm = require('../src/node_modules/npm');
var async = require('../src/node_modules/async');
async.series([
// load npm
function(callback) {
npm.load({}, function(er) {
npm.load({}, async function(er) {
if (er) {
console.error("Could not load NPM: " + er)
process.exit(1);
} else {
callback();
}
});
},
// load modules
function(callback) {
settings = require('../src/node/utils/Settings');
db = require('../src/node/db/DB');
callback();
},
try {
let settings = require('../src/node/utils/Settings');
let db = require('../src/node/db/DB');
await db.init();
// initialize the database
function (callback) {
db.init(callback);
},
// delete the pad and its links
function (callback) {
padManager = require('../src/node/db/PadManager');
await padManager.removePad(padId);
padManager.removePad(padId, function(err){
callback(err);
});
callback();
}
],
function (err) {
if(err) {
throw err;
} else {
console.log("Finished deleting padId: " + padId);
process.exit();
process.exit(0);
} catch (e) {
if (err.name === "apierror") {
console.error(e);
} else {
console.trace(e);
}
process.exit(1);
}
});

View file

@ -10,88 +10,66 @@ if (process.argv.length != 3) {
}
// get the padID
var padId = process.argv[2];
let padId = process.argv[2];
var db, dirty, padManager, pad, settings;
var neededDBValues = ["pad:"+padId];
let npm = require('../src/node_modules/npm');
var npm = require('../node_modules/ep_etherpad-lite/node_modules/npm');
var async = require('../node_modules/ep_etherpad-lite/node_modules/async');
async.series([
// load npm
function(callback) {
npm.load({}, function(er) {
npm.load({}, async function(er) {
if (er) {
console.error("Could not load NPM: " + er)
process.exit(1);
} else {
callback();
}
})
},
// load modules
function(callback) {
settings = require('../node_modules/ep_etherpad-lite/node/utils/Settings');
db = require('../node_modules/ep_etherpad-lite/node/db/DB');
dirty = require('../node_modules/ep_etherpad-lite/node_modules/ueberDB/node_modules/dirty')(padId + ".db");
callback();
},
try {
// initialize database
let settings = require('../src/node/utils/Settings');
let db = require('../src/node/db/DB');
await db.init();
// initialize the database
function (callback) {
db.init(callback);
},
// load extra modules
let dirtyDB = require('../src/node_modules/dirty');
let padManager = require('../src/node/db/PadManager');
let util = require('util');
// get the pad
function (callback) {
padManager = require('../node_modules/ep_etherpad-lite/node/db/PadManager');
// initialize output database
let dirty = dirtyDB(padId + '.db');
padManager.getPad(padId, function(err, _pad) {
pad = _pad;
callback(err);
});
},
// Promise wrapped get and set function
let wrapped = db.db.db.wrappedDB;
let get = util.promisify(wrapped.get.bind(wrapped));
let set = util.promisify(dirty.set.bind(dirty));
// array in which required key values will be accumulated
let neededDBValues = ['pad:' + padId];
// get the actual pad object
let pad = await padManager.getPad(padId);
function (callback) {
// add all authors
var authors = pad.getAllAuthors();
for (var i = 0; i < authors.length; i++) {
neededDBValues.push('globalAuthor:' + authors[i]);
}
neededDBValues.push(...pad.getAllAuthors().map(author => 'globalAuthor:' + author));
// add all revisions
var revHead = pad.head;
for (var i = 0; i <= revHead; i++) {
neededDBValues.push('pad:' + padId + ':revs:' + i);
for (let rev = 0; rev <= pad.head; ++rev) {
neededDBValues.push('pad:' + padId + ':revs:' + rev);
}
// get all chat values
var chatHead = pad.chatHead;
for (var i = 0; i <= chatHead; i++) {
neededDBValues.push('pad:' + padId + ':chat:' + i);
// add all chat values
for (let chat = 0; chat <= pad.chatHead; ++chat) {
neededDBValues.push('pad:' + padId + ':chat:' + chat);
}
// get and set all values
async.forEach(neededDBValues, function(dbkey, callback) {
db.db.db.wrappedDB.get(dbkey, function(err, dbvalue) {
if (err) { callback(err); return}
if (dbvalue && typeof dbvalue != 'object') {
dbvalue = JSON.parse(dbvalue); // if it's not json then parse it as json
for (let dbkey of neededDBValues) {
let dbvalue = await get(dbkey);
if (dbvalue && typeof dbvalue !== 'object') {
dbvalue = JSON.parse(dbvalue);
}
await set(dbkey, dbvalue);
}
dirty.set(dbkey, dbvalue, callback);
});
}, callback);
}
],
function (err) {
if (err) {
throw err;
} else {
console.log("finished");
process.exit();
console.log('finished');
process.exit(0);
} catch (er) {
console.error(er);
process.exit(1);
}
});