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

View File

@ -1,45 +1,38 @@
import fs from "fs"
import * as hassioApiTools from "./hassioApiTools.js"; import * as hassioApiTools from "./hassioApiTools.js";
import logger from "../config/winston.js"
const statusPath = "/data/status.json";
let baseStatus = { let status = {
status: "idle", status: "idle",
last_backup: null, last_backup: null,
next_backup: null, next_backup: null,
}; };
export function init() { export function init() {
if (!fs.existsSync(statusPath)) { if (status.status !== "idle") {
fs.writeFileSync(statusPath, JSON.stringify(baseStatus)); status.status = "idle";
} else { status.message = null;
let content = getStatus();
if (content.status !== "idle") {
content.status = "idle";
content.message = null;
setStatus(content);
}
} }
} }
export function getStatus() { export function getStatus() {
if (!fs.existsSync(statusPath)) { return status;
fs.writeFileSync(statusPath, JSON.stringify(baseStatus));
}
return JSON.parse(fs.readFileSync(statusPath).toString());
} }
export function setStatus(state) { export function setStatus(new_state) {
if (fs.existsSync(statusPath)) { let old_state_str = JSON.stringify(status);
let old_state_str = fs.readFileSync(statusPath).toString(); if(old_state_str !== JSON.stringify(new_state)){
if(old_state_str !== JSON.stringify(state)){ status = new_state;
fs.writeFileSync(statusPath, JSON.stringify(state)); hassioApiTools.publish_state(status);
hassioApiTools.publish_state(state);
}
}else{
fs.writeFileSync(statusPath, JSON.stringify(state));
hassioApiTools.publish_state(state);
} }
}
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){ __cant_connect_status(err){
let status = statusTools.getStatus(); statusTools.setError(`Can't connect to Nextcloud (${err})`, 3);
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 + ") !");
} }
async __createRoot() { async __createRoot() {