toggle syncthing button

This commit is contained in:
IrosTheBeggar 2021-05-01 02:05:50 -04:00
parent 155b184cc7
commit b7e07c080f
4 changed files with 63 additions and 10 deletions

View File

@ -669,4 +669,29 @@ exports.setup = (mstream) => {
res.status(500).json({ error: typeof err === 'string' ? err : 'Unknown Error' });
}
});
let enableFederationDebouncer = false;
mstream.post('/api/v1/admin/federation/enable', async (req, res) => {
try {
const schema = Joi.object({ enable: Joi.boolean().required() });
await schema.validateAsync(req.body);
}catch (err) {
return res.status(500).json({ error: 'Validation Error' });
}
try {
if (enableFederationDebouncer === true) { throw 'Debouncer Enabled'; }
await admin.enableFederation(req.body.enable);
res.json({});
} catch(err) {
winston.error('admin error', {stack: err});
res.status(500).json({ error: typeof err === 'string' ? err : 'Unknown Error' });
}
enableFederationDebouncer = true;
setTimeout(() => {
enableFederationDebouncer = false;
}, 5000);
});
}

View File

@ -48,7 +48,7 @@ exports.getPathId = (path) => {
// TODO: change this for server reboot
exports.setup = async () => {
if (config.program.federation.enabled === false) { return; }
if (config.program.federation.enabled === false) { return this.kill(); }
try {
await initSyncthingConfig();
@ -61,6 +61,18 @@ exports.setup = async () => {
}
}
let preventRebootFlag = false;
exports.kill = async () => {
if(spawnedProcess) {
preventRebootFlag = true;
spawnedProcess.stdin.pause();
spawnedProcess.kill();
delete spawnedProcess;
delete myId;
delete xmlObj;
}
}
function initSyncthingConfig() {
return new Promise((resolve, reject) => {
const newProcess = spawn(path.join(__dirname, `../../bin/syncthing/${osMap[platform]}`), [`--generate=${config.program.storage.syncConfigDirectory}`], {});
@ -349,7 +361,7 @@ function bootProgram() {
}
try {
spawnedProcess = spawn(path.join(__dirname, `../../bin/syncthing/${osMap[platform]}`), ['--home', config.program.storage.syncConfigDirectory], {});
spawnedProcess = spawn(path.join(__dirname, `../../bin/syncthing/${osMap[platform]}`), ['--home', config.program.storage.syncConfigDirectory, '--no-browser'], {});
spawnedProcess.stdout.on('data', (data) => {
winston.info(`sync: ${data}`);
@ -360,12 +372,17 @@ function bootProgram() {
});
spawnedProcess.on('close', (code) => {
winston.info('Sync: SyncThing failed. Attempting to reboot');
setTimeout(() => {
winston.info('Sync: Rebooting SyncThing');
delete spawnedProcess;
bootProgram();
}, 4000);
if (preventRebootFlag === false) {
winston.info('Syncthing failed. Attempting to reboot');
setTimeout(() => {
winston.info('Sync: Rebooting SyncThing');
delete spawnedProcess;
bootProgram();
}, 4000);
} else {
winston.info('Syncthing Turned Off');
preventRebootFlag = false;
}
});
winston.info('Sync: SyncThing Booted');

View File

@ -6,6 +6,7 @@ const mStreamServer = require('../server');
const dbQueue = require('../db/task-queue');
const logger = require('../logger');
const db = require('../db/manager');
const syncthing = require('../state/syncthing');
exports.loadFile = async (file) => {
return JSON.parse(await fs.readFile(file, 'utf-8'));
@ -317,3 +318,13 @@ exports.lockAdminApi = async (val) => {
config.program.lockAdmin = val;
}
exports.enableFederation = async (val) => {
const loadConfig = await this.loadFile(config.configFile);
loadConfig.federation.enabled = val;
await this.saveFile(loadConfig, config.configFile);
config.program.federation.enabled = val;
syncthing.setup();
}

View File

@ -106,10 +106,10 @@
<svg xmlns="http://www.w3.org/2000/svg" height="28" viewBox="0 0 48 48"><path fill="none" stroke="#388e3c" stroke-width="5" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" d="M8.5 8.5h9l-9 9v11l20-20h11l-31 31h11l20-20v11l-9 9h9"/></svg>
<span>Transcoding</span>
</div>
<!-- <div class="side-nav-item waves-effect waves-purple" onclick="changeView('federation-view', this)">
<div class="side-nav-item waves-effect waves-purple" onclick="changeView('federation-view', this)">
<svg xmlns="http://www.w3.org/2000/svg" height="28" viewBox="0 0 48 48"><path d="M 44.398438 17.300781 C 44.300781 15.898438 43.300781 14.800781 41.898438 14.601563 C 38.398438 14 30.300781 13 24.199219 13 C 18.101563 13 9.601563 13.898438 6 14.5 C 4.699219 14.699219 3.699219 15.800781 3.5 17.199219 L 3 25 C 2.898438 26.601563 4.101563 27.800781 5.699219 28.199219 C 8.601563 29 15.699219 29 24.199219 29 C 32.699219 29 39.5 28.898438 42.300781 28.300781 C 43.898438 28 45.101563 26.699219 45 25.101563 Z M 39 22 L 9 22 C 8.398438 22 8 21.5 8 21 C 8 20.5 8.398438 20 9 20 L 39 20 C 39.601563 20 40 20.5 40 21 C 40 21.5 39.601563 22 39 22 Z" fill="#ffc107"/><path d="M 23.699219 3 C 23.5 3 23.5 3 23.398438 3.199219 C 19 8.398438 15.699219 14.800781 13.398438 22.898438 C 11.800781 28.898438 10.5 36.101563 10 44.699219 C 10 44.898438 10.199219 45 10.300781 45 L 10.5 45 C 10.699219 45 10.699219 45 10.800781 44.800781 C 15.5 38.601563 19.800781 33.699219 26 29.800781 C 27 29.300781 27.5 29 28 29 C 28.199219 29 28.5 29.199219 29 29.800781 C 31.601563 33.398438 34.398438 36.898438 37.199219 41.800781 C 37.199219 42 37.5 42 37.699219 42 C 37.898438 42 38 41.800781 38 41.699219 C 37.300781 34.5 36.199219 27.898438 33.898438 21.398438 C 31.601563 14.699219 28.300781 8.5 24.101563 3.199219 C 24.101563 3 23.898438 3 23.699219 3 Z" fill="#455a64"/><path d="M 32 22.101563 C 30 16.101563 27.199219 10.699219 23.699219 6 C 20 10.800781 17.300781 16.5 15.398438 23.398438 C 14 28.601563 13 33.898438 12.398438 39.601563 C 16.300781 34.800781 20 31.199219 25 28.101563 L 25.101563 28 L 25.199219 28 C 25.398438 27.898438 25.601563 27.800781 25.699219 27.699219 C 26.5 27.300781 27.101563 27 28 27 C 29.398438 27 30.300781 28.199219 30.601563 28.601563 C 31.101563 29.199219 31.5 29.800781 32 30.5 C 33.101563 31.898438 34.101563 33.398438 35.199219 34.898438 C 34.398438 30.199219 33.398438 26 32 22.101563 Z" fill="#c5cae9"/></svg>
<span>Federation</span>
</div> -->
</div>
<div class="side-nav-item waves-effect waves-purple" onclick="changeView('logs-view', this)">
<svg xmlns="http://www.w3.org/2000/svg" height="28" viewBox="0 0 58 58"><path d="M36.028 0c-11.046 0-17 3.515-17 6v10.849l-4.335-4.335-.118-.118c-.586-.586-3.943-1-6.547 1.604-2.604 2.603-3.467 7.238-2.881 7.824l.118.118 13.763 13.763V52.5c0 3.038 5.954 5.5 17 5.5s17-2.462 17-5.5V6c0-2.485-5.954-6-17-6z" fill="#745a49"/><path d="M36.028 0C25.89 0 20.056 2.959 19.163 5.368 20.056 8.546 25.89 11 36.028 11 46.166 11 52 8.546 52.893 5.368 52.001 2.959 46.166 0 36.028 0zM15.735 13.556l-1.042-1.042-.118-.118c-.586-.586-3.943-1-6.547 1.604-2.604 2.603-3.467 7.238-2.881 7.824l.118.118.952.952c6.334-1.911 8.696-5.948 9.518-9.338z" fill="#d1a170"/><path d="M46.028 50a1 1 0 00-1 1v6.304a28.434 28.434 0 002-.435V51a1 1 0 00-1-1zM40.028 47a1 1 0 00-1 1v9.928a55.775 55.775 0 002-.124V48a1 1 0 00-1-1zM36.028 54a1 1 0 00-1 1v2.986c.333.005.657.014 1 .014.343 0 .666-.009 1-.014V55a1 1 0 00-1-1zM31.028 49a1 1 0 00-1 1v7.716c.642.065 1.304.122 2 .165V50a1 1 0 00-1-1zM24.028 42a1 1 0 00-1 1v13.251c.601.225 1.268.431 2 .618V43a1 1 0 00-1-1zM23.006 8.924l.022 10.078a1 1 0 001 .998h.001a1 1 0 00.999-1.002l-.02-9.339a17.271 17.271 0 01-2.002-.735zM27.991 10.364l.037 15.638a1 1 0 001 .998h.002a1 1 0 00.998-1.002l-.036-15.34a36.834 36.834 0 01-2.001-.294zM37.028 10.985V16a1 1 0 002 0v-5.083c-.645.034-1.313.056-2 .068zM44.028 10.37V27a1 1 0 002 0V9.94c-.628.159-1.291.304-2 .43zM48.028 9.336V19a1 1 0 002 0V8.435a13.82 13.82 0 01-2 .901z" fill="#3d3028"/></svg>
<span>Logs</span>