|
|
|
@ -140,28 +140,36 @@ var Path = require("path");
|
|
|
|
|
var readFileBin = require("./stream-file").readFileBin;
|
|
|
|
|
var Util = require("./common-util");
|
|
|
|
|
var Schedule = require("./schedule");
|
|
|
|
|
var Fse = require("fs-extra");
|
|
|
|
|
var nThen = require("nthen");
|
|
|
|
|
|
|
|
|
|
Decrees.load = function (Env, cb) {
|
|
|
|
|
Env.scheduleDecree = Env.scheduleDecree || Schedule();
|
|
|
|
|
|
|
|
|
|
var decreeName = Path.join(Env.paths.decree, 'decree.ndjson'); // XXX mkdirp
|
|
|
|
|
|
|
|
|
|
var stream = Fs.createReadStream(decreeName, {start: 0});
|
|
|
|
|
|
|
|
|
|
var handler = Decrees.createLineHandler(Env);
|
|
|
|
|
|
|
|
|
|
Env.scheduleDecree.blocking('', function (unblock) {
|
|
|
|
|
var done = Util.once(Util.both(cb, unblock));
|
|
|
|
|
readFileBin(stream, function (msgObj, next) {
|
|
|
|
|
var text = msgObj.buff.toString('utf8');
|
|
|
|
|
try {
|
|
|
|
|
handler(void 0, JSON.parse(text));
|
|
|
|
|
} catch (err) {
|
|
|
|
|
handler(err);
|
|
|
|
|
}
|
|
|
|
|
next();
|
|
|
|
|
}, function (err) {
|
|
|
|
|
done(err);
|
|
|
|
|
nThen(function (w) {
|
|
|
|
|
// ensure that the path to the decree log exists
|
|
|
|
|
Fse.mkdirp(Env.paths.decree, w(function (err) {
|
|
|
|
|
if (!err) { return; }
|
|
|
|
|
w.abort();
|
|
|
|
|
done(err);
|
|
|
|
|
}));
|
|
|
|
|
}).nThen(function () {
|
|
|
|
|
var decreeName = Path.join(Env.paths.decree, 'decree.ndjson');
|
|
|
|
|
var stream = Fs.createReadStream(decreeName, {start: 0});
|
|
|
|
|
var handler = Decrees.createLineHandler(Env);
|
|
|
|
|
readFileBin(stream, function (msgObj, next) {
|
|
|
|
|
var text = msgObj.buff.toString('utf8');
|
|
|
|
|
try {
|
|
|
|
|
handler(void 0, JSON.parse(text));
|
|
|
|
|
} catch (err) {
|
|
|
|
|
handler(err);
|
|
|
|
|
}
|
|
|
|
|
next();
|
|
|
|
|
}, function (err) {
|
|
|
|
|
done(err);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|