2022-09-27 23:38:40 +02:00
|
|
|
// import { CronJob } from "cron";
|
|
|
|
// import * as settingsTools from "./settingsTools.js";
|
|
|
|
// import * as hassioApiTools from "../services/haOsService.js";
|
|
|
|
// import * as statusTools from "./status.js";
|
|
|
|
// import * as pathTools from "./pathTools.js";
|
|
|
|
// // import webdav from "../services/webdavService.js";
|
2022-09-21 17:24:02 +02:00
|
|
|
|
2022-09-27 23:38:40 +02:00
|
|
|
// import logger from "../config/winston.js";
|
2022-09-21 17:24:02 +02:00
|
|
|
|
2022-09-27 23:38:40 +02:00
|
|
|
// class CronContainer {
|
|
|
|
// cronJob: CronJob | undefined;
|
|
|
|
// cronClean: CronJob | undefined;
|
2022-09-21 17:24:02 +02:00
|
|
|
|
2022-09-27 23:38:40 +02:00
|
|
|
// init() {
|
|
|
|
// const settings = settingsTools.getSettings();
|
|
|
|
// let cronStr = "";
|
|
|
|
// if (!this.cronClean) {
|
|
|
|
// logger.info("Starting auto clean cron...");
|
|
|
|
// this.cronClean = new CronJob(
|
|
|
|
// "0 1 * * *",
|
|
|
|
// this._clean,
|
|
|
|
// null,
|
|
|
|
// false,
|
|
|
|
// Intl.DateTimeFormat().resolvedOptions().timeZone
|
|
|
|
// );
|
|
|
|
// this.cronClean.start();
|
|
|
|
// }
|
|
|
|
// if (this.cronJob) {
|
|
|
|
// logger.info("Stopping Cron...");
|
|
|
|
// this.cronJob.stop();
|
|
|
|
// this.cronJob = undefined;
|
|
|
|
// }
|
|
|
|
// if (!settingsTools.check_cron(settingsTools.getSettings())) {
|
|
|
|
// logger.warn("No Cron settings available.");
|
|
|
|
// return;
|
|
|
|
// }
|
2022-09-21 17:24:02 +02:00
|
|
|
|
2022-09-27 23:38:40 +02:00
|
|
|
// switch (settings.cron_base) {
|
|
|
|
// case "0":
|
|
|
|
// logger.warn("No Cron settings available.");
|
|
|
|
// return;
|
|
|
|
// case "1": {
|
|
|
|
// const splited = settings.cron_hour.split(":");
|
|
|
|
// cronStr = "" + splited[1] + " " + splited[0] + " * * *";
|
|
|
|
// break;
|
|
|
|
// }
|
2022-09-21 17:24:02 +02:00
|
|
|
|
2022-09-27 23:38:40 +02:00
|
|
|
// case "2": {
|
|
|
|
// const splited = settings.cron_hour.split(":");
|
|
|
|
// cronStr =
|
|
|
|
// "" + splited[1] + " " + splited[0] + " * * " + settings.cron_weekday;
|
|
|
|
// break;
|
|
|
|
// }
|
2022-09-21 17:24:02 +02:00
|
|
|
|
2022-09-27 23:38:40 +02:00
|
|
|
// case "3": {
|
|
|
|
// const splited = settings.cron_hour.split(":");
|
|
|
|
// cronStr =
|
|
|
|
// "" +
|
|
|
|
// splited[1] +
|
|
|
|
// " " +
|
|
|
|
// splited[0] +
|
|
|
|
// " " +
|
|
|
|
// settings.cron_month_day +
|
|
|
|
// " * *";
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
// case "4": {
|
|
|
|
// cronStr = settings.cron_custom;
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// logger.info("Starting Cron...");
|
|
|
|
// this.cronJob = new CronJob(
|
|
|
|
// cronStr,
|
|
|
|
// this._createBackup,
|
|
|
|
// null,
|
|
|
|
// false,
|
|
|
|
// Intl.DateTimeFormat().resolvedOptions().timeZone
|
|
|
|
// );
|
|
|
|
// this.cronJob.start();
|
|
|
|
// this.updateNextDate();
|
|
|
|
// }
|
2022-09-21 17:24:02 +02:00
|
|
|
|
2022-09-27 23:38:40 +02:00
|
|
|
// updateNextDate() {
|
|
|
|
// let date;
|
|
|
|
// if (this.cronJob) {
|
|
|
|
// date = this.cronJob
|
|
|
|
// .nextDate()
|
|
|
|
// .setLocale("en")
|
|
|
|
// .toFormat("dd MMM yyyy, HH:mm");
|
|
|
|
// }
|
|
|
|
// const status = statusTools.getStatus();
|
|
|
|
// status.next_backup = date;
|
|
|
|
// statusTools.setStatus(status);
|
|
|
|
// }
|
2022-09-21 17:24:02 +02:00
|
|
|
|
2022-09-27 23:38:40 +02:00
|
|
|
// _createBackup() {
|
|
|
|
// logger.debug("Cron triggered !");
|
|
|
|
// const status = statusTools.getStatus();
|
|
|
|
// if (
|
|
|
|
// status.status === "creating" ||
|
|
|
|
// status.status === "upload" ||
|
|
|
|
// status.status === "download" ||
|
|
|
|
// status.status === "stopping" ||
|
|
|
|
// status.status === "starting"
|
|
|
|
// )
|
|
|
|
// return;
|
|
|
|
// hassioApiTools
|
|
|
|
// .stopAddons()
|
|
|
|
// .then(() => {
|
|
|
|
// hassioApiTools.getVersion().then((version) => {
|
|
|
|
// const name = settingsTools.getFormatedName(false, version);
|
|
|
|
// hassioApiTools.createNewBackup(name).then((id) => {
|
|
|
|
// hassioApiTools.downloadSnapshot(id).then(() => {
|
|
|
|
// webdav
|
|
|
|
// .uploadFile(
|
|
|
|
// id,
|
|
|
|
// webdav.getConf()?.back_dir + pathTools.auto + name + ".tar"
|
|
|
|
// )
|
|
|
|
// .then(() => {
|
|
|
|
// hassioApiTools.startAddons().catch(() => {
|
|
|
|
// // Skip
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
// })
|
|
|
|
// .catch(() => {
|
|
|
|
// hassioApiTools.startAddons().catch(() => {
|
|
|
|
// // Skip
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
// }
|
2022-09-21 17:24:02 +02:00
|
|
|
|
2022-09-27 23:38:40 +02:00
|
|
|
// _clean() {
|
|
|
|
// const autoCleanlocal = settingsTools.getSettings().auto_clean_local;
|
|
|
|
// if (autoCleanlocal && autoCleanlocal == "true") {
|
|
|
|
// hassioApiTools.clean().catch();
|
|
|
|
// }
|
|
|
|
// const autoCleanCloud = settingsTools.getSettings().auto_clean_backup;
|
|
|
|
// if (autoCleanCloud && autoCleanCloud == "true") {
|
|
|
|
// webdav.clean().catch();
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
2022-09-21 17:24:02 +02:00
|
|
|
|
2022-09-27 23:38:40 +02:00
|
|
|
// const INSTANCE = new CronContainer();
|
|
|
|
// export default INSTANCE;
|