From 28225dbf670a7b1ef7042b00a5547ba3e407435d Mon Sep 17 00:00:00 2001 From: SebClem Date: Wed, 10 Apr 2024 15:13:41 +0200 Subject: [PATCH] try different aproch for status --- nextcloud_backup/backend/error_code.md | 4 +- .../backend/src/services/webdavService.ts | 15 +++++ nextcloud_backup/backend/src/tools/status.ts | 56 ++++++++++--------- nextcloud_backup/backend/src/types/status.ts | 18 +++++- 4 files changed, 65 insertions(+), 28 deletions(-) diff --git a/nextcloud_backup/backend/error_code.md b/nextcloud_backup/backend/error_code.md index e0736a6..5268581 100644 --- a/nextcloud_backup/backend/error_code.md +++ b/nextcloud_backup/backend/error_code.md @@ -8,4 +8,6 @@ - `6` => Fail to clean - `7` => Fail to download snap - `8` => Fail to stop addon -- `9` => Fail to start addon \ No newline at end of file +- `9` => Fail to start addon + + diff --git a/nextcloud_backup/backend/src/services/webdavService.ts b/nextcloud_backup/backend/src/services/webdavService.ts index 32c4cf9..69f127e 100644 --- a/nextcloud_backup/backend/src/services/webdavService.ts +++ b/nextcloud_backup/backend/src/services/webdavService.ts @@ -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 = '\ @@ -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); } } diff --git a/nextcloud_backup/backend/src/tools/status.ts b/nextcloud_backup/backend/src/tools/status.ts index ce8a421..52c3f29 100644 --- a/nextcloud_backup/backend/src/tools/status.ts +++ b/nextcloud_backup/backend/src/tools/status.ts @@ -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); } diff --git a/nextcloud_backup/backend/src/types/status.ts b/nextcloud_backup/backend/src/types/status.ts index 93ae75c..bedca6e 100644 --- a/nextcloud_backup/backend/src/types/status.ts +++ b/nextcloud_backup/backend/src/types/status.ts @@ -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; - -} \ No newline at end of file + webdav: { + state: WebdabStatus; + last_check: DateTime; + blocked: boolean; + }; +}