Compare commits

..

No commits in common. "f3c0b6787ab310160e463b0cea1212117bfde3bb" and "143eecbcf90ea28b4d85b4cb74dfd9953de79ad2" have entirely different histories.

4 changed files with 106 additions and 59 deletions

View File

@ -160,9 +160,9 @@ router.post("/new-backup", function (req, res, next) {
hassioApiTools.startAddons().catch(() => {
})
});
}).catch(()=>{});
}).catch(()=>{});
}).catch(()=>{});;
});
});
});
})
.catch(() => {
hassioApiTools.startAddons().catch(() => {

View File

@ -37,8 +37,12 @@ function getVersion() {
resolve(version);
})
.catch((error) => {
statusTools.setError(`Fail to fetch HA Version (${error.message})`, 1);
reject(`Fail to fetch HA Version (${error.message})`);
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);
});
});
}
@ -75,8 +79,12 @@ function getAddonList() {
resolve(installed);
})
.catch((error) => {
statusTools.setError(`Fail to fetch addons list (${error.message})`, 1);
reject(`Fail to fetch addons list (${error.message})`);
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);
});
});
}
@ -158,8 +166,12 @@ function getSnapshots() {
resolve(snaps);
})
.catch((error) => {
statusTools.setError(`Fail to fetch Hassio backups (${error.message})`, 1);
reject(`Fail to fetch Hassio backups (${error.message})`);
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);
});
});
}
@ -201,12 +213,20 @@ function downloadSnapshot(id) {
})
.catch((err) => {
fs.unlinkSync(tmp_file);
statusTools.setError(`Fail to download Hassio backup (${error.message})`, 7);
reject(`Fail to download Hassio backup (${error.message})`);
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);
});
})
.catch((err) => {
statusTools.setError("Fail to download Hassio backup. Not found ?", 7);
status.status = "error";
status.message = "Fail to download Hassio snapshot. Not found ?";
status.error_code = 7;
statusTools.setStatus(status);
logger.error(status.message);
reject();
});
});
@ -285,8 +305,12 @@ function createNewBackup(name) {
resolve(result.body.data.slug);
})
.catch((error) => {
statusTools.setError(`Can't create new snapshot (${error.message})`, 5);
reject(`Can't create new snapshot (${error.message})`);
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);
});
}).catch(reject);
@ -316,9 +340,8 @@ function clean() {
logger.info("Local clean done.");
resolve();
})
.catch((e) => {
statusTools.setError(`Fail to clean backups (${e}) !`, 6);
reject(`Fail to clean backups (${e}) !`);
.catch(() => {
reject();
});
});
}
@ -379,15 +402,19 @@ function uploadSnapshot(path) {
})
.on("error", (err) => {
fs.unlinkSync(path);
statusTools.setError(`Fail to upload backup to home assistant (${err}) !`, 4);
reject(`Fail to upload backup to home assistant (${err}) !`);
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);
});
});
}
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;
@ -403,7 +430,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));
}
@ -415,11 +442,14 @@ function stopAddons() {
error = val.reason;
if (error) {
statusTools.setError(`Fail to stop addons(${error}) !`, 8);
status.status = "error";
status.error_code = 8;
status.message = `Fail to stop addons(${error}) !`;
statusTools.setStatus(status);
logger.error(status.message);
reject(status.message);
} else {
logger.info('... Ok');
logger.info('... Ok')
resolve();
}
});
@ -455,7 +485,12 @@ function startAddons() {
error = val.reason;
if (error) {
statusTools.setError(`Fail to start addons (${error}) !`, 9)
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);
reject(status.message);
} else {
logger.info('... Ok')

View File

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