From 3b364ae81568529ffe86ba549be091b7ba95a25d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20F=C3=BCrderer?= Date: Mon, 15 Jun 2020 16:23:42 +0200 Subject: [PATCH] Allow employees to trigger cronjobs manually --- .../controller/cronjob/CronjobController.java | 57 ++++++++++++++++++ .../resources/static/css/manual-cronjobs.css | 35 +++++++++++ .../resources/templates/fragments/intern.html | 2 + .../templates/intern/cronjobs/index.html | 60 +++++++++++++++++++ 4 files changed, 154 insertions(+) create mode 100644 prototype/src/main/resources/static/css/manual-cronjobs.css create mode 100644 prototype/src/main/resources/templates/intern/cronjobs/index.html diff --git a/prototype/src/main/java/org/hso/ecommerce/controller/cronjob/CronjobController.java b/prototype/src/main/java/org/hso/ecommerce/controller/cronjob/CronjobController.java index 0f54221..dbf7c6c 100644 --- a/prototype/src/main/java/org/hso/ecommerce/controller/cronjob/CronjobController.java +++ b/prototype/src/main/java/org/hso/ecommerce/controller/cronjob/CronjobController.java @@ -1,6 +1,7 @@ package org.hso.ecommerce.controller.cronjob; import java.sql.Timestamp; +import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; import java.util.GregorianCalendar; @@ -37,6 +38,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; interface ICronjob { /** @@ -63,10 +69,22 @@ interface ICronjob { * @param controller Back-reference that allows to use repositories. */ void executeAt(Calendar time, CronjobController controller); + + /** + * Get a name for this cronjob, that can be presented to the user in the frontend. + * + * @return A german name of this cronjob. + */ + String getDisplayName(); } @Component class Reorder implements ICronjob { + @Override + public String getDisplayName() { + return "Nachbestellung"; + } + @Override public Calendar nextExecution(Calendar reference) { if (reference.get(Calendar.HOUR_OF_DAY) >= 8) { @@ -215,6 +233,7 @@ class ScheduledCronjob { } @Component +@RequestMapping("intern/cronjobs") class CronjobController { private static final Logger log = LoggerFactory.getLogger(CronjobController.class); @@ -328,4 +347,42 @@ class CronjobController { public void onPostConstruct() { new Thread(this::runCronjobExecutionLoop).start(); } + + class ManualCronjob { + public final String identifier; + public final String visibleName; + + public ManualCronjob(String identifier, String visibleName) { + this.identifier = identifier; + this.visibleName = visibleName; + } + } + + private List listAllCronjobs() { + ArrayList entries = new ArrayList<>(); + for (Entry job : cronjobs.entrySet()) { + entries.add(new ManualCronjob(job.getKey(), job.getValue().getDisplayName())); + } + return entries; + } + + @GetMapping("/") + public String cronjobOverview(Model model) { + model.addAttribute("jobs", listAllCronjobs()); + return "intern/cronjobs/index"; + } + + @PostMapping("/run/{identifier}") + public String runCronjob(Model model, @PathVariable("identifier") String identifier) { + ICronjob jobToExecute = cronjobs.get(identifier); + if (jobToExecute != null) { + jobToExecute.executeAt(new GregorianCalendar(), this); + model.addAttribute("info", + String.format("Der Cronjob \"%s\" wurde ausgeführt.", jobToExecute.getDisplayName())); + } else { + model.addAttribute("error", "Der Cronjob konnte nicht gefunden werden."); + } + model.addAttribute("jobs", listAllCronjobs()); + return "intern/cronjobs/index"; + } } diff --git a/prototype/src/main/resources/static/css/manual-cronjobs.css b/prototype/src/main/resources/static/css/manual-cronjobs.css new file mode 100644 index 0000000..19205d5 --- /dev/null +++ b/prototype/src/main/resources/static/css/manual-cronjobs.css @@ -0,0 +1,35 @@ +.alert { + display: block; + text-transform: uppercase; + background: #c0392b; + color: white; + animation: blinking 1s linear infinite; + overflow: hidden; +} + +.alert > span { + display: inline-block; + padding-left: 100%; + animation: marquee 10s linear infinite; + white-space: nowrap; +} + +@keyframes marquee { + 0% { transform: translateX(0); } + 100% { transform: translateX(-100%); } +} + +@keyframes blinking { + 60% { + color: white; + } + 75% { + color: #c0392b; + } + 85% { + color: #c0392b; + } + 100% { + color: white; + } +} diff --git a/prototype/src/main/resources/templates/fragments/intern.html b/prototype/src/main/resources/templates/fragments/intern.html index 22de534..eee1c8d 100644 --- a/prototype/src/main/resources/templates/fragments/intern.html +++ b/prototype/src/main/resources/templates/fragments/intern.html @@ -47,6 +47,8 @@ +
  • Cronjobs
  • + diff --git a/prototype/src/main/resources/templates/intern/cronjobs/index.html b/prototype/src/main/resources/templates/intern/cronjobs/index.html new file mode 100644 index 0000000..ef7e681 --- /dev/null +++ b/prototype/src/main/resources/templates/intern/cronjobs/index.html @@ -0,0 +1,60 @@ + + + + + + + + Cronjobs + + + + + + + + +
    + +
    +

    Manuelle Ausführung von Cronjobs

    +
    + WARNUNG
    + Manuelles Triggern von Cronjobs kann zu unerwartetem Verhalten führen!
    + Diese Seite ist nur für Debugging-Zwecke gedacht. +
    + +

    + + + + + + + + + + + + +
    + +
    Cronjob
    +
    + +
    +
    +

    + +
    +
    + + + +