Merge branch 'ref/getInstance-vs-setInstance' into 'master'

Ref/get instance vs set instance

See merge request sheepitrenderfarm/client!345
This commit is contained in:
Sheepit Renderfarm
2024-11-20 23:13:00 +00:00
10 changed files with 32 additions and 33 deletions

View File

@@ -94,7 +94,7 @@ import okhttp3.HttpUrl;
public Client(Gui gui, Configuration configuration, String url) {
this.configuration = configuration;
this.server = new Server(url, this.configuration, this);
this.log = Log.getInstance(this.configuration);
this.log = Log.getInstance();
this.gui = gui;
this.directoryManager = new DirectoryManager(this.configuration);
this.renderingJob = null;

View File

@@ -72,19 +72,19 @@ public class DirectoryManager {
Files.deleteIfExists(targetArchivePath); // createLink only works if the target does not exist
try {
Files.createLink(targetArchivePath, existingArchivePath);
Log.getInstance(configuration).debug("Created hardlink from " + targetArchivePath + " to " + existingArchivePath);
Log.getInstance().debug("Created hardlink from " + targetArchivePath + " to " + existingArchivePath);
}
catch (UnsupportedOperationException // underlying file system does not support hard-linking
| FileSystemException // cache-dir and shared-zip are on separate file systems, even though hard-linking is supported
| SecurityException // user is not allowed to create hard-links
ignore) {
// Creating hardlinks might not be supported on some filesystems
Log.getInstance(configuration).debug("Failed to create hardlink, falling back to copying file to " + targetArchivePath);
Log.getInstance().debug("Failed to create hardlink, falling back to copying file to " + targetArchivePath);
Files.copy(existingArchivePath, targetArchivePath, StandardCopyOption.REPLACE_EXISTING);
}
}
catch (IOException e) {
Log.getInstance(configuration).error("Error while copying " + source + " from shared downloads directory to working dir");
Log.getInstance().error("Error while copying " + source + " from shared downloads directory to working dir");
return false;
}

View File

@@ -19,6 +19,8 @@
package com.sheepit.client;
import lombok.Getter;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
@@ -43,7 +45,7 @@ public final class Log {
public final int MAX_SIZE = 10000000; // in B
public final int PERIOD = 3600000; // duration in ms between two shrink
private static Log instance = null;
@Getter private static Log instance = null;
private Map<Integer, List<String>> checkpoints = new HashMap<>();
private int lastCheckPoint;
@@ -170,8 +172,7 @@ public final class Log {
}
}
public static synchronized Log getInstance(Configuration config) {
if (instance == null) {
public static synchronized void setInstance(Configuration config) {
String path = null;
boolean print = false;
boolean debug = false;
@@ -183,11 +184,9 @@ public final class Log {
instance = new Log(debug, print, path);
}
return instance;
}
public static synchronized void printCheckPoint(int point_) {
Log log = getInstance(null);
Log log = getInstance();
Optional<List<String>> logs = log.getForCheckPoint(point_);
if (logs.isPresent()) {
for (String alog : logs.get()) {

View File

@@ -107,7 +107,7 @@ public class Server extends Thread {
this.base_url = url_;
this.user_config = user_config_;
this.client = client_;
this.log = Log.getInstance(this.user_config);
this.log = Log.getInstance();
this.directoryManager = new DirectoryManager(this.user_config);
this.lastRequestTime = 0;
this.keepmealive_duration = 15 * 60 * 1000; // default 15min

View File

@@ -42,7 +42,7 @@ import java.io.InputStreamReader;
* The detail is limited to only if it's 32 or 64bit
*/
private final String arch = detectArch();
private static final Log log = Log.getInstance(null);
private static final Log log = Log.getInstance();
private static Integer logicalCores = -1; // Store core count throughout instances

View File

@@ -81,7 +81,7 @@ public class Linux extends OS {
actual_command.add(0, NICE_BINARY_PATH);
}
else {
Log.getInstance(null).error("No low priority binary, will not launch renderer in normal priority");
Log.getInstance().error("No low priority binary, will not launch renderer in normal priority");
}
ProcessBuilder builder = new ProcessBuilder(actual_command);
@@ -141,7 +141,7 @@ public class Linux extends OS {
hasNiceBinary = true;
}
catch (IOException e) {
Log.getInstance(null).error("Failed to find low priority binary, will not launch renderer in normal priority (" + e + ")");
Log.getInstance().error("Failed to find low priority binary, will not launch renderer in normal priority (" + e + ")");
}
finally {
if (process != null) {

View File

@@ -54,7 +54,7 @@ public class Mac extends OS {
actual_command.add(0, NICE_BINARY_PATH);
}
else {
Log.getInstance(null).error("No low priority binary, will not launch renderer in normal priority");
Log.getInstance().error("No low priority binary, will not launch renderer in normal priority");
}
ProcessBuilder builder = new ProcessBuilder(actual_command);
builder.redirectErrorStream(true);
@@ -111,7 +111,7 @@ public class Mac extends OS {
hasNiceBinary = true;
}
catch (IOException e) {
Log.getInstance(null).error("Failed to find low priority binary, will not launch renderer in normal priority (" + e + ")");
Log.getInstance().error("Failed to find low priority binary, will not launch renderer in normal priority (" + e + ")");
}
finally {
if (process != null) {

View File

@@ -53,7 +53,7 @@ public class GuiText implements Gui {
public GuiText() {
this.downloadProgress = new DownloadProgress(this);
this.framesRendered = 0;
this.log = Log.getInstance(null);
this.log = Log.getInstance();
this.df = new SimpleDateFormat("MMM dd HH:mm:ss");
this.eta = "";
this.statusOld = "";

View File

@@ -152,8 +152,8 @@ public class Worker {
if (sharedDownloadsDir != null) {
File dir = new File(sharedDownloadsDir);
if (dir.exists() == false && dir.mkdirs()) {
Log.getInstance(config).debug("created shared-zip directory " + dir);
if (dir.exists() == false && dir.mkdirs() == false) {
System.err.println("ERROR: Failed to create shared-zip directory " + dir);
}
else if (dir.exists() == false) {
System.err.println("ERROR: The shared-zip directory " + dir + " does not exist and cannot be automatically created");
@@ -176,10 +176,7 @@ public class Worker {
File a_dir = new File(cache_dir);
a_dir.mkdirs();
if (a_dir.isDirectory() && a_dir.canWrite()) {
DirectoryManager directoryManager = new DirectoryManager(config);
config.setCacheDir(a_dir);
directoryManager.createCacheDir();
}
else {
System.err.println("ERROR: The entered cache path is either not a directory or is not writable!");
@@ -474,7 +471,10 @@ public class Worker {
String configFilePath = config.getConfigFilePath() != null ? config.getConfigFilePath() : OS.getOS().getDefaultConfigFilePath();
config.setLogDirectory(log_dir != null ? log_dir : (new File (configFilePath).getParent()));
Log.getInstance(config).debug("client version " + Configuration.jarVersion);
(new DirectoryManager(config)).createCacheDir();
Log.setInstance(config);
Log.getInstance().debug("client version " + Configuration.jarVersion);
// Hostname change will overwrite the existing one (default or read from configuration file) but changes will be lost when the client closes
if (hostname != null) {

View File

@@ -111,7 +111,7 @@ public class Working implements Activity {
sessionUploadsStatsValue = new JLabel("0KB");
currentTheme = UIManager.getLookAndFeel().getName(); // Capture the theme on component instantiation
previousStatus = "";
log = Log.getInstance(parent_.getConfiguration());
log = Log.getInstance();
//Overwrite the window close button behaviour to showa dialogue
parent.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);