try different aproch for status

This commit is contained in:
SebClem 2024-04-10 15:13:41 +02:00
parent 17a6f1cbec
commit 35aaae183b
Signed by: sebclem
GPG Key ID: 5A4308F6A359EA50
4 changed files with 65 additions and 28 deletions

View File

@ -8,4 +8,6 @@
- `6` => Fail to clean
- `7` => Fail to download snap
- `8` => Fail to stop addon
- `9` => Fail to start addon
- `9` => Fail to start addon

View File

@ -10,6 +10,7 @@ import type { WebdavBackup } from "../types/services/webdav.js";
import type { WebdavConfig } from "../types/services/webdavConfig.js";
import { templateToRegexp } from "./backupConfigService.js";
import { getEndpoint } from "./webdavConfigService.js";
import { WebdabStatus } from "../types/status.js";
const PROPFIND_BODY =
'<?xml version="1.0" encoding="utf-8" ?>\
@ -38,6 +39,13 @@ export function checkWebdavLogin(config: WebdavConfig) {
},
(reason) => {
messageManager.error("Fail to connect to Webdav", reason?.message);
const status = statusTools.getStatus();
status.webdav = {
state: WebdabStatus.LOGIN_FAIL,
blocked: true,
last_check: DateTime.now()
}
statusTools.setStatus(status);
logger.error(`Fail to connect to Webdav`);
logger.error(reason);
return Promise.reject(reason);
@ -61,6 +69,13 @@ export async function createBackupFolder(conf: WebdavConfig) {
messageManager.error("Fail to create webdav root folder");
logger.error("Fail to create webdav root folder");
logger.error(error);
const status = statusTools.getStatus();
status.webdav = {
state: WebdabStatus.MK_FOLDER_FAIL,
blocked: true,
last_check: DateTime.now()
}
statusTools.setStatus(status);
return Promise.reject(error);
}
}

View File

@ -1,40 +1,46 @@
import { publish_state } from "../services/homeAssistantService.js";
import logger from "../config/winston.js"
import type { Status } from "../types/status.js";
import logger from "../config/winston.js";
import { type Status, WebdabStatus } from "../types/status.js";
import { DateTime } from "luxon";
let status: Status = {
status: "idle",
last_backup: undefined,
next_backup: undefined,
progress: -1,
status: "idle",
last_backup: undefined,
next_backup: undefined,
progress: -1,
webdav: {
state: WebdabStatus.INIT,
last_check: DateTime.now(),
blocked: true,
},
};
export function init() {
if (status.status !== "idle") {
status.status = "idle";
status.message = undefined;
status.progress = -1;
}
if (status.status !== "idle") {
status.status = "idle";
status.message = undefined;
status.progress = -1;
}
}
export function getStatus() {
return status;
return status;
}
export function setStatus(new_state: Status) {
const old_state_str = JSON.stringify(status);
if(old_state_str !== JSON.stringify(new_state)){
status = new_state;
publish_state(status);
}
const old_state_str = JSON.stringify(status);
if (old_state_str !== JSON.stringify(new_state)) {
status = new_state;
publish_state(status);
}
}
export function setError(message: string, error_code: number){
// 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);
export function setError(message: string, error_code: number) {
// 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

@ -1,3 +1,13 @@
import type { DateTime } from "luxon";
export enum WebdabStatus {
OK = "OK",
LOGIN_FAIL = "LOGIN_FAIL",
UPLOAD_FAIL = "UPLOAD_FAIL",
CON_ERROR = "CON_ERROR",
INIT = "INIT",
MK_FOLDER_FAIL = "MK_FOLDER_FAIL"
}
export interface Status {
status: string;
@ -6,5 +16,9 @@ export interface Status {
next_backup?: string;
message?: string;
error_code?: number;
}
webdav: {
state: WebdabStatus;
last_check: DateTime;
blocked: boolean;
};
}