Skip to content

Commit

Permalink
Merge branch 'w2p-116609_improve-running-process-observability' into …
Browse files Browse the repository at this point in the history
…w2p-116609_improve-running-process-observability-main

 Conflicts:
	dspace-api/src/main/java/org/dspace/scripts/ProcessServiceImpl.java
  • Loading branch information
nona-luypaert committed Dec 23, 2024
2 parents 070fe68 + 7787550 commit 40ac29f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 29 deletions.
47 changes: 18 additions & 29 deletions dspace-api/src/main/java/org/dspace/scripts/ProcessServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,12 @@
import org.dspace.eperson.Group;
import org.dspace.scripts.service.ProcessService;
import org.dspace.services.ConfigurationService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;

/**
* The implementation for the {@link ProcessService} class
*/
public class ProcessServiceImpl implements ProcessService, InitializingBean {
public class ProcessServiceImpl implements ProcessService {

private static final Logger log = org.apache.logging.log4j.LogManager.getLogger(ProcessService.class);

Expand All @@ -75,33 +74,6 @@ public class ProcessServiceImpl implements ProcessService, InitializingBean {
@Autowired
private ConfigurationService configurationService;

@Override
public void afterPropertiesSet() throws Exception {
try {
Context context = new Context();

// Processes that were running or scheduled when tomcat crashed, should be cleaned up during startup.
List<Process> processesToBeFailed = findByStatusAndCreationTimeOlderThan(
context, List.of(ProcessStatus.RUNNING, ProcessStatus.SCHEDULED), new Date());
for (Process process : processesToBeFailed) {
context.setCurrentUser(process.getEPerson());
// Fail the process.
log.info("Process with ID {} did not complete before tomcat shutdown, failing it now.",
process.getID());
fail(context, process);
// But still attach its log to the process.
appendLog(process.getID(), process.getName(),
"Process did not complete before tomcat shutdown.",
ProcessLogLevel.ERROR);
createLogBitstream(context, process);
}

context.complete();
} catch (Exception e) {
log.error("Unable to clean up Processes: ", e);
}
}

@Override
public Process create(Context context, EPerson ePerson, String scriptName,
List<DSpaceCommandLineParameter> parameters,
Expand Down Expand Up @@ -359,6 +331,23 @@ public int countByUser(Context context, EPerson user) throws SQLException {
return processDAO.countByUser(context, user);
}

@Override
public void failRunningProcesses(Context context) throws SQLException, IOException, AuthorizeException {
List<Process> processesToBeFailed = findByStatusAndCreationTimeOlderThan(
context, List.of(ProcessStatus.RUNNING, ProcessStatus.SCHEDULED), new Date());
for (Process process : processesToBeFailed) {
context.setCurrentUser(process.getEPerson());
// Fail the process.
log.info("Process with ID {} did not complete before tomcat shutdown, failing it now.", process.getID());
fail(context, process);
// But still attach its log to the process.
appendLog(process.getID(), process.getName(),
"Process did not complete before tomcat shutdown.",
ProcessLogLevel.ERROR);
createLogBitstream(context, process);
}
}

private String formatLogLine(int processId, String scriptName, String output, ProcessLogLevel processLogLevel) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
StringBuilder sb = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,14 @@ List<Process> findByStatusAndCreationTimeOlderThan(Context context, List<Process
* @throws SQLException If something goes wrong
*/
int countByUser(Context context, EPerson user) throws SQLException;

/**
* Cleans up running processes by failing them an attaching their logs to the process objects.
*
* @param context The DSpace context
* @throws SQLException
* @throws IOException
* @throws AuthorizeException
*/
void failRunningProcesses(Context context) throws SQLException, IOException, AuthorizeException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -65,6 +66,12 @@ public class ProcessRestRepository extends DSpaceRestRepository<ProcessRest, Int
@Autowired
private EPersonService epersonService;

@PostConstruct
public void init() throws SQLException, AuthorizeException, IOException {
Context context = new Context();
processService.failRunningProcesses(context);
context.complete();
}

@Override
@PreAuthorize("hasPermission(#id, 'PROCESS', 'READ')")
Expand Down

0 comments on commit 40ac29f

Please sign in to comment.