🔨 Change status storage method

This commit is contained in:
SebClem 2022-02-17 00:25:54 +01:00
parent 69b2f296e9
commit f3c0b6787a
No known key found for this signature in database
GPG Key ID: 3D8E353F900B1305
3 changed files with 57 additions and 104 deletions

View File

@ -13,7 +13,7 @@ const pipeline = promisify(stream.pipeline);
// Default timout to 90min
const create_snap_timeout = parseInt(process.env.CREATE_BACKUP_TIMEOUT) || ( 90 * 60 * 1000 );
const create_snap_timeout = parseInt(process.env.CREATE_BACKUP_TIMEOUT) || (90 * 60 * 1000);
function getVersion() {
@ -37,12 +37,8 @@ function getVersion() {
resolve(version);
})
.catch((error) => {
status.status = "error";
status.message = "Fail to fetch HA Version (" + error.message + ")";
status.error_code = 1;
statusTools.setStatus(status);
logger.error(status.message);
reject(error.message);
statusTools.setError(`Fail to fetch HA Version (${error.message})`, 1);
reject(`Fail to fetch HA Version (${error.message})`);
});
});
}
@ -79,12 +75,8 @@ function getAddonList() {
resolve(installed);
})
.catch((error) => {
status.status = "error";
status.message = "Fail to fetch addons list (" + error.message + ")";
status.error_code = 1;
statusTools.setStatus(status);
logger.error(status.message);
reject(error.message);
statusTools.setError(`Fail to fetch addons list (${error.message})`, 1);
reject(`Fail to fetch addons list (${error.message})`);
});
});
}
@ -166,12 +158,8 @@ function getSnapshots() {
resolve(snaps);
})
.catch((error) => {
status.status = "error";
status.message = "Fail to fetch Hassio snapshots (" + error.message + ")";
status.error_code = 1;
statusTools.setStatus(status);
logger.error(status.message);
reject(error.message);
statusTools.setError(`Fail to fetch Hassio backups (${error.message})`, 1);
reject(`Fail to fetch Hassio backups (${error.message})`);
});
});
}
@ -213,20 +201,12 @@ function downloadSnapshot(id) {
})
.catch((err) => {
fs.unlinkSync(tmp_file);
status.status = "error";
status.message = "Fail to download Hassio snapshot (" + err.message + ")";
status.error_code = 7;
statusTools.setStatus(status);
logger.error(status.message);
reject(err.message);
statusTools.setError(`Fail to download Hassio backup (${error.message})`, 7);
reject(`Fail to download Hassio backup (${error.message})`);
});
})
.catch((err) => {
status.status = "error";
status.message = "Fail to download Hassio snapshot. Not found ?";
status.error_code = 7;
statusTools.setStatus(status);
logger.error(status.message);
statusTools.setError("Fail to download Hassio backup. Not found ?", 7);
reject();
});
});
@ -305,12 +285,8 @@ function createNewBackup(name) {
resolve(result.body.data.slug);
})
.catch((error) => {
status.status = "error";
status.message = "Can't create new snapshot (" + error.message + ")";
status.error_code = 5;
statusTools.setStatus(status);
logger.error(status.message);
reject(status.message);
statusTools.setError(`Can't create new snapshot (${error.message})`, 5);
reject(`Can't create new snapshot (${error.message})`);
});
}).catch(reject);
@ -340,8 +316,9 @@ function clean() {
logger.info("Local clean done.");
resolve();
})
.catch(() => {
reject();
.catch((e) => {
statusTools.setError(`Fail to clean backups (${e}) !`, 6);
reject(`Fail to clean backups (${e}) !`);
});
});
}
@ -402,19 +379,15 @@ function uploadSnapshot(path) {
})
.on("error", (err) => {
fs.unlinkSync(path);
status.status = "error";
status.error_code = 4;
status.message = `Fail to upload backup to home assistant (${err}) !`;
statusTools.setStatus(status);
logger.error(status.message);
reject(status.message);
statusTools.setError(`Fail to upload backup to home assistant (${err}) !`, 4);
reject(`Fail to upload backup to home assistant (${err}) !`);
});
});
}
function stopAddons() {
return new Promise(((resolve, reject) => {
logger.info('Stopping addons...')
logger.info('Stopping addons...');
let status = statusTools.getStatus();
status.status = "stopping";
status.progress = -1;
@ -430,7 +403,7 @@ function stopAddons() {
let addons_slug = settingsTools.getSettings().auto_stop_addon
for (let addon of addons_slug) {
if (addon !== "") {
logger.debug(`... Stopping addon ${addon}`)
logger.debug(`... Stopping addon ${addon}`);
promises.push(got.post(`http://hassio/addons/${addon}/stop`, option));
}
@ -442,14 +415,11 @@ function stopAddons() {
error = val.reason;
if (error) {
status.status = "error";
status.error_code = 8;
status.message = `Fail to stop addons(${error}) !`;
statusTools.setStatus(status);
statusTools.setError(`Fail to stop addons(${error}) !`, 8);
logger.error(status.message);
reject(status.message);
} else {
logger.info('... Ok')
logger.info('... Ok');
resolve();
}
});
@ -485,12 +455,7 @@ function startAddons() {
error = val.reason;
if (error) {
let status = statusTools.getStatus();
status.status = "error";
status.error_code = 9;
status.message = `Fail to start addons (${error}) !`;
statusTools.setStatus(status);
logger.error(status.message);
statusTools.setError(`Fail to start addons (${error}) !`, 9)
reject(status.message);
} else {
logger.info('... Ok')
@ -505,7 +470,7 @@ function startAddons() {
}));
}
function publish_state(state){
function publish_state(state) {
// let data_error_sensor = {
// state: state.status == "error" ? "on" : "off",
@ -575,16 +540,16 @@ function publish_state(state){
// });
}
export {
getVersion,
getAddonList,
getFolderList,
getSnapshots,
downloadSnapshot,
createNewBackup,
uploadSnapshot,
stopAddons,
startAddons,
clean,
publish_state
export {
getVersion,
getAddonList,
getFolderList,
getSnapshots,
downloadSnapshot,
createNewBackup,
uploadSnapshot,
stopAddons,
startAddons,
clean,
publish_state
}

View File

@ -1,45 +1,38 @@
import fs from "fs"
import * as hassioApiTools from "./hassioApiTools.js";
import logger from "../config/winston.js"
const statusPath = "/data/status.json";
let baseStatus = {
let status = {
status: "idle",
last_backup: null,
next_backup: null,
};
export function init() {
if (!fs.existsSync(statusPath)) {
fs.writeFileSync(statusPath, JSON.stringify(baseStatus));
} else {
let content = getStatus();
if (content.status !== "idle") {
content.status = "idle";
content.message = null;
setStatus(content);
}
if (status.status !== "idle") {
status.status = "idle";
status.message = null;
}
}
export function getStatus() {
if (!fs.existsSync(statusPath)) {
fs.writeFileSync(statusPath, JSON.stringify(baseStatus));
}
return JSON.parse(fs.readFileSync(statusPath).toString());
return status;
}
export function setStatus(state) {
if (fs.existsSync(statusPath)) {
let old_state_str = fs.readFileSync(statusPath).toString();
if(old_state_str !== JSON.stringify(state)){
fs.writeFileSync(statusPath, JSON.stringify(state));
hassioApiTools.publish_state(state);
}
}else{
fs.writeFileSync(statusPath, JSON.stringify(state));
hassioApiTools.publish_state(state);
export function setStatus(new_state) {
let old_state_str = JSON.stringify(status);
if(old_state_str !== JSON.stringify(new_state)){
status = new_state;
hassioApiTools.publish_state(status);
}
}
export function setError(message, error_code){
// Check if we don't have another error stored
if (status.status != "error") {
status.status = "error"
status.message = message
status.error_code = error_code;
}
logger.error(message);
}

View File

@ -70,12 +70,7 @@ class WebdavTools {
});
}
__cant_connect_status(err){
let status = statusTools.getStatus();
status.status = "error";
status.error_code = 3;
status.message = "Can't connect to Nextcloud (" + err + ") !";
statusTools.setStatus(status);
logger.error("Can't connect to Nextcloud (" + err + ") !");
statusTools.setError(`Can't connect to Nextcloud (${err})`, 3);
}
async __createRoot() {