forked from DSpace/DSpace
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Matus Kasak
committed
Nov 19, 2024
1 parent
dad2ea2
commit 2c8c732
Showing
6 changed files
with
239 additions
and
0 deletions.
There are no files selected for viewing
140 changes: 140 additions & 0 deletions
140
dspace-api/src/main/java/org/dspace/administer/ProcessCleaner2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/** | ||
* The contents of this file are subject to the license and copyright | ||
* detailed in the LICENSE and NOTICE files at the root of the source | ||
* tree and available online at | ||
* | ||
* http://www.dspace.org/license/ | ||
*/ | ||
package org.dspace.administer; | ||
|
||
import org.apache.commons.cli.ParseException; | ||
import org.apache.commons.lang.time.DateUtils; | ||
import org.dspace.authorize.AuthorizeException; | ||
import org.dspace.content.ProcessStatus; | ||
import org.dspace.core.Context; | ||
import org.dspace.scripts.DSpaceRunnable; | ||
import org.dspace.scripts.Process; | ||
import org.dspace.scripts.factory.ScriptServiceFactory; | ||
import org.dspace.scripts.service.ProcessService; | ||
import org.dspace.services.ConfigurationService; | ||
import org.dspace.services.factory.DSpaceServicesFactory; | ||
import org.dspace.utils.DSpace; | ||
|
||
import java.io.IOException; | ||
import java.sql.SQLException; | ||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.List; | ||
|
||
/** | ||
* Script to cleanup the old processes in the specified state. | ||
* | ||
* @author Luca Giamminonni (luca.giamminonni at 4science.it) | ||
* | ||
*/ | ||
public class ProcessCleaner2 extends DSpaceRunnable<ProcessCleanerConfiguration2<ProcessCleaner2>> { | ||
|
||
private ConfigurationService configurationService; | ||
|
||
private ProcessService processService; | ||
|
||
|
||
private boolean cleanCompleted = false; | ||
|
||
private boolean cleanFailed = false; | ||
|
||
private boolean cleanRunning = false; | ||
|
||
private boolean help = false; | ||
|
||
private Integer days; | ||
|
||
|
||
@Override | ||
public void setup() throws ParseException { | ||
|
||
this.configurationService = DSpaceServicesFactory.getInstance().getConfigurationService(); | ||
this.processService = ScriptServiceFactory.getInstance().getProcessService(); | ||
|
||
this.help = commandLine.hasOption('h'); | ||
this.cleanFailed = commandLine.hasOption('f'); | ||
this.cleanRunning = commandLine.hasOption('r'); | ||
this.cleanCompleted = commandLine.hasOption('c') || (!cleanFailed && !cleanRunning); | ||
|
||
this.days = configurationService.getIntProperty("process-cleaner.days", 14); | ||
|
||
if (this.days <= 0) { | ||
throw new IllegalStateException("The number of days must be a positive integer."); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public void internalRun() throws Exception { | ||
|
||
if (help) { | ||
printHelp(); | ||
return; | ||
} | ||
|
||
Context context = new Context(); | ||
|
||
try { | ||
context.turnOffAuthorisationSystem(); | ||
performDeletion(context); | ||
} finally { | ||
context.restoreAuthSystemState(); | ||
context.complete(); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Delete the processes based on the specified statuses and the configured days | ||
* from their creation. | ||
*/ | ||
private void performDeletion(Context context) throws SQLException, IOException, AuthorizeException { | ||
|
||
List<ProcessStatus> statuses = getProcessToDeleteStatuses(); | ||
Date creationDate = calculateCreationDate(); | ||
|
||
handler.logInfo("Searching for processes with status: " + statuses); | ||
List<Process> processes = processService.findByStatusAndCreationTimeOlderThan(context, statuses, creationDate); | ||
handler.logInfo("Found " + processes.size() + " processes to be deleted"); | ||
for (Process process : processes) { | ||
processService.delete(context, process); | ||
} | ||
|
||
handler.logInfo("Process cleanup completed"); | ||
|
||
} | ||
|
||
/** | ||
* Returns the list of Process statuses do be deleted. | ||
*/ | ||
private List<ProcessStatus> getProcessToDeleteStatuses() { | ||
List<ProcessStatus> statuses = new ArrayList<ProcessStatus>(); | ||
if (cleanCompleted) { | ||
statuses.add(ProcessStatus.COMPLETED); | ||
} | ||
if (cleanFailed) { | ||
statuses.add(ProcessStatus.FAILED); | ||
} | ||
if (cleanRunning) { | ||
statuses.add(ProcessStatus.RUNNING); | ||
} | ||
return statuses; | ||
} | ||
|
||
private Date calculateCreationDate() { | ||
return DateUtils.addDays(new Date(), -days); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public ProcessCleanerConfiguration2<ProcessCleaner2> getScriptConfiguration() { | ||
return new DSpace().getServiceManager() | ||
.getServiceByName("process-cleaner-2", ProcessCleanerConfiguration2.class); | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
dspace-api/src/main/java/org/dspace/administer/ProcessCleanerCli2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* The contents of this file are subject to the license and copyright | ||
* detailed in the LICENSE and NOTICE files at the root of the source | ||
* tree and available online at | ||
* | ||
* http://www.dspace.org/license/ | ||
*/ | ||
package org.dspace.administer; | ||
|
||
/** | ||
* The {@link ProcessCleaner} for CLI. | ||
* | ||
* @author Luca Giamminonni (luca.giamminonni at 4science.it) | ||
* | ||
*/ | ||
public class ProcessCleanerCli2 extends ProcessCleaner2 { | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
dspace-api/src/main/java/org/dspace/administer/ProcessCleanerCliConfiguration2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* The contents of this file are subject to the license and copyright | ||
* detailed in the LICENSE and NOTICE files at the root of the source | ||
* tree and available online at | ||
* | ||
* http://www.dspace.org/license/ | ||
*/ | ||
package org.dspace.administer; | ||
|
||
/** | ||
* The {@link ProcessCleanerConfiguration} for CLI. | ||
* | ||
* @author Luca Giamminonni (luca.giamminonni at 4science.it) | ||
* | ||
*/ | ||
public class ProcessCleanerCliConfiguration2 extends ProcessCleanerConfiguration2<ProcessCleanerCli2> { | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
dspace-api/src/main/java/org/dspace/administer/ProcessCleanerConfiguration2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* The contents of this file are subject to the license and copyright | ||
* detailed in the LICENSE and NOTICE files at the root of the source | ||
* tree and available online at | ||
* | ||
* http://www.dspace.org/license/ | ||
*/ | ||
package org.dspace.administer; | ||
|
||
import org.apache.commons.cli.Options; | ||
import org.dspace.scripts.configuration.ScriptConfiguration; | ||
|
||
/** | ||
* The {@link ScriptConfiguration} for the {@link ProcessCleaner2} script. | ||
*/ | ||
public class ProcessCleanerConfiguration2<T extends ProcessCleaner2> extends ScriptConfiguration<T> { | ||
|
||
private Class<T> dspaceRunnableClass; | ||
|
||
@Override | ||
public Options getOptions() { | ||
if (options == null) { | ||
|
||
Options options = new Options(); | ||
|
||
options.addOption("h", "help", false, "help"); | ||
|
||
options.addOption("r", "running", false, "delete the process with RUNNING status"); | ||
options.getOption("r").setType(boolean.class); | ||
|
||
options.addOption("f", "failed", false, "delete the process with FAILED status"); | ||
options.getOption("f").setType(boolean.class); | ||
|
||
options.addOption("c", "completed", false, | ||
"delete the process with COMPLETED status (default if no statuses are specified)"); | ||
options.getOption("c").setType(boolean.class); | ||
|
||
super.options = options; | ||
} | ||
return options; | ||
} | ||
|
||
@Override | ||
public Class<T> getDspaceRunnableClass() { | ||
return dspaceRunnableClass; | ||
} | ||
|
||
@Override | ||
public void setDspaceRunnableClass(Class<T> dspaceRunnableClass) { | ||
this.dspaceRunnableClass = dspaceRunnableClass; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters