botpress
Version:
The world's first CMS for bots. Easily create, manage and extend chatbots.
144 lines (118 loc) • 4.66 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _bluebird = require('bluebird');
var _bluebird2 = _interopRequireDefault(_bluebird);
var _nanoid = require('nanoid');
var _nanoid2 = _interopRequireDefault(_nanoid);
var _moment = require('moment');
var _moment2 = _interopRequireDefault(_moment);
var _ms = require('ms');
var _ms2 = _interopRequireDefault(_ms);
var _lodash = require('lodash');
var _helpers = require('../database/helpers');
var _helpers2 = _interopRequireDefault(_helpers);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new _bluebird2.default(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return _bluebird2.default.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
const DEFAULTS = {
timestampColumn: 'created_on'
/** The DB Janitor is the component that
automatically clears old records from the specific tables
according to the configuration.
@namespace DbJanitor
@example
bp.janitor.add({...})
*/
};const createJanitor = ({ db, logger, intervalMs = (0, _ms2.default)('1m') }) => {
const tasks = [];
let currentPromise = null;
// TODO: impplement `debuounce` param which, when set,
// prevents the specific task form running too often
// The goal is to have the interval reasonably low (1/5/10s)
// for some tasks like dialog sessions
// but don't run other tasks like logs more often than every 1/5/10min
const runTask = (() => {
var _ref = _asyncToGenerator(function* ({ table, ttl, timestampColumn }) {
logger.debug(`[DB Janitor] Running for table "${table}"`);
const knex = yield db.get();
const outdatedCondition = (0, _helpers2.default)(knex).date.isBefore(timestampColumn, (0, _moment2.default)().subtract(ttl, 'ms'));
return knex(table).where(outdatedCondition).del().then();
});
return function runTask(_x) {
return _ref.apply(this, arguments);
};
})();
const runTasks = () => {
logger.debug('[DB Janitor] Running tasks');
if (currentPromise) {
// don't run the tasks if the previous batch didn't finish yet
logger.debug('[DB Janitor] Skipping the current run, previous operation still running');
return;
}
currentPromise = _bluebird2.default.each(tasks, runTask).catch(err => {
logger.error('[DB Janitor] Error:', err.message);
}).finally(() => {
currentPromise = null;
});
};
let intervalId = null;
/**
* Start the daemon that will keep checking the DB and delete
* the outdated records according to the config,
* see {@link DbJanitor#add}.
* @function DbJanitor#start
* @returns {void}
*/
const start = () => {
if (intervalId) {
return;
}
intervalId = setInterval(runTasks, intervalMs);
logger.info('[DB Janitor] Started');
};
/**
* Add the table for the janitor to keep watching and cleaning.
* @function DbJanitor#add
* @param {object} options
* @param {string} options.table The name of the DB table to watch.
* @param {number} options.ttl Records Time to Live in **milliseconds**.
* @param {string} [options.timestampColumn="created_on"] The column
* to check if the record is outdated.
* @returns {string} The id of the added task.
*/
const add = options => {
logger.debug(`[DB Janitor] Added table "${options.table}"`);
const id = (0, _nanoid2.default)();
tasks.push(Object.assign({ id }, DEFAULTS, options));
return id;
};
/**
* Remove the table for the janitor to keep watching and cleaning.
* @function DbJanitor#remove
* @param {string} id The ID of the task returned by {@link DbJanitor#add}.
* @returns {void}
*/
const remove = id => {
const i = (0, _lodash.findIndex)(tasks, { id });
if (i < 0) {
logger.error(`[DB Janitor] Unknown task ID "${id}"`);
return;
}
const [{ table }] = tasks.splice(i, 1);
logger.debug(`[DB Janitor] Removed table "${table}"`);
};
/**
* Stop the daemon.
* @function DbJanitor#stop
* @returns {void}
*/
const stop = () => {
clearInterval(intervalId);
intervalId = null;
logger.info('[DB Janitor] Stopped');
};
return { start, add, remove, stop };
};
exports.default = createJanitor;
//# sourceMappingURL=index.js.map