Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a script-running housekeeping template, and schedule running of W… #1447

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading