diff --git a/src/main/java/edu/harvard/iq/dataverse/harvest/client/ClientHarvestRun.java b/src/main/java/edu/harvard/iq/dataverse/harvest/client/ClientHarvestRun.java index 0dc94f835e9..c04c5fb7008 100644 --- a/src/main/java/edu/harvard/iq/dataverse/harvest/client/ClientHarvestRun.java +++ b/src/main/java/edu/harvard/iq/dataverse/harvest/client/ClientHarvestRun.java @@ -40,12 +40,13 @@ public void setId(Long id) { this.id = id; } - public enum RunResultType { SUCCESS, FAILURE, INPROGRESS }; + public enum RunResultType { SUCCESS, RUN_FAILED, RUN_IN_PROGRESS, DELETE_FAILED }; private static String RESULT_LABEL_SUCCESS = "SUCCESS"; - private static String RESULT_LABEL_FAILURE = "FAILED"; - private static String RESULT_LABEL_INPROGRESS = "IN PROGRESS"; - private static String RESULT_DELETE_IN_PROGRESS = "DELETE IN PROGRESS"; + private static String RESULT_LABEL_RUN_FAILED = "RUN FAILED"; + private static String RESULT_LABEL_RUN_IN_PROGRESS = "RUN IN PROGRESS"; + private static String RESULT_LABEL_DELETE_IN_PROGRESS = "DELETE IN PROGRESS"; + private static String RESULT_LABEL_DELETE_FAILED = "DELETE FAILED"; @ManyToOne @JoinColumn(nullable = false) @@ -67,34 +68,34 @@ public RunResultType getResult() { public String getResultLabel() { if (harvestingClient != null && harvestingClient.isDeleteInProgress()) { - return RESULT_DELETE_IN_PROGRESS; - } - - if (isSuccess()) { + return RESULT_LABEL_DELETE_IN_PROGRESS; + } else if (isDeleteFailed()) { + return RESULT_LABEL_DELETE_FAILED; + } else if (isSuccess()) { return RESULT_LABEL_SUCCESS; - } else if (isFailed()) { - return RESULT_LABEL_FAILURE; - } else if (isInProgress()) { - return RESULT_LABEL_INPROGRESS; + } else if (isRunFailed()) { + return RESULT_LABEL_RUN_FAILED; + } else if (isRunInProgress()) { + return RESULT_LABEL_RUN_IN_PROGRESS; } return null; } public String getDetailedResultLabel() { if (harvestingClient != null && harvestingClient.isDeleteInProgress()) { - return RESULT_DELETE_IN_PROGRESS; - } - if (isSuccess()) { + return RESULT_LABEL_DELETE_IN_PROGRESS; + } else if (isDeleteFailed()) { + return RESULT_LABEL_DELETE_FAILED; + } else if (isSuccess()) { String resultLabel = RESULT_LABEL_SUCCESS; - resultLabel = resultLabel.concat("; "+harvestedDatasetCount+" harvested, "); resultLabel = resultLabel.concat(deletedDatasetCount+" deleted, "); resultLabel = resultLabel.concat(failedDatasetCount+" failed."); return resultLabel; - } else if (isFailed()) { - return RESULT_LABEL_FAILURE; - } else if (isInProgress()) { - return RESULT_LABEL_INPROGRESS; + } else if (isRunFailed()) { + return RESULT_LABEL_RUN_FAILED; + } else if (isRunInProgress()) { + return RESULT_LABEL_RUN_IN_PROGRESS; } return null; } @@ -111,21 +112,29 @@ public void setSuccess() { harvestResult = RunResultType.SUCCESS; } - public boolean isFailed() { - return RunResultType.FAILURE == harvestResult; + public boolean isRunFailed() { + return RunResultType.RUN_FAILED == harvestResult; } - public void setFailed() { - harvestResult = RunResultType.FAILURE; + public void setRunFailed() { + harvestResult = RunResultType.RUN_FAILED; } - public boolean isInProgress() { - return RunResultType.INPROGRESS == harvestResult || + public boolean isRunInProgress() { + return RunResultType.RUN_IN_PROGRESS == harvestResult || (harvestResult == null && startTime != null && finishTime == null); } - public void setInProgress() { - harvestResult = RunResultType.INPROGRESS; + public void setRunInProgress() { + harvestResult = RunResultType.RUN_IN_PROGRESS; + } + + public boolean isDeleteFailed() { + return RunResultType.DELETE_FAILED == harvestResult; + } + + public void setDeleteFailed() { + harvestResult = RunResultType.DELETE_FAILED; } // Time of this harvest attempt: diff --git a/src/main/java/edu/harvard/iq/dataverse/harvest/client/HarvestingClientServiceBean.java b/src/main/java/edu/harvard/iq/dataverse/harvest/client/HarvestingClientServiceBean.java index 0af73550190..ee607adbb9e 100644 --- a/src/main/java/edu/harvard/iq/dataverse/harvest/client/HarvestingClientServiceBean.java +++ b/src/main/java/edu/harvard/iq/dataverse/harvest/client/HarvestingClientServiceBean.java @@ -5,8 +5,6 @@ import edu.harvard.iq.dataverse.DataverseRequestServiceBean; import edu.harvard.iq.dataverse.DataverseServiceBean; import edu.harvard.iq.dataverse.EjbDataverseEngine; -import edu.harvard.iq.dataverse.engine.command.exception.CommandException; -import edu.harvard.iq.dataverse.engine.command.impl.DeleteHarvestingClientCommand; import edu.harvard.iq.dataverse.search.IndexServiceBean; import edu.harvard.iq.dataverse.timer.DataverseTimerServiceBean; import java.util.ArrayList; @@ -18,6 +16,7 @@ import javax.ejb.Stateless; import javax.ejb.TransactionAttribute; import javax.ejb.TransactionAttributeType; + import javax.inject.Inject; import javax.inject.Named; import javax.persistence.EntityManager; @@ -87,9 +86,9 @@ public void resetHarvestInProgress(Long hcId) { // And if there is an unfinished RunResult object, we'll // just mark it as a failure: - if (harvestingClient.getLastRun() != null - && harvestingClient.getLastRun().isInProgress()) { - harvestingClient.getLastRun().setFailed(); + ClientHarvestRun lastRun = harvestingClient.getLastRun(); + if (lastRun != null && lastRun.isRunInProgress()) { + lastRun.setRunFailed(); } } @@ -108,9 +107,22 @@ public void setHarvestInProgress(Long hcId, Date startTime) { ClientHarvestRun currentRun = new ClientHarvestRun(); currentRun.setHarvestingClient(harvestingClient); currentRun.setStartTime(startTime); - currentRun.setInProgress(); + currentRun.setRunInProgress(); harvestingClient.getRunHistory().add(currentRun); } + + @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) + public void resetDeleteInProgress(Long hcId) { + HarvestingClient harvestingClient = em.find(HarvestingClient.class, hcId); + if (harvestingClient == null) { + return; + } + em.refresh(harvestingClient); + if (harvestingClient.isDeleteInProgress()) { + harvestingClient.setDeleteInProgress(false); + harvestingClient.getLastRun().setDeleteFailed(); + } + } @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) public void setDeleteInProgress(Long hcId) { @@ -175,7 +187,7 @@ public void setHarvestSuccess(Long hcId, Date currentTime, int harvestedCount, i ClientHarvestRun currentRun = harvestingClient.getLastRun(); - if (currentRun != null && currentRun.isInProgress()) { + if (currentRun != null && currentRun.isRunInProgress()) { // TODO: what if there's no current run in progress? should we just // give up quietly, or should we make a noise of some kind? -- L.A. 4.4 @@ -197,11 +209,11 @@ public void setHarvestFailure(Long hcId, Date currentTime) { ClientHarvestRun currentRun = harvestingClient.getLastRun(); - if (currentRun != null && currentRun.isInProgress()) { + if (currentRun != null && currentRun.isRunInProgress()) { // TODO: what if there's no current run in progress? should we just // give up quietly, or should we make a noise of some kind? -- L.A. 4.4 - currentRun.setFailed(); + currentRun.setRunFailed(); currentRun.setFinishTime(currentTime); } } diff --git a/src/main/java/edu/harvard/iq/dataverse/harvest/client/ResetHarvestInProgressBean.java b/src/main/java/edu/harvard/iq/dataverse/harvest/client/ResetHarvestInProgressBean.java new file mode 100644 index 00000000000..39fc4c084af --- /dev/null +++ b/src/main/java/edu/harvard/iq/dataverse/harvest/client/ResetHarvestInProgressBean.java @@ -0,0 +1,23 @@ +package edu.harvard.iq.dataverse.harvest.client; + +import javax.annotation.PostConstruct; +import javax.ejb.EJB; +import javax.ejb.Singleton; +import javax.ejb.Startup; + +@Singleton +@Startup +public class ResetHarvestInProgressBean { + + @EJB + HarvestingClientServiceBean harvestingClientService; + + @PostConstruct + public void init() { + for (HarvestingClient client : harvestingClientService.getAllHarvestingClients()) { + harvestingClientService.resetHarvestInProgress(client.getId()); + harvestingClientService.resetDeleteInProgress(client.getId()); + } + } + +}