Skip to content

Commit

Permalink
Merge pull request #1447 from libris/feature/lxl-4487
Browse files Browse the repository at this point in the history
Add a script-running housekeeping template, and schedule running of W…
  • Loading branch information
jannistsiroyannis committed Jul 22, 2024
2 parents 8ad635c + 12b99d3 commit 8eece1a
Show file tree
Hide file tree
Showing 5 changed files with 431 additions and 6 deletions.
1 change: 1 addition & 0 deletions housekeeping/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ dependencies {
// XL dependencies
implementation(project(':whelk-core'))
implementation(project(':server-common'))
implementation(project(':whelktool'))

// Groovy
implementation "org.codehaus.groovy:groovy:${groovyVersion}"
Expand Down
82 changes: 82 additions & 0 deletions housekeeping/src/main/groovy/whelk/housekeeping/ScriptRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package whelk.housekeeping;

import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import whelk.Whelk;
import whelk.housekeeping.HouseKeeper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

public class ScriptRunner extends HouseKeeper {
private String status = "OK";
private final String scriptName;
private final String schedule;
private final Logger logger = LogManager.getLogger(this.getClass());
private final Whelk premadeWhelk;

public ScriptRunner(Whelk premadeWhelk, String scriptName, String schedule) {
this.scriptName = scriptName;
this.schedule = schedule;
this.premadeWhelk = premadeWhelk;
}

public String getName() {
return "Scriptrunner: " + scriptName;
}

public String getStatusDescription() {
return status;
}

public String getCronSchedule() {
return schedule;
}

public void trigger() {
try {
InputStream scriptStream = ScriptRunner.class.getClassLoader().getResourceAsStream(scriptName);
String scriptText = IOUtils.toString(new InputStreamReader(scriptStream));

Path scriptWorkingDir = Files.createTempDirectory("housekeeping_script_execution");
Path scriptFilePath = scriptWorkingDir.resolve("script.groovy");
Path reportPath = scriptWorkingDir.resolve("report");
Files.createDirectories(reportPath);

Files.writeString(scriptFilePath, scriptText);
String[] args =
{
"--allow-loud",
"--report",
reportPath.toString(),
scriptFilePath.toString(),
};
whelk.datatool.WhelkTool.main2(args, premadeWhelk);

Path errorLogPath = reportPath.resolve("ERRORS.txt");
if (Files.size(errorLogPath) > 0) {
String firstError = "[could not read]";
try (BufferedReader errorLogReader = Files.newBufferedReader(errorLogPath)) {
firstError = errorLogReader.readLine();
}
String errorMessage = "Execution of script (" + scriptName + ") resulted in non-zero size ERRORS.txt, first line of which is: "
+ firstError + "\nSee: " + errorLogPath;
logger.error(errorMessage);
status = errorMessage;
} else {
status = "OK";
}

} catch (Exception e) {
logger.error("Failed script (" + scriptName + ") execution attempt: ", e);
throw new RuntimeException(e);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ public class WebInterface extends HttpServlet {
// Automatic generation is disabled for now, may need design changes approved before activation.
//new NotificationGenerator(whelk),
//new NotificationSender(whelk),

new InquirySender(whelk),
new NotificationCleaner(whelk),
new ImageLinker(whelk),
new ExportSizePredictor(whelk),
new ScriptRunner(whelk, "wikidatalinking.groovy", "0 19 10 1,3,5,7,9,11 *"),
]

houseKeepers.each { hk ->
Expand Down
Loading

0 comments on commit 8eece1a

Please sign in to comment.