Commit 51833dd1 authored by Matija Obreza's avatar Matija Obreza

Merge branch '367-scheduled-kpi-execution' into 'master'

Resolve "@Scheduled KPI execution"

Closes #367

See merge request genesys-pgr/genesys-server!282
parents 99ec18fb 697913c6
......@@ -308,6 +308,16 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.javacrumbs.shedlock</groupId>
<artifactId>shedlock-spring</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>net.javacrumbs.shedlock</groupId>
<artifactId>shedlock-provider-hazelcast</artifactId>
<version>0.18.2</version>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
......
/*
* 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<Execution> 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;
});
}
}
......@@ -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);
}
}
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment