Merge branch 'ref/waitForever' into 'master'

Ref: clean up code

See merge request sheepitrenderfarm/client!281
This commit is contained in:
Sheepit Renderfarm
2023-12-07 15:18:28 +00:00

View File

@@ -42,7 +42,6 @@ import java.util.TimerTask;
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.sheepit.client.Error.ServerCode; import com.sheepit.client.Error.ServerCode;
@@ -216,15 +215,7 @@ import okhttp3.HttpUrl;
// it means the client has to wait until the next day // it means the client has to wait until the next day
wait += 24 * 3600 * 1000; wait += 24 * 3600 * 1000;
} }
try { this.sleep(wait);
Thread.sleep(wait);
}
catch (InterruptedException e3) {
}
catch (IllegalArgumentException e3) {
this.log.error("Client::run sleepA failed " + e3);
}
} }
this.gui.status("Requesting Job"); this.gui.status("Requesting Job");
this.renderingJob = this.server.requestJob(); this.renderingJob = this.server.requestJob();
@@ -235,36 +226,15 @@ import okhttp3.HttpUrl;
} }
catch (SheepItExceptionSessionDisabled e) { catch (SheepItExceptionSessionDisabled e) {
this.gui.error(Error.humanString(Error.Type.SESSION_DISABLED)); this.gui.error(Error.humanString(Error.Type.SESSION_DISABLED));
// should wait forever to actually display the message to the user this.waitForever();
while (shuttingdown == false) {
try {
Thread.sleep(1000);
}
catch (InterruptedException e1) {
}
}
} }
catch (SheepItExceptionSessionDisabledDenoisingNotSupported e) { catch (SheepItExceptionSessionDisabledDenoisingNotSupported e) {
this.gui.error(Error.humanString(Error.Type.DENOISING_NOT_SUPPORTED)); this.gui.error(Error.humanString(Error.Type.DENOISING_NOT_SUPPORTED));
// should wait forever to actually display the message to the user this.waitForever();
while (shuttingdown == false) {
try {
Thread.sleep(1000);
}
catch (InterruptedException e1) {
}
}
} }
catch (SheepItExceptionNoRendererAvailable e) { catch (SheepItExceptionNoRendererAvailable e) {
this.gui.error(Error.humanString(Error.Type.RENDERER_NOT_AVAILABLE)); this.gui.error(Error.humanString(Error.Type.RENDERER_NOT_AVAILABLE));
// should wait forever to actually display the message to the user this.waitForever();
while (shuttingdown == false) {
try {
Thread.sleep(1000);
}
catch (InterruptedException e1) {
}
}
} }
catch (SheepItExceptionNoSession e) { catch (SheepItExceptionNoSession e) {
this.log.debug("User has no session and needs to re-authenticate"); this.log.debug("User has no session and needs to re-authenticate");
@@ -281,19 +251,7 @@ import okhttp3.HttpUrl;
Date now = new Date(); Date now = new Date();
this.gui.status(String.format("Waiting until %tR before requesting job", next_request)); this.gui.status(String.format("Waiting until %tR before requesting job", next_request));
long timeToSleep = next_request.getTimeInMillis() - now.getTime(); long timeToSleep = next_request.getTimeInMillis() - now.getTime();
try { this.activeSleep(timeToSleep);
int timeSlept = 0;
while (timeSlept < timeToSleep && this.running && !this.shuttingdown) {
Thread.sleep(1000);
timeSlept += 1000;
}
}
catch (InterruptedException e3) {
}
catch (IllegalArgumentException e3) {
this.log.error("Client::run sleepB failed " + e3);
}
} }
// if we have broken the wait loop because a stop or shutdown signal, go back to the main loop to exit // if we have broken the wait loop because a stop or shutdown signal, go back to the main loop to exit
@@ -314,14 +272,8 @@ import okhttp3.HttpUrl;
int time_sleep = 1000 * 60 * wait; int time_sleep = 1000 * 60 * wait;
this.gui.status(String.format("Cannot connect to the server. Please check your connectivity. Will try again at %tR", this.gui.status(String.format("Cannot connect to the server. Please check your connectivity. Will try again at %tR",
new Date(new Date().getTime() + time_sleep))); new Date(new Date().getTime() + time_sleep)));
try {
int timeSlept = 0; if (this.activeSleep(time_sleep) == false) {
while (timeSlept < time_sleep && this.running && !this.shuttingdown) {
Thread.sleep(1000);
timeSlept += 1000;
}
}
catch (InterruptedException e1) {
return -3; return -3;
} }
this.log.removeCheckPoint(step); this.log.removeCheckPoint(step);
@@ -332,16 +284,11 @@ import okhttp3.HttpUrl;
int time_sleep = 1000 * 60 * wait; int time_sleep = 1000 * 60 * wait;
this.gui.status(String.format("The server is overloaded and cannot allocate a job. Will try again at %tR", this.gui.status(String.format("The server is overloaded and cannot allocate a job. Will try again at %tR",
new Date(new Date().getTime() + time_sleep))); new Date(new Date().getTime() + time_sleep)));
try {
int timeSlept = 0; if (this.activeSleep(time_sleep) == false) {
while (timeSlept < time_sleep && this.running && !this.shuttingdown) {
Thread.sleep(1000);
timeSlept += 1000;
}
}
catch (InterruptedException e1) {
return -3; return -3;
} }
this.log.removeCheckPoint(step); this.log.removeCheckPoint(step);
continue; // go back to ask job continue; // go back to ask job
} }
@@ -350,14 +297,8 @@ import okhttp3.HttpUrl;
int time_sleep = 1000 * 60 * wait; int time_sleep = 1000 * 60 * wait;
this.gui.status(String.format("The server is under maintenance and cannot allocate a job. Will try again at %tR", this.gui.status(String.format("The server is under maintenance and cannot allocate a job. Will try again at %tR",
new Date(new Date().getTime() + time_sleep))); new Date(new Date().getTime() + time_sleep)));
try {
int timeSlept = 0; if (this.activeSleep(time_sleep) == false) {
while (timeSlept < time_sleep && this.running && !this.shuttingdown) {
Thread.sleep(1000);
timeSlept += 1000;
}
}
catch (InterruptedException e1) {
return -3; return -3;
} }
this.log.removeCheckPoint(step); this.log.removeCheckPoint(step);
@@ -367,16 +308,11 @@ import okhttp3.HttpUrl;
int wait = ThreadLocalRandom.current().nextInt(15, 30 + 1); // max is exclusive int wait = ThreadLocalRandom.current().nextInt(15, 30 + 1); // max is exclusive
int time_sleep = 1000 * 60 * wait; int time_sleep = 1000 * 60 * wait;
this.gui.status(String.format("Bad answer from the server. Will try again at %tR", new Date(new Date().getTime() + time_sleep))); this.gui.status(String.format("Bad answer from the server. Will try again at %tR", new Date(new Date().getTime() + time_sleep)));
try {
int timeSlept = 0; if (this.activeSleep(time_sleep) == false) {
while (timeSlept < time_sleep && this.running && !this.shuttingdown) {
Thread.sleep(1000);
timeSlept += 1000;
}
}
catch (InterruptedException e1) {
return -3; return -3;
} }
this.log.removeCheckPoint(step); this.log.removeCheckPoint(step);
continue; // go back to ask job continue; // go back to ask job
} }
@@ -398,16 +334,9 @@ import okhttp3.HttpUrl;
this.noJobRetryIter++ : this.noJobRetryIter++ :
(retrySchemeInMilliSeconds.length - 1)]; (retrySchemeInMilliSeconds.length - 1)];
this.gui.status(String.format("No job available. Will try again at %tR", new Date(new Date().getTime() + time_sleep))); this.gui.status(String.format("No job available. Will try again at %tR", new Date(new Date().getTime() + time_sleep)));
int time_slept = 0; if (this.activeSleep(time_sleep) == false) {
while (time_slept < time_sleep && this.running && this.shuttingdown == false) {
try {
Thread.sleep(250);
}
catch (InterruptedException e) {
return -3; return -3;
} }
time_slept += 250;
}
this.log.removeCheckPoint(step); this.log.removeCheckPoint(step);
continue; // go back to ask job continue; // go back to ask job
} }
@@ -439,13 +368,7 @@ import okhttp3.HttpUrl;
if (Integer.parseInt(currentJob.getId()) < MIN_JOB_ID) { if (Integer.parseInt(currentJob.getId()) < MIN_JOB_ID) {
// Add the proper explanation to the existing error message and keep the client waiting forever to ensure the user sees the error // Add the proper explanation to the existing error message and keep the client waiting forever to ensure the user sees the error
this.gui.error(Error.humanString(ret) + " The error happened during the test frame render. Restart the client and try again."); this.gui.error(Error.humanString(ret) + " The error happened during the test frame render. Restart the client and try again.");
while (shuttingdown == false) { this.waitForever();
try {
Thread.sleep(1000);
}
catch (InterruptedException e1) {
}
}
break; // if the shutdown signal is triggered then exit the while (this.running) loop to initiate the shutdown process break; // if the shutdown signal is triggered then exit the while (this.running) loop to initiate the shutdown process
} }
@@ -478,11 +401,7 @@ import okhttp3.HttpUrl;
this.gui.status("Sending frames. Please wait"); this.gui.status("Sending frames. Please wait");
while (this.shouldWaitBeforeRender()) { while (this.shouldWaitBeforeRender()) {
try { this.sleep(4000);
Thread.sleep(4000); // wait a little bit
}
catch (InterruptedException e3) {
}
} }
} }
this.log.removeCheckPoint(step); this.log.removeCheckPoint(step);
@@ -492,12 +411,8 @@ import okhttp3.HttpUrl;
// due to user requesting to exit the App and we are just waiting for the upload queue to empty // due to user requesting to exit the App and we are just waiting for the upload queue to empty
// If the user cancels the exit, then this.running will be true and the main loop will take // If the user cancels the exit, then this.running will be true and the main loop will take
// control again // control again
try {
Thread.sleep(2300); // wait a little bit
this.gui.status("Uploading rendered frames before exiting. Please wait"); this.gui.status("Uploading rendered frames before exiting. Please wait");
} this.sleep(2300);
catch (InterruptedException e3) {
}
// This loop will remain valid until all the background uploads have // This loop will remain valid until all the background uploads have
// finished (unless the stop() method has been triggered) // finished (unless the stop() method has been triggered)
@@ -575,6 +490,54 @@ import okhttp3.HttpUrl;
notify(); notify();
} }
public void waitForever() {
// the client is most likely dead.
// instead of exiting, wait forever to display an error message on the UI
while (shuttingdown == false) {
try {
Thread.sleep(1000);
}
catch (InterruptedException ignored) {
}
}
}
/**
* Sleep but stop if the client is not running or shutting down
* @param wait in ms
*/
public boolean activeSleep(long wait) {
try {
long timeSlept = 0;
while (timeSlept < wait && this.running && this.shuttingdown == false) {
Thread.sleep(1000);
timeSlept += 1000;
}
return true;
}
catch (InterruptedException ignored) {
return false;
}
}
/**
* @param wait in ms
*/
public boolean sleep(long wait) {
try {
int timeSlept = 0;
while (timeSlept < wait) {
Thread.sleep(1000);
timeSlept += 1000;
}
return true;
}
catch (InterruptedException ignored) {
return false;
}
}
public void askForStop() { public void askForStop() {
this.log.debug("Client::askForStop"); this.log.debug("Client::askForStop");
this.running = false; this.running = false;