From 6ec51715188d72b9c27aa1d1199315d453387174 Mon Sep 17 00:00:00 2001 From: ansuz Date: Tue, 13 Oct 2020 10:56:40 +0530 Subject: [PATCH] add support for changing a few more Env parameters at runtime --- lib/commands/admin-rpc.js | 12 +++++- lib/decrees.js | 88 +++++++++++++++++++++++++++------------ lib/historyKeeper.js | 5 +-- 3 files changed, 73 insertions(+), 32 deletions(-) diff --git a/lib/commands/admin-rpc.js b/lib/commands/admin-rpc.js index a97ffeb2b..4f0ee6fcc 100644 --- a/lib/commands/admin-rpc.js +++ b/lib/commands/admin-rpc.js @@ -207,13 +207,14 @@ the server adds two pieces of information to the supplied decree: } if (!changed) { return void cb(); } + Env.Log.info('ADMIN_DECREE', decree); Decrees.write(Env, decree, cb); }; // CryptPad_AsyncStore.rpc.send('ADMIN', ['INSTANCE_STATUS], console.log) var instanceStatus = function (Env, Server, cb) { cb(void 0, { - restrictRegistration: Boolean(Env.restrictRegistration), + restrictRegistration: Env.restrictRegistration, launchTime: Env.launchTime, currentTime: +new Date(), @@ -223,7 +224,14 @@ var instanceStatus = function (Env, Server, cb) { defaultStorageLimit: Env.defaultStorageLimit, lastEviction: Env.lastEviction, - knownActiveAccounts: Env.knownActiveAccounts, + // FIXME eviction is run in a worker and this isn't returned + //knownActiveAccounts: Env.knownActiveAccounts, + disableIntegratedEviction: Env.disableIntegratedEviction, + disableIntegratedTasks: Env.disableIntegratedTasks, + + maxUploadSize: Env.maxUploadSize, + premiumUploadSize: Env.premiumUploadSize, + lastEviction: Env.lastEviction, }); }; diff --git a/lib/decrees.js b/lib/decrees.js index e8b83f9d8..7e5864fd5 100644 --- a/lib/decrees.js +++ b/lib/decrees.js @@ -11,6 +11,13 @@ UPDATE_DEFAULT_STORAGE() SET_QUOTA(, limit) RM_QUOTA() +SET_MAX_UPLOAD_SIZE +SET_PREMIUM_UPLOAD_SIZE + +// BACKGROUND PROCESSES +DISABLE_INTEGRATED_TASKS +DISABLE_INTEGRATED_EVICTION + NOT IMPLEMENTED: // RESTRICTED REGISTRATION @@ -24,18 +31,9 @@ UPDATE_ACCOUNT_RETENTION_TIME UPDATE_ARCHIVE_RETENTION_TIME // 3.0 -UPDATE_MAX_UPLOAD_SIZE -UPDATE_PREMIUM_UPLOAD_SIZE - -// 4.0 Env.adminEmail Env.supportMailbox Env.DEV_MODE || Env.FRESH_MODE, -Env.maxUploadSize -Env.premiumUploadSize -Env.disableIntegratedTasks -Env.disableIntegratedEviction - */ var commands = {}; @@ -52,33 +50,69 @@ var commands = {}; */ +var args_isBoolean = function (args) { + return !(!Array.isArray(args) || typeof(args[0]) !== 'boolean'); +}; + // Toggles a simple boolean -// CryptPad_AsyncStore.rpc.send('ADMIN', [ 'ADMIN_DECREE', ['RESTRICT_REGISTRATION', [true]]], console.log) -commands.RESTRICT_REGISTRATION = function (Env, args) { - if (!Array.isArray(args) || typeof(args[0]) !== 'boolean') { - throw new Error('INVALID_ARGS'); - } - var bool = args[0]; - if (bool === Env.restrictRegistration) { return false; } - Env.restrictRegistration = bool; - return true; +var makeBooleanSetter = function (attr) { + return function (Env, args) { + if (!args_isBoolean(args)) { + throw new Error('INVALID_ARGS'); + } + var bool = args[0]; + if (bool === Env[attr]) { return false; } + Env[attr] = bool; + return true; + }; }; +// CryptPad_AsyncStore.rpc.send('ADMIN', [ 'ADMIN_DECREE', ['RESTRICT_REGISTRATION', [true]]], console.log) +commands.RESTRICT_REGISTRATION = makeBooleanSetter('restrictRegistration'); + +// CryptPad_AsyncStore.rpc.send('ADMIN', [ 'ADMIN_DECREE', ['DISABLE_INTEGRATED_EVICTION', [true]]], console.log) +commands.DISABLE_INTEGRATED_EVICTION = makeBooleanSetter('disableIntegratedEviction'); + +// CryptPad_AsyncStore.rpc.send('ADMIN', [ 'ADMIN_DECREE', ['DISABLE_INTEGRATED_TASKS', [true]]], console.log) +commands.DISABLE_INTEGRATED_TASKS = makeBooleanSetter('disableIntegratedTasks'); + +/* var isNonNegativeNumber = function (n) { return !(typeof(n) !== 'number' || isNaN(n) || n < 0); }; +*/ -// CryptPad_AsyncStore.rpc.send('ADMIN', [ 'ADMIN_DECREE', ['UPDATE_DEFAULT_STORAGE', [100 * 1024 * 1024]]], console.log) -commands.UPDATE_DEFAULT_STORAGE = function (Env, args) { - if (!Array.isArray(args) || !isNonNegativeNumber(args[0])) { - throw new Error('INVALID_ARGS'); - } - var limit = args[0]; - if (limit === Env.defaultStorageLimit) { return false; } - Env.defaultStorageLimit = limit; - return true; +var isInteger = function (n) { + return !(typeof(n) !== 'number' || isNaN(n) || (n % 1) !== 0); }; +var args_isInteger = function (args) { + return !(!Array.isArray(args) || !isInteger(args[0])); +}; + +var makeIntegerSetter = function (attr) { + return function (Env, args) { + if (!args_isInteger(args)) { + throw new Error('INVALID_ARGS'); + } + var integer = args[0]; + if (integer === Env[attr]) { return false; } + Env[attr] = integer; + return true; + }; +}; + +// CryptPad_AsyncStore.rpc.send('ADMIN', [ 'ADMIN_DECREE', ['SET_MAX_UPLOAD_SIZE', [50 * 1024 * 1024]]], console.log) +commands.SET_MAX_UPLOAD_SIZE = makeIntegerSetter('maxUploadSize'); + +// CryptPad_AsyncStore.rpc.send('ADMIN', [ 'ADMIN_DECREE', ['SET_PREMIUM_UPLOAD_SIZE', [150 * 1024 * 1024]]], console.log) +commands.SET_PREMIUM_UPLOAD_SIZE = makeIntegerSetter('premiumUploadSize'); + +// CryptPad_AsyncStore.rpc.send('ADMIN', [ 'ADMIN_DECREE', ['UPDATE_DEFAULT_STORAGE', [100 * 1024 * 1024]]], console.log) +commands.UPDATE_DEFAULT_STORAGE = makeIntegerSetter('defaultStorageLimit'); + +commands.SET_LAST_EVICTION = makeIntegerSetter('lastEviction'); + var Quota = require("./commands/quota"); var Keys = require("./keys"); var Util = require("./common-util"); diff --git a/lib/historyKeeper.js b/lib/historyKeeper.js index 299756cac..fc71c9856 100644 --- a/lib/historyKeeper.js +++ b/lib/historyKeeper.js @@ -165,10 +165,9 @@ module.exports.create = function (Env, cb) { } })); }).nThen(function () { - if (Env.disableIntegratedTasks) { return; } - var tasks_running; Env.intervals.taskExpiration = setInterval(function () { + if (Env.disableIntegratedTasks) { return; } if (tasks_running) { return; } tasks_running = true; Env.runTasks(function (err) { @@ -179,7 +178,6 @@ module.exports.create = function (Env, cb) { }); }, 1000 * 60 * 5); // run every five minutes }).nThen(function () { - if (Env.disableIntegratedEviction) { return; } const ONE_DAY = 24 * 1000 * 60 * 60; // setting the time of the last eviction to "now" // effectively makes it so that we'll start evicting after the server @@ -187,6 +185,7 @@ module.exports.create = function (Env, cb) { var active = false; Env.intervals.eviction = setInterval(function () { + if (Env.disableIntegratedEviction) { return; } if (active) { return; } var now = +new Date(); // evict inactive data once per day