From 697913c651ec0227793fb48111e66a6e8d7ab5fe Mon Sep 17 00:00:00 2001 From: Pavlov Viacheslav Date: Fri, 9 Nov 2018 14:01:47 +0200 Subject: [PATCH] @Scheduled KPI execution - increased max lock duration, fixed lockProvider bean init --- pom.xml | 10 +++ .../service/worker/KpiScheduledExecutor.java | 85 +++++++++++++++++++ .../spring/config/HazelcastConfig.java | 7 ++ src/main/resources/log4j.properties | 1 + 4 files changed, 103 insertions(+) create mode 100644 src/main/java/org/genesys2/server/service/worker/KpiScheduledExecutor.java diff --git a/pom.xml b/pom.xml index e6488ac8d..812f73b0c 100644 --- a/pom.xml +++ b/pom.xml @@ -308,6 +308,16 @@ test + + net.javacrumbs.shedlock + shedlock-spring + 2.1.0 + + + net.javacrumbs.shedlock + shedlock-provider-hazelcast + 0.18.2 + com.querydsl querydsl-jpa diff --git a/src/main/java/org/genesys2/server/service/worker/KpiScheduledExecutor.java b/src/main/java/org/genesys2/server/service/worker/KpiScheduledExecutor.java new file mode 100644 index 000000000..631970d6a --- /dev/null +++ b/src/main/java/org/genesys2/server/service/worker/KpiScheduledExecutor.java @@ -0,0 +1,85 @@ +/* + * Copyright 2018 Global Crop Diversity Trust + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.genesys2.server.service.worker; + +import net.javacrumbs.shedlock.core.SchedulerLock; + +import org.apache.commons.lang3.time.StopWatch; +import org.genesys2.server.model.kpi.Execution; +import org.genesys2.server.security.AsAdminInvoker; +import org.genesys2.server.service.KPIService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +/** + * Component to periodically run all KPI Executions + */ +@Component +public class KpiScheduledExecutor { + + /** The Constant LOG. */ + public static final Logger LOG = LoggerFactory.getLogger(KpiScheduledExecutor.class); + + /** The kpi service. */ + @Autowired + private KPIService kpiService; + + /** The as admin invoker. */ + @Autowired + protected AsAdminInvoker asAdminInvoker; + + /** + * Run executions. + * + * @throws Exception the exception + */ + @Scheduled(cron = "0 0 3 * * *") + // @Scheduled(cron = "0 0/10 * * * *") // Test every 10min + @SchedulerLock(name = "org.genesys2.server.service.worker.KpiExecutor") + public void runExecutions() throws Exception { + LOG.info("Started scheduled executions run"); + StopWatch stopWatch = StopWatch.createStarted(); + asAdminInvoker.invoke(() -> { + final int chunkSize = 50; + int page = 0; + Page executions; + do { + executions = kpiService.listExecutions(new PageRequest(page, chunkSize)); + for (Execution ex : executions.getContent()) { + try { + LOG.info("Started execution {} after {}ms", ex.getName(), stopWatch.getTime()); + kpiService.execute(ex); + LOG.info("Execution {} successful after {}ms", ex.getName(), stopWatch.getTime()); + } catch (Throwable e) { + LOG.error("Error running KPI Execution {}: {}", ex.getName(), e.getMessage(), e); + } + } + page++; + } while (executions.getTotalPages() < page); + + if (executions != null) { + LOG.info("Run of {} executions ended successfully after {}ms.", executions.getTotalElements(), stopWatch.getTime()); + } + return true; + }); + } +} diff --git a/src/main/java/org/genesys2/spring/config/HazelcastConfig.java b/src/main/java/org/genesys2/spring/config/HazelcastConfig.java index c0835d474..4bfc2afe1 100644 --- a/src/main/java/org/genesys2/spring/config/HazelcastConfig.java +++ b/src/main/java/org/genesys2/spring/config/HazelcastConfig.java @@ -23,6 +23,8 @@ import java.util.Arrays; import java.util.List; import java.util.Properties; +import net.javacrumbs.shedlock.provider.hazelcast.HazelcastLockProvider; +import net.javacrumbs.shedlock.spring.annotation.EnableSchedulerLock; import org.apache.commons.lang3.StringUtils; import org.genesys.blocks.security.lockout.AccountLockoutManager.AttemptStatistics; import org.genesys2.spring.hazelcast.HazelcastCacheRegionFactoryWrapper; @@ -59,6 +61,7 @@ import com.hazelcast.web.spring.SpringAwareWebFilter; @Configuration @EnableCaching +@EnableSchedulerLock(defaultLockAtMostFor = "PT15M") public abstract class HazelcastConfig { protected final Logger LOG = LoggerFactory.getLogger(HazelcastConfig.class); @@ -406,4 +409,8 @@ public abstract class HazelcastConfig { return properties; } + @Bean + public HazelcastLockProvider lockProvider(HazelcastInstance hazelcastInstance) { + return new HazelcastLockProvider(hazelcastInstance); + } } diff --git a/src/main/resources/log4j.properties b/src/main/resources/log4j.properties index 135a204fa..b6d63c044 100644 --- a/src/main/resources/log4j.properties +++ b/src/main/resources/log4j.properties @@ -29,6 +29,7 @@ log4j.rootLogger=error, stdout log4j.category.org.genesys2=warn log4j.category.org.genesys=warn log4j.category.org.genesys2.server.api=warn +log4j.category.org.genesys2.server.service.worker.KpiScheduledExecutor=info log4j.category.liquibase=debug #log4j.category.springfox=trace #log4j.category.org.genesys2.server.servlet.controller=debug -- GitLab