Skip to content

Commit

Permalink
Merge pull request DSpace#9728 from atmire/w2p-116609_improve-running…
Browse files Browse the repository at this point in the history
…-process-observability-main

Improve observability of running processes + Fail them during startup
  • Loading branch information
tdonohue authored Dec 5, 2024
2 parents 16427a5 + 070fe68 commit 1b16a55
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 16 deletions.
64 changes: 53 additions & 11 deletions dspace-api/src/main/java/org/dspace/scripts/ProcessServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@
import org.dspace.core.LogHelper;
import org.dspace.eperson.EPerson;
import org.dspace.eperson.Group;
import org.dspace.eperson.service.EPersonService;
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 {
public class ProcessServiceImpl implements ProcessService, InitializingBean {

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

Expand All @@ -72,7 +73,34 @@ public class ProcessServiceImpl implements ProcessService {
private MetadataFieldService metadataFieldService;

@Autowired
private EPersonService ePersonService;
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,
Expand Down Expand Up @@ -293,8 +321,8 @@ public int countSearch(Context context, ProcessQueryParameterContainer processQu
@Override
public void appendLog(int processId, String scriptName, String output, ProcessLogLevel processLogLevel)
throws IOException {
File tmpDir = FileUtils.getTempDirectory();
File tempFile = new File(tmpDir, scriptName + processId + ".log");
File logsDir = getLogsDirectory();
File tempFile = new File(logsDir, processId + "-" + scriptName + ".log");
FileWriter out = new FileWriter(tempFile, true);
try {
try (BufferedWriter writer = new BufferedWriter(out)) {
Expand All @@ -309,12 +337,15 @@ public void appendLog(int processId, String scriptName, String output, ProcessLo
@Override
public void createLogBitstream(Context context, Process process)
throws IOException, SQLException, AuthorizeException {
File tmpDir = FileUtils.getTempDirectory();
File tempFile = new File(tmpDir, process.getName() + process.getID() + ".log");
FileInputStream inputStream = FileUtils.openInputStream(tempFile);
appendFile(context, process, inputStream, Process.OUTPUT_TYPE, process.getName() + process.getID() + ".log");
inputStream.close();
tempFile.delete();
File logsDir = getLogsDirectory();
File tempFile = new File(logsDir, process.getID() + "-" + process.getName() + ".log");
if (tempFile.exists()) {
FileInputStream inputStream = FileUtils.openInputStream(tempFile);
appendFile(context, process, inputStream, Process.OUTPUT_TYPE,
process.getID() + "-" + process.getName() + ".log");
inputStream.close();
tempFile.delete();
}
}

@Override
Expand Down Expand Up @@ -343,4 +374,15 @@ private String formatLogLine(int processId, String scriptName, String output, Pr
return sb.toString();
}

private File getLogsDirectory() {
String pathStr = configurationService.getProperty("dspace.dir")
+ File.separator + "log" + File.separator + "processes";
File logsDir = new File(pathStr);
if (!logsDir.exists()) {
if (!logsDir.mkdirs()) {
throw new RuntimeException("Couldn't create [dspace.dir]/log/processes/ directory.");
}
}
return logsDir;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public void handleCompletion() {

@Override
public void handleException(Exception e) {
handleException(null, e);
handleException(e.getMessage(), e);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -929,10 +929,10 @@ public void getProcessOutput() throws Exception {
getClient(token).perform(get("/api/system/processes/" + process1.getID() + "/output"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name",
is(process1.getName() + process1.getID() + ".log")))
is(process1.getID() + "-" + process1.getName() + ".log")))
.andExpect(jsonPath("$.type", is("bitstream")))
.andExpect(jsonPath("$.metadata['dc.title'][0].value",
is(process1.getName() + process1.getID() + ".log")))
is(process1.getID() + "-" + process1.getName() + ".log")))
.andExpect(jsonPath("$.metadata['dspace.process.filetype'][0].value",
is("script_output")));

Expand All @@ -942,10 +942,10 @@ public void getProcessOutput() throws Exception {
.perform(get("/api/system/processes/" + process1.getID() + "/output"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name",
is(process1.getName() + process1.getID() + ".log")))
is(process1.getID() + "-" + process1.getName() + ".log")))
.andExpect(jsonPath("$.type", is("bitstream")))
.andExpect(jsonPath("$.metadata['dc.title'][0].value",
is(process1.getName() + process1.getID() + ".log")))
is(process1.getID() + "-" + process1.getName() + ".log")))
.andExpect(jsonPath("$.metadata['dspace.process.filetype'][0].value",
is("script_output")));

Expand Down

0 comments on commit 1b16a55

Please sign in to comment.