ScheduledEasySMTAChecking.java
/*
* Copyright 2020 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 java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import org.genesys.server.component.security.AsAdminInvoker;
import org.genesys.server.model.genesys.MaterialRequest;
import org.genesys.server.model.genesys.QMaterialRequest;
import org.genesys.server.persistence.MaterialRequestRepository;
import org.genesys.server.service.RequestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.querydsl.core.types.Predicate;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
/**
* Scheduled checking of Easy-SMTA accounts of incomplete requests younger than 1 month.
* Validates and sends requests to the genebanks if Easy-SMTA account exists.
*
* Task runs on 1 cluster node every 8 hours
*
* @author Maxym Borodenko
*/
@Component
public class ScheduledEasySMTAChecking {
/** The Constant LOG. */
public static final Logger LOG = LoggerFactory.getLogger(ScheduledEasySMTAChecking.class);
/** The request service. */
@Autowired
private RequestService requestService;
/** The request repository. */
@Autowired
private MaterialRequestRepository requestRepository;
/** Execute code as admin */
@Autowired
private AsAdminInvoker asAdminInvoker;
// @Scheduled(cron = "0 0/1 * * * *") // Test every 1 min
@Scheduled(cron = "0 0 */8 ? * *") // every 8 hours
@SchedulerLock(name = "org.genesys.server.service.worker.ScheduledEasySMTAChecking")
public void runChecking() throws Exception {
Instant inst = ZonedDateTime.now(ZoneOffset.UTC).minusMonths(1).toInstant();
Predicate predicate = QMaterialRequest.materialRequest.state.eq(MaterialRequest.NOTVALIDATED)
.and(QMaterialRequest.materialRequest.internalRequest.isFalse())
.and(QMaterialRequest.materialRequest.createdDate.after(inst));
asAdminInvoker.invoke(() -> {
requestRepository.findAll(predicate).forEach(mr -> {
try {
requestService.validateRequest(requestService.get(mr.getUuid()));
} catch (Exception e) {
LOG.warn("Cannot validate material request for {}: {}", mr.getEmail(), e.getMessage());
}
});
return true;
});
}
}