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

View file

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

View file

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