Files
Jellystat/backend/tasks/BackupTask.js
Thegan Govender e7912397d2 Code clean-up + API Keys
Cleaned up redundant code and moved around classes to be better grouped eg routes folder

Moved endpoints to other route files to better represent their actions

Removed redundant endpoints

Renamed endpoints to be more meaningful

Added API Key authorisations to utilize the API outside of Jellystat UI (Still need to document Endpoints)

Added new column to app_config to store api keys

New API Section under settings

Updated backups route name
2023-07-01 22:52:19 +02:00

85 lines
2.2 KiB
JavaScript

const db = require("../db");
const Logging = require("../routes/logging");
const backup = require("../routes/backup");
const moment = require('moment');
const { randomUUID } = require('crypto');
async function BackupTask() {
let interval=10000;
async function intervalCallback() {
clearInterval(intervalTask);
try{
let current_time = moment();
const { rows: config } = await db.query(
'SELECT * FROM app_config where "ID"=1'
);
if (config.length===0 || config[0].JF_HOST === null || config[0].JF_API_KEY === null)
{
return;
}
const last_execution=await db.query( `SELECT "TimeRun"
FROM public.jf_logging
WHERE "Name"='Backup' AND "Result"='Success'
ORDER BY "TimeRun" DESC
LIMIT 1`).then((res) => res.rows);
if(last_execution.length!==0)
{
let last_execution_time = moment(last_execution[0].TimeRun).add(1, 'day');
if(current_time.isAfter(last_execution_time)===false)
{
intervalTask = setInterval(intervalCallback, interval);
return;
}
}
console.log('Running Scheduled Backup');
let refLog={logData:[],result:'Success'};
await backup.backup(refLog);
let endTime = moment();
let diffInSeconds = endTime.diff(current_time, 'seconds');
const uuid = randomUUID();
const log=
{
"Id":uuid,
"Name":"Backup",
"Type":"Task",
"ExecutionType":"Automatic",
"Duration":diffInSeconds,
"TimeRun":current_time,
"Log":JSON.stringify(refLog.logData),
"Result":refLog.result
};
Logging.insertLog(log);
console.log('Scheduled Backup Complete');
} catch (error)
{
console.log(error);
return [];
}
intervalTask = setInterval(intervalCallback, interval);
}
let intervalTask = setInterval(intervalCallback, interval);
}
module.exports = {
BackupTask,
};