Skip to content

Commit

Permalink
Added new process
Browse files Browse the repository at this point in the history
  • Loading branch information
Matus Kasak committed Nov 19, 2024
1 parent dad2ea2 commit 2c8c732
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 0 deletions.
140 changes: 140 additions & 0 deletions dspace-api/src/main/java/org/dspace/administer/ProcessCleaner2.java
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);
}

}
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 {

}
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> {

}
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;
}

}
5 changes: 5 additions & 0 deletions dspace/config/spring/api/scripts.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@
<property name="dspaceRunnableClass" value="org.dspace.administer.ProcessCleanerCli"/>
</bean>

<bean id="process-cleaner-2" class="org.dspace.administer.ProcessCleanerCliConfiguration2">
<property name="description" value="Cleanup all the old processes in the specified state"/>
<property name="dspaceRunnableClass" value="org.dspace.administer.ProcessCleanerCli2"/>
</bean>

<bean id="filter-media" class="org.dspace.app.mediafilter.MediaFilterScriptConfiguration">
<property name="description" value="Perform the media filtering to extract full text from documents and to create thumbnails"/>
<property name="dspaceRunnableClass" value="org.dspace.app.mediafilter.MediaFilterScript"/>
Expand Down
5 changes: 5 additions & 0 deletions dspace/config/spring/rest/scripts.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
<property name="dspaceRunnableClass" value="org.dspace.administer.ProcessCleaner"/>
</bean>

<bean id="process-cleaner-2" class="org.dspace.administer.ProcessCleanerCliConfiguration2">
<property name="description" value="Cleanup all the old processes in the specified state"/>
<property name="dspaceRunnableClass" value="org.dspace.administer.ProcessCleanerCli2"/>
</bean>

<bean id="orcid-bulk-push" class="org.dspace.orcid.script.OrcidBulkPushScriptConfiguration" primary="true">
<property name="description" value="Perform the bulk synchronization of all the BATCH configured ORCID entities placed in the ORCID queue"/>
<property name="dspaceRunnableClass" value="org.dspace.orcid.script.OrcidBulkPush"/>
Expand Down

0 comments on commit 2c8c732

Please sign in to comment.