Improvements for automatic device removal, #3089
Ylian Saint-Hilaire committed
Sep 1, 2021 at 22:21 UTC
7d25391f78e0a581b4d38bb9edf9201dcecd67fe
2 files changed
+19
-8
db.js
+11
-6
@@ -114,7 +114,7 @@ module.exports.CreateDB = function (parent, func) {
114
}
115
116
// Remove inactive devices
117
- obj.removeInactiveDevices = function () {
117
+ obj.removeInactiveDevices = function (showall, cb) {
118
// Get a list of domains and what their inactive device removal setting is
119
var removeInactiveDevicesPerDomain = {}, minRemoveInactiveDevicesPerDomain = {}, minRemoveInactiveDevice = 9999;
120
for (var i in parent.config.domains) {
@@ -144,7 +144,7 @@ module.exports.CreateDB = function (parent, func) {
144
}
145
146
// If there are no such settings for any domain, we can exit now.
147
- if (minRemoveInactiveDevice == 9999) return;
147
+ if (minRemoveInactiveDevice == 9999) { if (cb) { cb("No device removal policy set, nothing to do."); } return; }
148
const now = Date.now();
149
150
// For each domain with a inactive device removal setting, get a list of last device connections
@@ -152,10 +152,14 @@ module.exports.CreateDB = function (parent, func) {
152
obj.GetAllTypeNoTypeField('lastconnect', domainid, function (err, docs) {
153
if ((err != null) || (docs == null)) return;
154
for (var j in docs) {
155
- const days = (now - docs[j].time) / 86400000; // Calculate the number of inactive days
155
+ const days = Math.floor((now - docs[j].time) / 86400000); // Calculate the number of inactive days
156
+ var expireDays = -1;
157
+ if (removeInactiveDevicesPerDomain[docs[j].domain]) { expireDays = removeInactiveDevicesPerDomain[docs[j].domain]; }
158
+ const mesh = parent.webserver.meshes[docs[j].meshid];
159
+ if (mesh && (typeof mesh.expireDevs == 'number') && (expireDays > mesh.expireDevs)) { expireDays = mesh.expireDevs; }
160
var remove = false;
157
- if (removeInactiveDevicesPerDomain[docs[j].domain] && (removeInactiveDevicesPerDomain[docs[j].domain] < days)) { remove = true; }
158
- else { const mesh = parent.webserver.meshes[docs[j].meshid]; if (mesh && (typeof mesh.expireDevs == 'number') && (mesh.expireDevs < days)) { remove = true; } }
161
+ if ((expireDays > 0) && (expireDays < days)) { remove = true; }
162
+ if (cb) { if (showall || remove) { cb(docs[j]._id.substring(2) + ', ' + days + ' days, expire ' + expireDays + ' days' + (remove ? ', removing' : '')); } }
163
if (remove) {
164
// Check if this device is connected right now
165
const nodeid = docs[j]._id.substring(2);
@@ -163,7 +167,8 @@ module.exports.CreateDB = function (parent, func) {
167
if (conn == null) {
168
// Remove the device
169
obj.Get(nodeid, function (err, docs) {
166
- if ((err != null) || (docs == null) || (docs.length != 1)) return;
170
+ if (err != null) return;
171
+ if ((docs == null) || (docs.length != 1)) { obj.Remove('lc' + nodeid); return; } // Remove last connect time
172
const node = docs[0];
173
174
// Delete this node including network interface information, events and timeline
meshuser.js
+8
-2
@@ -6248,9 +6248,15 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
6248
if (cmdData.result == '') { cmdData.result = 'No relays.'; }
6249
}
6250
6251
+ // removeinactivedevices showall|showremoved
6252
function serverUserCommandRemoveInactiveDevices(cmdData) {
6252
- parent.db.removeInactiveDevices();
6253
- cmdData.result = 'Ok';
6253
+ var arg = cmdData.cmdargs['_'][0];
6254
+ if ((arg == null) && (arg != 'showremoved') && (arg != 'showall')) {
6255
+ cmdData.result = 'Usage: removeinactivedevices [showremoved|showall]';
6256
+ } else {
6257
+ parent.db.removeInactiveDevices((arg == 'showall'), function (msg) { try { ws.send(JSON.stringify({ action: 'serverconsole', value: msg, tag: cmdData.command.tag })); } catch (ex) { } });
6258
+ cmdData.result = 'Ok';
6259
+ }
6260
}
6261
6262
function serverUserCommandAutoBackup(cmdData) {