Compare commits

...

2 Commits

Author SHA1 Message Date
dependabot[bot]
93ada90d08
⬆️ Bump got in /nextcloud_backup/rootfs/opt/nextcloud_backup
Bumps [got](https://github.com/sindresorhus/got) from 11.8.2 to 12.0.1.
- [Release notes](https://github.com/sindresorhus/got/releases)
- [Commits](https://github.com/sindresorhus/got/compare/v11.8.2...v12.0.1)

---
updated-dependencies:
- dependency-name: got
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-01-10 06:00:57 +00:00
7ba85fb3ac
🔨 Add backend support for multiple cron settings #88 2021-12-22 11:45:02 +01:00
5 changed files with 249 additions and 129 deletions

View File

@ -17,7 +17,7 @@
"ejs": "~3.1.6", "ejs": "~3.1.6",
"express": "~4.17.1", "express": "~4.17.1",
"form-data": "^4.0.0", "form-data": "^4.0.0",
"got": "^11.8.2", "got": "^12.0.1",
"http-errors": "~1.8.0", "http-errors": "~1.8.0",
"jquery": "^3.6.0", "jquery": "^3.6.0",
"moment": "^2.29.1", "moment": "^2.29.1",
@ -56,9 +56,9 @@
} }
}, },
"node_modules/@sindresorhus/is": { "node_modules/@sindresorhus/is": {
"version": "4.0.1", "version": "4.2.1",
"resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-4.0.1.tgz", "resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-4.2.1.tgz",
"integrity": "sha512-Qm9hBEBu18wt1PO2flE7LPb30BHMQt1eQgbV76YntdNk73XZGpn3izvGTYxbGgzXKgbCjiia0uxTd3aTNQrY/g==", "integrity": "sha512-BrzrgtaqEre0qfvI8sMTaEvx+bayuhPmfe2rfeUGPPHYr/PLxCOqkOe4TQTDPb+qcqgNcsAtXV/Ew74mcDIE8w==",
"engines": { "engines": {
"node": ">=10" "node": ">=10"
}, },
@ -67,14 +67,14 @@
} }
}, },
"node_modules/@szmarczak/http-timer": { "node_modules/@szmarczak/http-timer": {
"version": "4.0.6", "version": "5.0.1",
"resolved": "https://registry.npmjs.org/@szmarczak/http-timer/-/http-timer-4.0.6.tgz", "resolved": "https://registry.npmjs.org/@szmarczak/http-timer/-/http-timer-5.0.1.tgz",
"integrity": "sha512-4BAffykYOgO+5nzBWYwE3W90sBgLJoUPRWWcL8wlyiM8IB8ipJz3UMJ9KXQd1RKQXpKp8Tutn80HZtWsu2u76w==", "integrity": "sha512-+PmQX0PiAYPMeVYe237LJAYvOMYW1j2rH5YROyS3b4CTVJum34HfRvKvAzozHAQG0TnHNdUfY9nCeUyRAs//cw==",
"dependencies": { "dependencies": {
"defer-to-connect": "^2.0.0" "defer-to-connect": "^2.0.1"
}, },
"engines": { "engines": {
"node": ">=10" "node": ">=14.16"
} }
}, },
"node_modules/@types/cacheable-request": { "node_modules/@types/cacheable-request": {
@ -272,9 +272,9 @@
} }
}, },
"node_modules/cacheable-lookup": { "node_modules/cacheable-lookup": {
"version": "5.0.4", "version": "6.0.4",
"resolved": "https://registry.npmjs.org/cacheable-lookup/-/cacheable-lookup-5.0.4.tgz", "resolved": "https://registry.npmjs.org/cacheable-lookup/-/cacheable-lookup-6.0.4.tgz",
"integrity": "sha512-2/kNscPhpcxrOigMZzbiWF7dz8ilhb/nIHU3EyZiXWXpeq/au8qJ8VhdftMkty3n7Gj6HIGalQG8oiBNB3AJgA==", "integrity": "sha512-mbcDEZCkv2CZF4G01kr8eBd/5agkt9oCqz75tJMSIsquvRZ2sL6Hi5zGVKi/0OSC9oO1GHfJ2AV0ZIOY9vye0A==",
"engines": { "engines": {
"node": ">=10.6.0" "node": ">=10.6.0"
} }
@ -735,6 +735,11 @@
"node": ">= 6" "node": ">= 6"
} }
}, },
"node_modules/form-data-encoder": {
"version": "1.7.1",
"resolved": "https://registry.npmjs.org/form-data-encoder/-/form-data-encoder-1.7.1.tgz",
"integrity": "sha512-EFRDrsMm/kyqbTQocNvRXMLjc7Es2Vk+IQFx/YW7hkUH1eBl4J1fqiP34l74Yt0pFLCNpc06fkbVk00008mzjg=="
},
"node_modules/forwarded": { "node_modules/forwarded": {
"version": "0.2.0", "version": "0.2.0",
"resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz",
@ -766,29 +771,53 @@
} }
}, },
"node_modules/got": { "node_modules/got": {
"version": "11.8.2", "version": "12.0.1",
"resolved": "https://registry.npmjs.org/got/-/got-11.8.2.tgz", "resolved": "https://registry.npmjs.org/got/-/got-12.0.1.tgz",
"integrity": "sha512-D0QywKgIe30ODs+fm8wMZiAcZjypcCodPNuMz5H9Mny7RJ+IjJ10BdmGW7OM7fHXP+O7r6ZwapQ/YQmMSvB0UQ==", "integrity": "sha512-1Zhoh+lDej3t7Ks1BP/Jufn+rNqdiHQgUOcTxHzg2Dao1LQfp5S4Iq0T3iBxN4Zdo7QqCJL+WJUNzDX6rCP2Ew==",
"dependencies": { "dependencies": {
"@sindresorhus/is": "^4.0.0", "@sindresorhus/is": "^4.2.0",
"@szmarczak/http-timer": "^4.0.5", "@szmarczak/http-timer": "^5.0.1",
"@types/cacheable-request": "^6.0.1", "@types/cacheable-request": "^6.0.2",
"@types/responselike": "^1.0.0", "@types/responselike": "^1.0.0",
"cacheable-lookup": "^5.0.3", "cacheable-lookup": "^6.0.4",
"cacheable-request": "^7.0.1", "cacheable-request": "^7.0.2",
"decompress-response": "^6.0.0", "decompress-response": "^6.0.0",
"http2-wrapper": "^1.0.0-beta.5.2", "form-data-encoder": "1.7.1",
"lowercase-keys": "^2.0.0", "get-stream": "^6.0.1",
"p-cancelable": "^2.0.0", "http2-wrapper": "^2.1.9",
"lowercase-keys": "^3.0.0",
"p-cancelable": "^3.0.0",
"responselike": "^2.0.0" "responselike": "^2.0.0"
}, },
"engines": { "engines": {
"node": ">=10.19.0" "node": ">=14.16"
}, },
"funding": { "funding": {
"url": "https://github.com/sindresorhus/got?sponsor=1" "url": "https://github.com/sindresorhus/got?sponsor=1"
} }
}, },
"node_modules/got/node_modules/get-stream": {
"version": "6.0.1",
"resolved": "https://registry.npmjs.org/get-stream/-/get-stream-6.0.1.tgz",
"integrity": "sha512-ts6Wi+2j3jQjqi70w5AlN8DFnkSwC+MqmxEzdEALB2qXZYV3X/b1CTfgPLGJNMeAWxdPfU8FO1ms3NUfaHCPYg==",
"engines": {
"node": ">=10"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/got/node_modules/lowercase-keys": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/lowercase-keys/-/lowercase-keys-3.0.0.tgz",
"integrity": "sha512-ozCC6gdQ+glXOQsveKD0YsDy8DSQFjDTz4zyzEHNV5+JP5D62LmfDZ6o1cycFx9ouG940M5dE8C8CTewdj2YWQ==",
"engines": {
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/has-flag": { "node_modules/has-flag": {
"version": "3.0.0", "version": "3.0.0",
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz",
@ -836,12 +865,12 @@
"integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw==" "integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw=="
}, },
"node_modules/http2-wrapper": { "node_modules/http2-wrapper": {
"version": "1.0.3", "version": "2.1.10",
"resolved": "https://registry.npmjs.org/http2-wrapper/-/http2-wrapper-1.0.3.tgz", "resolved": "https://registry.npmjs.org/http2-wrapper/-/http2-wrapper-2.1.10.tgz",
"integrity": "sha512-V+23sDMr12Wnz7iTcDeJr3O6AIxlnvT/bmaAAAP/Xda35C90p9599p0F1eHR/N1KILWSoWVAiOMFjBBXaXSMxg==", "integrity": "sha512-QHgsdYkieKp+6JbXP25P+tepqiHYd+FVnDwXpxi/BlUcoIB0nsmTOymTNvETuTO+pDuwcSklPE72VR3DqV+Haw==",
"dependencies": { "dependencies": {
"quick-lru": "^5.1.1", "quick-lru": "^5.1.1",
"resolve-alpn": "^1.0.0" "resolve-alpn": "^1.2.0"
}, },
"engines": { "engines": {
"node": ">=10.19.0" "node": ">=10.19.0"
@ -1162,11 +1191,11 @@
} }
}, },
"node_modules/p-cancelable": { "node_modules/p-cancelable": {
"version": "2.1.1", "version": "3.0.0",
"resolved": "https://registry.npmjs.org/p-cancelable/-/p-cancelable-2.1.1.tgz", "resolved": "https://registry.npmjs.org/p-cancelable/-/p-cancelable-3.0.0.tgz",
"integrity": "sha512-BZOr3nRQHOntUjTrH8+Lh54smKHoHyur8We1V8DSMVrl5A2malOOwuJRnKRDjSnkoeBh4at6BwEnb5I7Jl31wg==", "integrity": "sha512-mlVgR3PGuzlo0MmTdk4cXqXWlwQDLnONTAg6sm62XkMJEiRxN3GL3SffkYvqwonbkJBcrI7Uvv5Zh9yjvn2iUw==",
"engines": { "engines": {
"node": ">=8" "node": ">=12.20"
} }
}, },
"node_modules/parseurl": { "node_modules/parseurl": {
@ -1636,16 +1665,16 @@
"peer": true "peer": true
}, },
"@sindresorhus/is": { "@sindresorhus/is": {
"version": "4.0.1", "version": "4.2.1",
"resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-4.0.1.tgz", "resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-4.2.1.tgz",
"integrity": "sha512-Qm9hBEBu18wt1PO2flE7LPb30BHMQt1eQgbV76YntdNk73XZGpn3izvGTYxbGgzXKgbCjiia0uxTd3aTNQrY/g==" "integrity": "sha512-BrzrgtaqEre0qfvI8sMTaEvx+bayuhPmfe2rfeUGPPHYr/PLxCOqkOe4TQTDPb+qcqgNcsAtXV/Ew74mcDIE8w=="
}, },
"@szmarczak/http-timer": { "@szmarczak/http-timer": {
"version": "4.0.6", "version": "5.0.1",
"resolved": "https://registry.npmjs.org/@szmarczak/http-timer/-/http-timer-4.0.6.tgz", "resolved": "https://registry.npmjs.org/@szmarczak/http-timer/-/http-timer-5.0.1.tgz",
"integrity": "sha512-4BAffykYOgO+5nzBWYwE3W90sBgLJoUPRWWcL8wlyiM8IB8ipJz3UMJ9KXQd1RKQXpKp8Tutn80HZtWsu2u76w==", "integrity": "sha512-+PmQX0PiAYPMeVYe237LJAYvOMYW1j2rH5YROyS3b4CTVJum34HfRvKvAzozHAQG0TnHNdUfY9nCeUyRAs//cw==",
"requires": { "requires": {
"defer-to-connect": "^2.0.0" "defer-to-connect": "^2.0.1"
} }
}, },
"@types/cacheable-request": { "@types/cacheable-request": {
@ -1818,9 +1847,9 @@
"integrity": "sha512-zauLjrfCG+xvoyaqLoV8bLVXXNGC4JqlxFCutSDWA6fJrTo2ZuvLYTqZ7aHBLZSMOopbzwv8f+wZcVzfVTI2Dg==" "integrity": "sha512-zauLjrfCG+xvoyaqLoV8bLVXXNGC4JqlxFCutSDWA6fJrTo2ZuvLYTqZ7aHBLZSMOopbzwv8f+wZcVzfVTI2Dg=="
}, },
"cacheable-lookup": { "cacheable-lookup": {
"version": "5.0.4", "version": "6.0.4",
"resolved": "https://registry.npmjs.org/cacheable-lookup/-/cacheable-lookup-5.0.4.tgz", "resolved": "https://registry.npmjs.org/cacheable-lookup/-/cacheable-lookup-6.0.4.tgz",
"integrity": "sha512-2/kNscPhpcxrOigMZzbiWF7dz8ilhb/nIHU3EyZiXWXpeq/au8qJ8VhdftMkty3n7Gj6HIGalQG8oiBNB3AJgA==" "integrity": "sha512-mbcDEZCkv2CZF4G01kr8eBd/5agkt9oCqz75tJMSIsquvRZ2sL6Hi5zGVKi/0OSC9oO1GHfJ2AV0ZIOY9vye0A=="
}, },
"cacheable-request": { "cacheable-request": {
"version": "7.0.2", "version": "7.0.2",
@ -2180,6 +2209,11 @@
"mime-types": "^2.1.12" "mime-types": "^2.1.12"
} }
}, },
"form-data-encoder": {
"version": "1.7.1",
"resolved": "https://registry.npmjs.org/form-data-encoder/-/form-data-encoder-1.7.1.tgz",
"integrity": "sha512-EFRDrsMm/kyqbTQocNvRXMLjc7Es2Vk+IQFx/YW7hkUH1eBl4J1fqiP34l74Yt0pFLCNpc06fkbVk00008mzjg=="
},
"forwarded": { "forwarded": {
"version": "0.2.0", "version": "0.2.0",
"resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz",
@ -2199,21 +2233,35 @@
} }
}, },
"got": { "got": {
"version": "11.8.2", "version": "12.0.1",
"resolved": "https://registry.npmjs.org/got/-/got-11.8.2.tgz", "resolved": "https://registry.npmjs.org/got/-/got-12.0.1.tgz",
"integrity": "sha512-D0QywKgIe30ODs+fm8wMZiAcZjypcCodPNuMz5H9Mny7RJ+IjJ10BdmGW7OM7fHXP+O7r6ZwapQ/YQmMSvB0UQ==", "integrity": "sha512-1Zhoh+lDej3t7Ks1BP/Jufn+rNqdiHQgUOcTxHzg2Dao1LQfp5S4Iq0T3iBxN4Zdo7QqCJL+WJUNzDX6rCP2Ew==",
"requires": { "requires": {
"@sindresorhus/is": "^4.0.0", "@sindresorhus/is": "^4.2.0",
"@szmarczak/http-timer": "^4.0.5", "@szmarczak/http-timer": "^5.0.1",
"@types/cacheable-request": "^6.0.1", "@types/cacheable-request": "^6.0.2",
"@types/responselike": "^1.0.0", "@types/responselike": "^1.0.0",
"cacheable-lookup": "^5.0.3", "cacheable-lookup": "^6.0.4",
"cacheable-request": "^7.0.1", "cacheable-request": "^7.0.2",
"decompress-response": "^6.0.0", "decompress-response": "^6.0.0",
"http2-wrapper": "^1.0.0-beta.5.2", "form-data-encoder": "1.7.1",
"lowercase-keys": "^2.0.0", "get-stream": "^6.0.1",
"p-cancelable": "^2.0.0", "http2-wrapper": "^2.1.9",
"lowercase-keys": "^3.0.0",
"p-cancelable": "^3.0.0",
"responselike": "^2.0.0" "responselike": "^2.0.0"
},
"dependencies": {
"get-stream": {
"version": "6.0.1",
"resolved": "https://registry.npmjs.org/get-stream/-/get-stream-6.0.1.tgz",
"integrity": "sha512-ts6Wi+2j3jQjqi70w5AlN8DFnkSwC+MqmxEzdEALB2qXZYV3X/b1CTfgPLGJNMeAWxdPfU8FO1ms3NUfaHCPYg=="
},
"lowercase-keys": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/lowercase-keys/-/lowercase-keys-3.0.0.tgz",
"integrity": "sha512-ozCC6gdQ+glXOQsveKD0YsDy8DSQFjDTz4zyzEHNV5+JP5D62LmfDZ6o1cycFx9ouG940M5dE8C8CTewdj2YWQ=="
}
} }
}, },
"has-flag": { "has-flag": {
@ -2256,12 +2304,12 @@
} }
}, },
"http2-wrapper": { "http2-wrapper": {
"version": "1.0.3", "version": "2.1.10",
"resolved": "https://registry.npmjs.org/http2-wrapper/-/http2-wrapper-1.0.3.tgz", "resolved": "https://registry.npmjs.org/http2-wrapper/-/http2-wrapper-2.1.10.tgz",
"integrity": "sha512-V+23sDMr12Wnz7iTcDeJr3O6AIxlnvT/bmaAAAP/Xda35C90p9599p0F1eHR/N1KILWSoWVAiOMFjBBXaXSMxg==", "integrity": "sha512-QHgsdYkieKp+6JbXP25P+tepqiHYd+FVnDwXpxi/BlUcoIB0nsmTOymTNvETuTO+pDuwcSklPE72VR3DqV+Haw==",
"requires": { "requires": {
"quick-lru": "^5.1.1", "quick-lru": "^5.1.1",
"resolve-alpn": "^1.0.0" "resolve-alpn": "^1.2.0"
} }
}, },
"iconv-lite": { "iconv-lite": {
@ -2509,9 +2557,9 @@
} }
}, },
"p-cancelable": { "p-cancelable": {
"version": "2.1.1", "version": "3.0.0",
"resolved": "https://registry.npmjs.org/p-cancelable/-/p-cancelable-2.1.1.tgz", "resolved": "https://registry.npmjs.org/p-cancelable/-/p-cancelable-3.0.0.tgz",
"integrity": "sha512-BZOr3nRQHOntUjTrH8+Lh54smKHoHyur8We1V8DSMVrl5A2malOOwuJRnKRDjSnkoeBh4at6BwEnb5I7Jl31wg==" "integrity": "sha512-mlVgR3PGuzlo0MmTdk4cXqXWlwQDLnONTAg6sm62XkMJEiRxN3GL3SffkYvqwonbkJBcrI7Uvv5Zh9yjvn2iUw=="
}, },
"parseurl": { "parseurl": {
"version": "1.3.3", "version": "1.3.3",

View File

@ -15,7 +15,7 @@
"ejs": "~3.1.6", "ejs": "~3.1.6",
"express": "~4.17.1", "express": "~4.17.1",
"form-data": "^4.0.0", "form-data": "^4.0.0",
"got": "^11.8.2", "got": "^12.0.1",
"http-errors": "~1.8.0", "http-errors": "~1.8.0",
"jquery": "^3.6.0", "jquery": "^3.6.0",
"moment": "^2.29.1", "moment": "^2.29.1",

View File

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

View File

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

View File

@ -32,8 +32,8 @@ function getStatus() {
function setStatus(state) { function setStatus(state) {
if (fs.existsSync(statusPath)) { if (fs.existsSync(statusPath)) {
let old_state_str = fs.readFileSync(statusPath).toString(); let old_state_str = fs.readFileSync(statusPath).toString();
if(old_state_str !== JSON.stringify(state)){ if(old_state_str !== JSON.stringify(state, null, 2)){
fs.writeFileSync(statusPath, JSON.stringify(state)); fs.writeFileSync(statusPath, JSON.stringify(state, null, 2));
hassioApiTools.publish_state(state); hassioApiTools.publish_state(state);
} }
}else{ }else{