KpiScheduledExecutor.java
/*
* 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.genesys.server.service.worker;
import org.apache.commons.lang3.time.StopWatch;
import org.genesys.server.component.security.AsAdminInvoker;
import org.genesys.server.model.kpi.Execution;
import org.genesys.server.persistence.kpi.ExecutionRepository;
import org.genesys.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;
import org.springframework.transaction.annotation.Transactional;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
/**
* 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;
@Autowired
private ExecutionRepository executionRepository;
/**
* Run executions.
*
* @throws Exception the exception
*/
@Scheduled(cron = "0 0 3 * * *")
// @Scheduled(cron = "0 0/10 * * * *") // Test every 10min
@SchedulerLock(name = "org.genesys.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(PageRequest.of(page, chunkSize));
for (Execution execution : executions.getContent()) {
if (! execution.isActive()) {
LOG.info("Skipping deactivated execution {}.", execution.getName());
continue;
}
try {
LOG.info("Started execution {} after {}ms", execution.getName(), stopWatch.getTime());
kpiService.executeAndSave(execution);
LOG.info("Execution {} successful after {}ms", execution.getName(), stopWatch.getTime());
} catch (Throwable e) {
LOG.error("Error running KPI Execution {}: {}", execution.getName(), e.getMessage(), e);
}
}
page++;
} while (executions.getTotalPages() < page);
LOG.info("Run of {} executions ended successfully after {}ms.", executions.getTotalElements(), stopWatch.getTime());
return true;
});
}
@Transactional
@Scheduled(fixedDelayString = "PT2H", initialDelayString = "PT5M")
@SchedulerLock(name = "org.genesys.server.service.worker.KpiExecutor")
public void removeOldRuns() throws Exception {
asAdminInvoker.invoke(() -> {
LOG.info("Running scheduled removal of old runs across all executions");
executionRepository.findAll().forEach(execution -> {
LOG.info("Scheduled removal of old runs of execution {} id={}", execution.getTitle(), execution.getId());
kpiService.purgeExecutionRuns(execution);
});
return true;
});
}
}