From 6ea12fac79e41067981797d4a1c0aaf6f5ceac25 Mon Sep 17 00:00:00 2001 From: Sheepit Renderfarm Date: Thu, 7 Dec 2023 15:18:27 +0000 Subject: [PATCH] Ref: clean up code, do not duplicate code --- src/main/java/com/sheepit/client/Client.java | 175 ++++++++----------- 1 file changed, 69 insertions(+), 106 deletions(-) diff --git a/src/main/java/com/sheepit/client/Client.java b/src/main/java/com/sheepit/client/Client.java index 05955dd..14d90f0 100644 --- a/src/main/java/com/sheepit/client/Client.java +++ b/src/main/java/com/sheepit/client/Client.java @@ -42,7 +42,6 @@ import java.util.TimerTask; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ThreadLocalRandom; -import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import com.sheepit.client.Error.ServerCode; @@ -216,15 +215,7 @@ import okhttp3.HttpUrl; // it means the client has to wait until the next day wait += 24 * 3600 * 1000; } - try { - Thread.sleep(wait); - } - catch (InterruptedException e3) { - - } - catch (IllegalArgumentException e3) { - this.log.error("Client::run sleepA failed " + e3); - } + this.sleep(wait); } this.gui.status("Requesting Job"); this.renderingJob = this.server.requestJob(); @@ -235,36 +226,15 @@ import okhttp3.HttpUrl; } catch (SheepItExceptionSessionDisabled e) { this.gui.error(Error.humanString(Error.Type.SESSION_DISABLED)); - // should wait forever to actually display the message to the user - while (shuttingdown == false) { - try { - Thread.sleep(1000); - } - catch (InterruptedException e1) { - } - } + this.waitForever(); } catch (SheepItExceptionSessionDisabledDenoisingNotSupported e) { this.gui.error(Error.humanString(Error.Type.DENOISING_NOT_SUPPORTED)); - // should wait forever to actually display the message to the user - while (shuttingdown == false) { - try { - Thread.sleep(1000); - } - catch (InterruptedException e1) { - } - } + this.waitForever(); } catch (SheepItExceptionNoRendererAvailable e) { this.gui.error(Error.humanString(Error.Type.RENDERER_NOT_AVAILABLE)); - // should wait forever to actually display the message to the user - while (shuttingdown == false) { - try { - Thread.sleep(1000); - } - catch (InterruptedException e1) { - } - } + this.waitForever(); } catch (SheepItExceptionNoSession e) { this.log.debug("User has no session and needs to re-authenticate"); @@ -281,19 +251,7 @@ import okhttp3.HttpUrl; Date now = new Date(); this.gui.status(String.format("Waiting until %tR before requesting job", next_request)); long timeToSleep = next_request.getTimeInMillis() - now.getTime(); - try { - 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); - } + this.activeSleep(timeToSleep); } // 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; 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))); - try { - int timeSlept = 0; - while (timeSlept < time_sleep && this.running && !this.shuttingdown) { - Thread.sleep(1000); - timeSlept += 1000; - } - } - catch (InterruptedException e1) { + + if (this.activeSleep(time_sleep) == false) { return -3; } this.log.removeCheckPoint(step); @@ -332,16 +284,11 @@ import okhttp3.HttpUrl; 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", new Date(new Date().getTime() + time_sleep))); - try { - int timeSlept = 0; - while (timeSlept < time_sleep && this.running && !this.shuttingdown) { - Thread.sleep(1000); - timeSlept += 1000; - } - } - catch (InterruptedException e1) { + + if (this.activeSleep(time_sleep) == false) { return -3; } + this.log.removeCheckPoint(step); continue; // go back to ask job } @@ -350,14 +297,8 @@ import okhttp3.HttpUrl; 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", new Date(new Date().getTime() + time_sleep))); - try { - int timeSlept = 0; - while (timeSlept < time_sleep && this.running && !this.shuttingdown) { - Thread.sleep(1000); - timeSlept += 1000; - } - } - catch (InterruptedException e1) { + + if (this.activeSleep(time_sleep) == false) { return -3; } this.log.removeCheckPoint(step); @@ -367,16 +308,11 @@ import okhttp3.HttpUrl; int wait = ThreadLocalRandom.current().nextInt(15, 30 + 1); // max is exclusive 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))); - try { - int timeSlept = 0; - while (timeSlept < time_sleep && this.running && !this.shuttingdown) { - Thread.sleep(1000); - timeSlept += 1000; - } - } - catch (InterruptedException e1) { + + if (this.activeSleep(time_sleep) == false) { return -3; } + this.log.removeCheckPoint(step); continue; // go back to ask job } @@ -398,15 +334,8 @@ import okhttp3.HttpUrl; this.noJobRetryIter++ : (retrySchemeInMilliSeconds.length - 1)]; this.gui.status(String.format("No job available. Will try again at %tR", new Date(new Date().getTime() + time_sleep))); - int time_slept = 0; - while (time_slept < time_sleep && this.running && this.shuttingdown == false) { - try { - Thread.sleep(250); - } - catch (InterruptedException e) { - return -3; - } - time_slept += 250; + if (this.activeSleep(time_sleep) == false) { + return -3; } this.log.removeCheckPoint(step); continue; // go back to ask job @@ -439,13 +368,7 @@ import okhttp3.HttpUrl; 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 this.gui.error(Error.humanString(ret) + " The error happened during the test frame render. Restart the client and try again."); - while (shuttingdown == false) { - try { - Thread.sleep(1000); - } - catch (InterruptedException e1) { - } - } + this.waitForever(); 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"); while (this.shouldWaitBeforeRender()) { - try { - Thread.sleep(4000); // wait a little bit - } - catch (InterruptedException e3) { - } + this.sleep(4000); } } 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 // If the user cancels the exit, then this.running will be true and the main loop will take // control again - try { - Thread.sleep(2300); // wait a little bit - this.gui.status("Uploading rendered frames before exiting. Please wait"); - } - catch (InterruptedException e3) { - } + this.gui.status("Uploading rendered frames before exiting. Please wait"); + this.sleep(2300); // This loop will remain valid until all the background uploads have // finished (unless the stop() method has been triggered) @@ -575,6 +490,54 @@ import okhttp3.HttpUrl; 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() { this.log.debug("Client::askForStop"); this.running = false;