🔨 Add backend support for multiple cron settings #88

This commit is contained in:
SebClem 2021-12-22 11:43:50 +01:00
parent 7b7d5a2b0e
commit 7ba85fb3ac
No known key found for this signature in database
GPG Key ID: 3D8E353F900B1305
3 changed files with 140 additions and 68 deletions

View File

@ -21,7 +21,7 @@ function updatetNextDate() {
class CronContainer {
constructor() {
this.cronJob = null;
this.cronJobs = [];
this.cronClean = null;
}
@ -33,52 +33,70 @@ class CronContainer {
this.cronClean = new CronJob("0 1 * * *", this._clean, null, false, Intl.DateTimeFormat().resolvedOptions().timeZone);
this.cronClean.start();
}
if (this.cronJob != null) {
logger.info("Stopping Cron...");
this.cronJob.stop();
this.cronJob = null;
}
if (!settingsTools.check_cron(settingsTools.getSettings())) {
logger.warn("No Cron settings available.");
return;
}
switch (settings.cron_base) {
case "0":
logger.warn("No Cron settings available.");
return;
case "1": {
let splited = settings.cron_hour.split(":");
cronStr = "" + splited[1] + " " + splited[0] + " * * *";
break;
}
case "2": {
let splited = settings.cron_hour.split(":");
cronStr = "" + splited[1] + " " + splited[0] + " * * " + settings.cron_weekday;
break;
}
case "3": {
let splited = settings.cron_hour.split(":");
cronStr = "" + splited[1] + " " + splited[0] + " " + settings.cron_month_day + " * *";
break;
}
case "4": {
cronStr = settings.cron_custom;
break;
if(this.cronJobs.length != 0){
this._clean_cron_jobs();
}
if (settings.cron.length == 0){
logger.warn("No Cron settings available.");
}
else {
let i = 0;
for(let cron of settings.cron){
logger.info(`Starting cron #${i} ...`)
switch (cron.cron_base) {
case "1": {
let splited = cron.cron_hour.split(":");
cronStr = "" + splited[1] + " " + splited[0] + " * * *";
break;
}
case "2": {
let splited = cron.cron_hour.split(":");
cronStr = "" + splited[1] + " " + splited[0] + " * * " + cron.cron_weekday;
break;
}
case "3": {
let splited = cron.cron_hour.split(":");
cronStr = "" + splited[1] + " " + splited[0] + " " + cron.cron_month_day + " * *";
break;
}
case "4": {
cronStr = cron.cron_custom;
break;
}
}
this.cronJobs.push(new CronJob(cronStr, this._createBackup, null, true, Intl.DateTimeFormat().resolvedOptions().timeZone));
i ++ ;
}
}
logger.info("Starting Cron...");
this.cronJob = new CronJob(cronStr, this._createBackup, null, false, Intl.DateTimeFormat().resolvedOptions().timeZone);
this.cronJob.start();
this.updateNextDate();
}
updateNextDate() {
let date;
if (this.cronJob == null) date = null;
else date = this.cronJob.nextDate().format("MMM D, YYYY HH:mm");
if (this.cronJobs.length == 0)
date = null;
else {
let last_date = null;
for(let item of this.cronJobs){
if(last_date == null)
last_date = item.nextDate();
else{
if(last_date > item.nextDate())
last_date = item.nextDate();
}
}
date = last_date.format("MMM D, YYYY HH:mm");
}
let status = statusTools.getStatus();
status.next_backup = date;
statusTools.setStatus(status);
@ -124,6 +142,16 @@ class CronContainer {
webdav.clean().catch();
}
}
_clean_cron_jobs(){
let i = 0;
for(let elem of this.cronJobs){
logger.info(`Stopping Crong job #${i}`)
elem.stop();
i++;
}
this.cronJobs = []
}
}
class Singleton {

View File

@ -6,48 +6,92 @@ const CronJob = require("cron").CronJob;
const settingsPath = "/data/backup_conf.json";
function check_cron(conf) {
if (conf.cron_base != null) {
if (conf.cron_base === "1" || conf.cron_base === "2" || conf.cron_base === "3") {
if (conf.cron_hour != null && conf.cron_hour.match(/\d\d:\d\d/)) {
if (conf.cron_base === "1") return true;
} else return false;
}
if (conf.cron != null) {
if (!Array.isArray(conf.cron))
return false;
if (conf.cron_base === "2") {
return conf.cron_weekday != null && conf.cron_weekday >= 0 && conf.cron_weekday <= 6;
}
if (conf.cron_base === "3") {
return conf.cron_month_day != null && conf.cron_month_day >= 1 && conf.cron_month_day <= 28;
}
if (conf.cron_base === "4") {
if (conf.cron_custom != null) {
try {
new CronJob(conf.cron_custom, () => {});
return true;
} catch(e) {
return false;
for (let elem of conf.cron){
if (elem.cron_base != null) {
if (elem.cron_base === "1" || elem.cron_base === "2" || elem.cron_base === "3") {
if (elem.cron_hour != null && elem.cron_hour.match(/\d\d:\d\d/)) {
if (elem.cron_base === "1")
continue;
}
else
return false;
}
}else return false;
if (elem.cron_base === "2") {
if( elem.cron_weekday != null && elem.cron_weekday >= 0 && elem.cron_weekday <= 6)
continue;
else
return false;
}
if (elem.cron_base === "3") {
if( elem.cron_month_day != null && elem.cron_month_day >= 1 && elem.cron_month_day <= 28)
continue;
else
return false
}
if (elem.cron_base === "4") {
if (elem.cron_custom != null) {
try {
new CronJob(elem.cron_custom, () => { });
continue;
} catch (e) {
return false;
}
}
else
return false;
}
}
else
return false
}
}
else
return false;
if (conf.cron_base === "0") return true;
} else return false;
return true;
}
return false;
function migrate_cron_conf(conf) {
let obj = {
cron_base: conf.cron_base,
cron_hour: conf.cron_hour,
cron_weekday: conf.cron_weekday,
cron_month_day: conf.cron_month_day,
cron_custom: conf.cron_custom,
}
conf.cron = [obj];
delete conf.cron_base;
delete conf.cron_hour;
delete conf.cron_weekday;
delete conf.cron_month_day;
delete conf.cron_custom;
return conf
}
function check(conf, fallback = false) {
let needSave = false;
if (conf.cron == null && !Array.isArray(conf.cron) && conf.cron_base != null) {
// Migrate to new cron conf format
logger.warn("Migrating to new cron conf format !")
conf = migrate_cron_conf(conf);
needSave = true;
}
if (!check_cron(conf)) {
if (fallback) {
logger.warn("Bad value for cron settings, fallback to default ");
conf.cron_base = "0";
conf.cron_hour = "00:00";
conf.cron_weekday = "0";
conf.cron_month_day = "1";
conf.cron_custom = "5 4 * * *";
conf.cron = []
} else {
logger.error("Bad value for cron settings");
return [false, "Bad cron settings"];
@ -184,7 +228,7 @@ function getSettings() {
}
function setSettings(settings) {
fs.writeFileSync(settingsPath, JSON.stringify(settings));
fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2));
}
exports.getSettings = getSettings;

View File

@ -32,8 +32,8 @@ function getStatus() {
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));
if(old_state_str !== JSON.stringify(state, null, 2)){
fs.writeFileSync(statusPath, JSON.stringify(state, null, 2));
hassioApiTools.publish_state(state);
}
}else{