All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.otto.edison.jobs.controller.JobDefinitionsController Maven / Gradle / Ivy

There is a newer version: 3.3.2
Show newest version
package de.otto.edison.jobs.controller;

import de.otto.edison.configuration.EdisonApplicationProperties;
import de.otto.edison.jobs.definition.JobDefinition;
import de.otto.edison.jobs.domain.JobMeta;
import de.otto.edison.jobs.service.JobDefinitionService;
import de.otto.edison.jobs.service.JobMetaService;
import de.otto.edison.navigation.NavBar;
import de.otto.edison.navigation.configuration.NavBarConfiguration;
import de.otto.edison.status.domain.Link;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.time.Duration;
import java.util.*;

import static de.otto.edison.jobs.controller.JobDefinitionRepresentation.representationOf;
import static de.otto.edison.navigation.NavBarItem.navBarItem;
import static de.otto.edison.status.domain.Link.link;
import static de.otto.edison.util.UrlHelper.baseUriOf;
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
import static java.util.stream.Collectors.toList;
import static jakarta.servlet.http.HttpServletResponse.SC_NOT_FOUND;
import static org.springframework.web.bind.annotation.RequestMethod.GET;

@Controller
@ConditionalOnProperty(prefix = "edison.jobs", name = "external-trigger", havingValue = "true", matchIfMissing = true)
@EnableConfigurationProperties(EdisonApplicationProperties.class)
public class JobDefinitionsController {

    private final String jobDefinitionsUri;

    private final JobDefinitionService jobDefinitionService;
    private final JobMetaService jobMetaService;
    private final EdisonApplicationProperties applicationProperties;

    @Autowired
    public JobDefinitionsController(final JobDefinitionService definitionService,
                                    final JobMetaService jobMetaService,
                                    @Qualifier(NavBarConfiguration.RIGHT_NAV_BAR) final NavBar rightNavBar,
                                    final EdisonApplicationProperties applicationProperties) {
        this.jobDefinitionService = definitionService;
        this.jobMetaService = jobMetaService;
        this.applicationProperties = applicationProperties;
        jobDefinitionsUri = String.format("%s/jobdefinitions", applicationProperties.getManagement().getBasePath());
        rightNavBar.register(navBarItem(10, "Job Definitions", jobDefinitionsUri));
    }

    @RequestMapping(value = "${edison.application.management.base-path:/internal}/jobdefinitions", method = GET, produces = "application/json")
    @ResponseBody
    public Map> getJobDefinitionsAsJson(final HttpServletRequest request) {
        final String baseUri = baseUriOf(request);
        return singletonMap("links", new ArrayList() {{
            addAll(jobDefinitionService.getJobDefinitions()
                    .stream()
                    .map((def) -> link(
                            "http://github.com/otto-de/edison/link-relations/job/definition",
                            baseUri + jobDefinitionsUri + "/" + def.jobType(),
                            def.jobName()))
                    .collect(toList()));
            add(link("self", baseUriOf(request) + jobDefinitionsUri, "Self"));
        }});
    }

    @RequestMapping(value = "${edison.application.management.base-path:/internal}/jobdefinitions", method = GET, produces = "*/*")
    public ModelAndView getJobDefinitionsAsHtml(final HttpServletRequest request) {
        return new ModelAndView("jobdefinitions", new HashMap() {{
            put("baseUri", baseUriOf(request));
            put("jobdefinitions", jobDefinitionService.getJobDefinitions()
                    .stream()
                    .map((def) -> {
                        final JobMeta jobMeta = jobMetaService.getJobMeta(def.jobType());
                        return new HashMap() {{
                            put("isDisabled", jobMeta != null && jobMeta.isDisabled());
                            put("comment", jobMeta != null ? jobMeta.getDisabledComment() : "");
                            put("jobType", def.jobType());
                            put("name", def.jobName());
                            put("description", def.description());
                            put("maxAge", def.maxAge().isPresent() ? def.maxAge().get().toMinutes() + " Minutes" : "unlimited");
                            put("frequency", frequencyOf(def));
                            put("retry", retryOf(def));
                        }};
                    })
                    .collect(toList()));
        }});
    }

    @RequestMapping(value = "${edison.application.management.base-path:/internal}/jobdefinitions/{jobType}", method = GET, produces = "application/json")
    @ResponseBody
    public JobDefinitionRepresentation getJobDefinition(final @PathVariable String jobType,
                                                        final HttpServletRequest request,
                                                        final HttpServletResponse response) throws IOException {

        Optional jobDefinition = jobDefinitionService.getJobDefinition(jobType);
        if (jobDefinition.isPresent()) {
            return representationOf(jobDefinition.get(), baseUriOf(request), applicationProperties.getManagement().getBasePath());
        } else {
            response.sendError(SC_NOT_FOUND, "Job not found");
            return null;
        }
    }

    @RequestMapping(value = "${edison.application.management.base-path:/internal}/jobdefinitions/{jobType}", method = GET, produces = "*/*")
    public ModelAndView getJobDefinitionAsHtml(final @PathVariable String jobType,
                                               final HttpServletRequest request,
                                               final HttpServletResponse response) throws IOException {
        final JobMeta jobMeta = jobMetaService.getJobMeta(jobType);
        final Optional> optionalResult = jobDefinitionService.getJobDefinition(jobType)
                .map((def) -> new HashMap() {{
                    put("isDisabled", jobMeta.isDisabled());
                    put("comment", jobMeta.getDisabledComment());
                    put("jobType", def.jobType());
                    put("name", def.jobName());
                    put("description", def.description());
                    put("maxAge", def.maxAge().isPresent() ? def.maxAge().get().toMinutes() + " Minutes" : "unlimited");
                    put("frequency", frequencyOf(def));
                    put("retry", retryOf(def));
                }});
        if (optionalResult.isPresent()) {
            return new ModelAndView("jobdefinitions", new HashMap() {{
                put("baseUri", baseUriOf(request));
                put("jobdefinitions", singletonList(optionalResult.get()));
            }});
        } else {
            response.sendError(SC_NOT_FOUND, "JobDefinition " + jobType + " not found.");
            return null;
        }
    }

    private String frequencyOf(final JobDefinition def) {
        if (def.cron().isPresent()) {
            return def.cron().get();
        } else {
            return fixedDelayFrequency(def.fixedDelay());
        }
    }

    private String fixedDelayFrequency(Optional duration) {
        if (duration.isPresent()) {
            if (duration.get().toMinutes() < 1) {
                return "Every " + duration.get().toMillis() / 1000 + " Seconds";
            } else {
                return "Every " + duration.get().toMinutes() + " Minutes";
            }
        } else {
            return "Never";
        }
    }

    private String retryOf(final JobDefinition def) {
        final String delay = def.retryDelay().isPresent() ? " with " + def.retryDelay().get().getSeconds() + " seconds delay." : ".";
        return def.retries() == 0 ? "Do not retry triggering" : "Retry trigger " + def.retries() + " times" + delay;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy