Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
RunOnFluxBot committed Nov 21, 2023
1 parent 422cc03 commit 738ce54
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
56 changes: 53 additions & 3 deletions services/appsService.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,15 @@ const testPortsCache = {
ttl: 1000 * 60 * 60 * 3, // 3 hours
maxAge: 1000 * 60 * 60 * 3, // 3 hours
};

const syncthingAppsCache = {
max: 500,
};

const trySpawningGlobalAppCache = new LRUCache(GlobalAppsSpawnLRUoptions);
const myLongCache = new LRUCache(longCache);
const failedNodesTestPortsCache = new LRUCache(testPortsCache);
const receiveOnlySyncthingAppsCache = new LRUCache(syncthingAppsCache);

let removalInProgress = false;
let installationInProgress = false;
Expand Down Expand Up @@ -1864,7 +1870,7 @@ async function createAppVolume(appSpecifications, appName, isComponent, res) {
for (let i = 0; i < containersData.length; i += 1) {
const container = containersData[i];
const containerDataFlags = container.split(':')[1] ? container.split(':')[0] : '';
if (containerDataFlags.includes('s')) {
if (containerDataFlags.includes('s') || containerDataFlags.includes('r')) {
const containerFolder = i === 0 ? '' : `/appdata${container.split(':')[1].replace(containersData[0], '')}`;
const stFolderCreation = {
status: 'Creating .stfolder for syncthing...',
Expand Down Expand Up @@ -9876,6 +9882,7 @@ async function getDeviceID(fluxIP) {
}

let updateSyncthingRunning = false;
let syncthingAppsFirstRun = true;
// update syncthing configuration for locally installed apps
async function syncthingApps() {
try {
Expand Down Expand Up @@ -9906,7 +9913,7 @@ async function syncthingApps() {
for (let i = 0; i < containersData.length; i += 1) {
const container = containersData[i];
const containerDataFlags = container.split(':')[1] ? container.split(':')[0] : '';
if (containerDataFlags.includes('s')) {
if (containerDataFlags.includes('s') || containerDataFlags.includes('r')) {
const containerFolder = i === 0 ? '' : `/appdata${container.split(':')[1].replace(containersData[0], '')}`;
const identifier = installedApp.name;
const appId = dockerService.getAppIdentifier(identifier);
Expand Down Expand Up @@ -9945,13 +9952,34 @@ async function syncthingApps() {
}
}
}
let folderSyncType = 'sendreceive';
if (containerDataFlags.includes('r')) {
if (syncthingAppsFirstRun) {
receiveOnlySyncthingAppsCache.set(identifier, 6);
} else if (receiveOnlySyncthingAppsCache.has(identifier)) {
const numberOfRuns = receiveOnlySyncthingAppsCache.get(identifier);
if (numberOfRuns === 4) {
// eslint-disable-next-line no-await-in-loop
const folderReset = await syncthingService.dbRevert(folder.id);
log.info(`Reset syncthing app ${identifier} result: ${folderReset}`);
}
if (numberOfRuns === 5) {
folderSyncType = 'sendreceive';
}
receiveOnlySyncthingAppsCache.set(identifier, numberOfRuns + 1);
} else {
folderSyncType = 'receiveonly';
receiveOnlySyncthingAppsCache.set(identifier, 1);
}
}
folderIds.push(id);
foldersConfiguration.push({
id,
label,
path: folder,
devices,
paused: false,
type: folderSyncType,
});
}
}
Expand All @@ -9963,7 +9991,7 @@ async function syncthingApps() {
for (let i = 0; i < containersData.length; i += 1) {
const container = containersData[i];
const containerDataFlags = container.split(':')[1] ? container.split(':')[0] : '';
if (containerDataFlags.includes('s')) {
if (containerDataFlags.includes('s') || containerDataFlags.includes('r')) {
const containerFolder = i === 0 ? '' : `/appdata${container.split(':')[1].replace(containersData[0], '')}`;
const identifier = `${installedComponent.name}_${installedApp.name}`;
const appId = dockerService.getAppIdentifier(identifier);
Expand Down Expand Up @@ -10002,13 +10030,34 @@ async function syncthingApps() {
}
}
}
let folderSyncType = 'sendreceive';
if (containerDataFlags.includes('r')) {
if (syncthingAppsFirstRun) {
receiveOnlySyncthingAppsCache.set(identifier, 6);
} else if (receiveOnlySyncthingAppsCache.has(identifier)) {
const numberOfRuns = receiveOnlySyncthingAppsCache.get(identifier);
if (numberOfRuns === 4) {
// eslint-disable-next-line no-await-in-loop
const folderReset = await syncthingService.dbRevert(folder.id);
log.info(`Reset syncthing app ${identifier} result: ${folderReset}`);
}
if (numberOfRuns === 5) {
folderSyncType = 'sendreceive';
}
receiveOnlySyncthingAppsCache.set(identifier, numberOfRuns + 1);
} else {
folderSyncType = 'receiveonly';
receiveOnlySyncthingAppsCache.set(identifier, 1);
}
}
folderIds.push(id);
foldersConfiguration.push({
id,
label,
path: folder,
devices,
paused: false,
type: folderSyncType,
});
}
}
Expand Down Expand Up @@ -10074,6 +10123,7 @@ async function syncthingApps() {
log.error(error);
} finally {
updateSyncthingRunning = false;
syncthingAppsFirstRun = false;
await serviceHelper.delay(2 * 60 * 1000);
syncthingApps();
}
Expand Down
15 changes: 15 additions & 0 deletions services/syncthingService.js
Original file line number Diff line number Diff line change
Expand Up @@ -1661,6 +1661,20 @@ async function postDbRevert(req, res) {
});
}

/**
* To request revert of a receive only folder. Reverting a folder means to undo all local changes. This API call does nothing if the folder is not a receive only folder. Takes the mandatory parameter {folder}.
* @param {string} folder Request.
*/
async function dbRevert(folder) {
let apiPath = '/rest/db/revert';
if (folder) {
apiPath += `?folder=${folder}`;
} else {
throw new Error('folder parameter is mandatory');
}
return performRequest('post', apiPath);
}

/**
* To request immediate scan. Takes the optional parameters {folder} (folder ID), {sub} (path relative to the folder root) and {next} (time in seconds)
* @param {object} req Request.
Expand Down Expand Up @@ -2317,6 +2331,7 @@ module.exports = {
postDbOverride,
postDbPrio,
postDbRevert,
dbRevert,
postDbScan,
// EVENTS
getEvents,
Expand Down

0 comments on commit 738ce54

Please sign in to comment.