Skip to content

Commit

Permalink
Merge pull request #50 from gnmyt/updates/cron-migration
Browse files Browse the repository at this point in the history
⏰ Umstieg auf Cron-Ausdrücke
  • Loading branch information
gnmyt authored Aug 29, 2022
2 parents 96248fb + d1b35b5 commit 4831540
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 43 deletions.
29 changes: 18 additions & 11 deletions client/src/common/components/Dropdown/DropdownComponent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function DropdownComponent() {
...dialog(config[key]),
onSuccess: value => {
fetch("/api/config/" + key, {headers, method: "PATCH", body: JSON.stringify({value})})
.then(() => showFeedback());
.then(res => showFeedback(!res.ok ? "Deine Änderungen wurden nicht übernommen. Überprüfe deine Eingabe." : undefined));
}
})
}
Expand Down Expand Up @@ -218,22 +218,29 @@ function DropdownComponent() {
});
}

const updateLevel = async () => {
const updateCronManually = () => patchDialog("cron", (value) => ({
title: <>Test-Häufigkeit einstellen <a href="https://crontab.guru/" target="_blank">?</a></>, placeholder: "Cron-Regel", value: value,
}), false);

const updateCron = async () => {
toggleDropdown();
setDialog({
title: "Test-Häufigkeit einstellen",
select: true,
selectOptions: {
1: "Durchgehend (jede Minute)",
2: "Sehr häufig (alle 30 Minuten)",
3: "Standard (jede Stunde)",
4: "Selten (alle 3 Stunden)",
5: "Sehr selten (alle 6 Stunden)"
"* * * * *": "Durchgehend (jede Minute)",
"0,30 * * * *": "Sehr häufig (alle 30 Minuten)",
"0 * * * *": "Standard (jede Stunde)",
"0 0,3,6,9,12,15,18,21 * * *": "Selten (alle 3 Stunden)",
"0 0,6,12,18 * * *": "Sehr selten (alle 6 Stunden)"
},
value: config.timeLevel,
value: config.cron,
unsetButton: true,
unsetButtonText: "Manuell festlegen",
onClear: updateCronManually,
onSuccess: value => {
fetch("/api/config/timeLevel", {headers: headers, method: "PATCH", body: JSON.stringify({value: value})})
.then(() => showFeedback(undefined, false));
fetch("/api/config/cron", {headers: headers, method: "PATCH", body: JSON.stringify({value: value})})
.then(() => showFeedback());
}
});
}
Expand Down Expand Up @@ -309,7 +316,7 @@ function DropdownComponent() {
<FontAwesomeIcon icon={faKey}/>
<h3>Passwort ändern</h3>
</div>
<div className="dropdown-item" onClick={updateLevel}>
<div className="dropdown-item" onClick={updateCron}>
<FontAwesomeIcon icon={faClock}/>
<h3>Häufigkeit einstellen</h3>
</div>
Expand Down
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"dependencies": {
"axios": "^0.27.2",
"bcrypt": "^5.0.1",
"cron-validator": "^1.3.1",
"decompress": "^4.2.1",
"decompress-targz": "^4.1.1",
"decompress-unzip": "^4.0.1",
Expand Down
2 changes: 1 addition & 1 deletion server/controller/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const configDefaults = {
"ping": "25",
"download": "100",
"upload": "50",
"timeLevel": "3",
"cron": "0 * * * *",
"serverId": "none",
"password": "none",
"healthChecksUrl": "https://hc-ping.com/<uuid>"
Expand Down
2 changes: 1 addition & 1 deletion server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const run = async () => {
await config.insertDefaults();

// Start all timer
timerTask.startTimer((await config.get("timeLevel")).value);
timerTask.startTimer((await config.get("cron")).value);
setInterval(async () => require('./tasks/speedtest').removeOld(), 60000);

// Start Healthchecks
Expand Down
10 changes: 7 additions & 3 deletions server/routes/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const app = require('express').Router();
const config = require('../controller/config');
const timer = require('../tasks/timer');
const cron = require('cron-validator');

// Gets all config entries
app.get("/", async (req, res) => {
Expand All @@ -24,19 +25,22 @@ app.get("/:key", async (req, res) => {
app.patch("/:key", async (req, res) => {
if (!req.body.value) return res.status(400).json({message: "You need to provide the new value"});

if ((req.params.key === "ping" || req.params.key === "download" || req.params.key === "upload" || req.params.key === "timeLevel") && isNaN(req.body.value))
if ((req.params.key === "ping" || req.params.key === "download" || req.params.key === "upload") && isNaN(req.body.value))
return res.status(400).json({message: "You need to provide a number in order to change this"});

if (req.params.key === "ping")
req.body.value = req.body.value.toString().split(".")[0];

if (req.params.key === "password" && req.body.value !== "none") req.body.value = await require('bcrypt').hash(req.body.value, 10);

if (req.params.key === "cron" && !cron.isValidCron(req.body.value.toString()))
return res.status(500).json({message: "Not a valid cron expression"});

if (!await config.update(req.params.key, req.body.value)) return res.status(404).json({message: "The provided key does not exist"});

if (req.params.key === "timeLevel") {
if (req.params.key === "cron") {
timer.stopTimer();
timer.startTimer(req.body.value);
timer.startTimer(req.body.value.toString());
}

res.json({message: `The key '${req.params.key}' has been successfully updated`});
Expand Down
30 changes: 4 additions & 26 deletions server/tasks/timer.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,12 @@
const pauseController = require('../controller/pause');
const schedule = require('node-schedule');
const {isValidCron} = require("cron-validator");

let job;

module.exports.startTimer = (timeLevel) => {
job = schedule.scheduleJob(getRuleFromLevel(timeLevel), () => this.runTask());
}

const getRuleFromLevel = (level) => {
const rule = new schedule.RecurrenceRule();

switch (level) {
case "1":
rule.second = 0;
break;
case "2":
rule.minute = [0, 30]
break;
case "4":
rule.hour = [0, 3, 6, 9, 12, 15, 18, 21];
break;
case "5":
rule.hour = [0, 6, 12, 18];
break;
default:
rule.minute = 0;
break;
}

return rule;
module.exports.startTimer = (cron) => {
if (!isValidCron(cron)) return;
job = schedule.scheduleJob(cron, () => this.runTask());
}

module.exports.runTask = async () => {
Expand Down
8 changes: 7 additions & 1 deletion server/util/speedtest.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ module.exports = async (serverId, binary_path = './bin/speedtest' + (process.pla
const line = buffer.toString().replace("\n", "");
if (!line.startsWith("{")) return;

let data = JSON.parse(line);
let data;
try {
data = JSON.parse(line);
} catch (e) {
data.error = e.message;
}

if (data.error) result.error = data.error;

if (data.type === "result") result = data;
Expand Down

0 comments on commit 4831540

Please sign in to comment.