This commit is contained in:
Michele Artini 2021-04-23 09:19:15 +02:00
parent 764c6a3ee2
commit a0da981c29
1 changed files with 22 additions and 12 deletions

View File

@ -4,6 +4,7 @@ import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletRequest;
@ -41,14 +42,13 @@ public class OpenaireInternalApiController extends AbstractDnetController {
synchronized (lastExecution) {
if (lastExecution.getStatus() != ImportStatus.RUNNING) {
lastExecution.startNew();
lastExecution.startNew("Importing dedup events - request from " + req.getRemoteAddr());
new Thread(() -> {
try {
databaseUtils.importDedupEvents();
lastExecution.complete();
} catch (final Throwable e) {
lastExecution.fail(e);
log.error("Error importing conflicts and duplicates", e);
}
}).start();
} else {
@ -85,7 +85,7 @@ public class OpenaireInternalApiController extends AbstractDnetController {
private Long dateStart;
private Long dateEnd;
private ImportStatus status = ImportStatus.NOT_LAUNCHED;
private String error;
private String message;
public String getId() {
return id;
@ -119,31 +119,41 @@ public class OpenaireInternalApiController extends AbstractDnetController {
this.status = status;
}
public String getError() {
return error;
public String getMessage() {
return message;
}
public void setError(final String error) {
this.error = error;
public void setMessage(final String message) {
this.message = message;
}
public void startNew() {
public void startNew(final String message) {
setId("import-" + UUID.randomUUID());
setDateStart(new Date().getTime());
setDateStart(System.currentTimeMillis());
setDateEnd(null);
setStatus(ImportStatus.RUNNING);
setError(null);
setMessage(message);
log.info(message);
}
public void complete() {
setDateEnd(new Date().getTime());
setDateEnd(System.currentTimeMillis());
setStatus(ImportStatus.SUCCESS);
final long millis = getDateEnd() - getDateStart();
setMessage(String
.format("Import of dedup events completed in %d min, %d sec", TimeUnit.MILLISECONDS.toMinutes(millis), TimeUnit.MILLISECONDS.toSeconds(millis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))));
log.info(getMessage());
}
public void fail(final Throwable e) {
setDateEnd(new Date().getTime());
setStatus(ImportStatus.FAILED);
setError(e.getMessage());
setMessage(e.getMessage());
log.error("Error importing conflicts and duplicates", e);
}
}