Skip to content

Commit

Permalink
Add possibility to add user defined resources
Browse files Browse the repository at this point in the history
  • Loading branch information
lukfor committed Sep 14, 2024
1 parent 53b2dcf commit e4fca96
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@
</div>
{{else}}
<h5 class="card-title"><i class="fas fa-times-circle text-danger"></i> {{name}}</h5>
{{{error}}}
<div class="log-pane">
<pre>{{error}}</pre>
</div>
{{/enabled}}
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@ public String getVersion() {
if (isInstalled()) {
String binary = getBinary();
Command command = new Command(binary, "version");
command.saveStdOut(FileUtil.path("docker-version.txt"));
StringBuffer stdout = new StringBuffer();
command.writeStdout(stdout);
command.setSilent(true);
command.execute();
String version = FileUtil.readFileAsString("docker-version.txt");
FileUtil.deleteFile("docker-version.txt");
return version;
return stdout.toString();
} else {
return "Docker not installed.";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,11 @@ public boolean isInstalled() {
public String getVersion() {
if (isInstalled()) {
Command command = new Command(binary, "info");
command.saveStdOut(FileUtil.path("info-version.txt"));
StringBuffer stdout = new StringBuffer();
command.writeStdout(stdout);
command.setSilent(true);
command.execute();
String version = FileUtil.readFileAsString("info-version.txt");
FileUtil.deleteFile("info-version.txt");
return version;
return stdout.toString();
} else {
return "Nextflow not installed.";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import cloudgene.mapred.server.responses.ServerResponse;
import cloudgene.mapred.server.responses.StatisticsResponse;
import cloudgene.mapred.server.services.ServerService;
import cloudgene.mapred.util.TextUtil;
import genepi.io.FileUtil;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.http.MediaType;
Expand Down Expand Up @@ -88,8 +89,7 @@ public String getDetails() {
public String getLogs() {
File file = new File(LOG_FILENAME);
if (file.exists()) {
String content = serverService.tail(file, 1000);
return content;
return TextUtil.tail(file, 1000);
} else {
return "No log file available.";
}
Expand Down
82 changes: 30 additions & 52 deletions src/main/java/cloudgene/mapred/server/services/ServerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.*;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

import cloudgene.mapred.plugins.nextflow.NextflowPlugin;
import cloudgene.mapred.util.command.Command;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
Expand Down Expand Up @@ -211,6 +208,34 @@ public String getClusterDetails() {
plugins.add(pluginObject);
}

//check user defined resources
for (Map<String, String> resource: application.getSettings().getResources()){
String name = resource.get("name");
ObjectNode pluginObject = mapper.createObjectNode();
pluginObject.put("name", name);
if (!resource.containsKey("command")) {
pluginObject.put("error", "Command defined in resource '" + name + "'");
}
String cmd = resource.get("command");
String[] tiles = cmd.split(" ");
String[] params = Arrays.copyOfRange(tiles, 1, tiles.length);
Command command = new Command(tiles[0], params);
command.setSilent(true);
StringBuffer output = new StringBuffer();
StringBuffer error = new StringBuffer();
command.writeStdout(output);
command.writeStderr(error);
int exitCode = command.execute();
if (exitCode == 0) {
pluginObject.put("enabled", true);
pluginObject.put("details", output.toString());
}else {
pluginObject.put("enabled", false);
pluginObject.put("error", output.toString()+ "\n" + error.toString());
}
plugins.add(pluginObject);
}

// database
object.put("db_max_active", application.getDatabase().getDataSource().getMaxActive());
object.put("db_active", application.getDatabase().getDataSource().getNumActive());
Expand All @@ -222,53 +247,6 @@ public String getClusterDetails() {
return object.toString();
}

public String tail(File file, int lines) {
java.io.RandomAccessFile fileHandler = null;
try {
fileHandler = new java.io.RandomAccessFile(file, "r");
long fileLength = fileHandler.length() - 1;
StringBuilder sb = new StringBuilder();
int line = 0;

for (long filePointer = fileLength; filePointer != -1; filePointer--) {
fileHandler.seek(filePointer);
int readByte = fileHandler.readByte();

if (readByte == 0xA) {
line = line + 1;
if (line == lines) {
if (filePointer == fileLength) {
continue;
}
break;
}
} else if (readByte == 0xD) {
line = line + 1;
if (line == lines) {
if (filePointer == fileLength - 1) {
continue;
}
break;
}
}
sb.append((char) readByte);
}

String lastLine = sb.reverse().toString();
return lastLine;
} catch (java.io.IOException e) {
log.error("Parsing log file failed.", e);
return null;
} finally {
if (fileHandler != null)
try {
fileHandler.close();
} catch (IOException e) {
log.error("Parsing log file failed.", e);
}
}
}

public void updateNextflowConfig(String content) {
NextflowPlugin plugin = (NextflowPlugin) PluginManager.getInstance().getPlugin(NextflowPlugin.ID);
String filename = plugin.getNextflowConfig();
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/cloudgene/mapred/util/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public class Settings {

private List<Map<String, String>> errorHandlers = new Vector<Map<String, String>>();

private List<Map<String, String>> resources = new Vector<>();

private int autoRetireInterval = 5;

private int retireAfter = 6;
Expand Down Expand Up @@ -389,6 +391,14 @@ public List<Map<String, String>> getErrorHandlers() {
return errorHandlers;
}

public void setResources(List<Map<String, String>> resources) {
this.resources = resources;
}

public List<Map<String, String>> getResources() {
return resources;
}

public void setRepository(ApplicationRepository repository) {
this.repository = repository;
}
Expand Down
59 changes: 59 additions & 0 deletions src/main/java/cloudgene/mapred/util/TextUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package cloudgene.mapred.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;

public class TextUtil {

private static final Logger log = LoggerFactory.getLogger(TextUtil.class);

public static String tail(File file, int lines) {
java.io.RandomAccessFile fileHandler = null;
try {
fileHandler = new java.io.RandomAccessFile(file, "r");
long fileLength = fileHandler.length() - 1;
StringBuilder sb = new StringBuilder();
int line = 0;

for (long filePointer = fileLength; filePointer != -1; filePointer--) {
fileHandler.seek(filePointer);
int readByte = fileHandler.readByte();

if (readByte == 0xA) {
line = line + 1;
if (line == lines) {
if (filePointer == fileLength) {
continue;
}
break;
}
} else if (readByte == 0xD) {
line = line + 1;
if (line == lines) {
if (filePointer == fileLength - 1) {
continue;
}
break;
}
}
sb.append((char) readByte);
}

String lastLine = sb.reverse().toString();
return lastLine;
} catch (java.io.IOException e) {
log.error("Parsing log file failed.", e);
return null;
} finally {
if (fileHandler != null)
try {
fileHandler.close();
} catch (IOException e) {
log.error("Parsing log file failed.", e);
}
}
}
}
Loading

0 comments on commit e4fca96

Please sign in to comment.