From e0e84df720d484cd741fe88fa0bf66841cf0a284 Mon Sep 17 00:00:00 2001
From: Andreas Svanberg <andreass@dsv.su.se>
Date: Tue, 14 Jan 2025 14:50:31 +0100
Subject: [PATCH] Reset the failure flag in workers on successful runs. (#76)

Reviewed-on: https://gitea.dsv.su.se/DMC/scipro/pulls/76
Reviewed-by: Tom Zhao <tom.zhao@dsv.su.se>
Co-authored-by: Andreas Svanberg <andreass@dsv.su.se>
Co-committed-by: Andreas Svanberg <andreass@dsv.su.se>
---
 .../scipro/workerthreads/AbstractWorker.java  | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/core/src/main/java/se/su/dsv/scipro/workerthreads/AbstractWorker.java b/core/src/main/java/se/su/dsv/scipro/workerthreads/AbstractWorker.java
index 9c97efdb03..940668b430 100755
--- a/core/src/main/java/se/su/dsv/scipro/workerthreads/AbstractWorker.java
+++ b/core/src/main/java/se/su/dsv/scipro/workerthreads/AbstractWorker.java
@@ -1,9 +1,6 @@
 package se.su.dsv.scipro.workerthreads;
 
 import jakarta.inject.Inject;
-import jakarta.inject.Provider;
-import jakarta.persistence.EntityManager;
-import jakarta.persistence.EntityTransaction;
 import java.util.Date;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -58,6 +55,22 @@ public abstract class AbstractWorker implements Worker {
              * Do manually transaction-handled work
              */
             try {
+                // When the switch from Guice to Spring happened all workers became singletons
+                // because that's the default in Spring. In Guice they were "prototype" scoped
+                // and therefore the worker object was re-created before each execution which
+                // reset the successfulWorker field to true.
+                //
+                // Now that they're singletons the field is never reset to true after a
+                // failure and the worker will be stuck in a failed state even after a
+                // subsequent successful run.
+                //
+                // TODO:
+                //  In the future this flag should be removed and any execution that does
+                //  not throw an exception should be considered successful.
+                //  If a worker needs to signal a non-exception as a failure that should
+                //  be an internal matter and not something the scheduler should consider.
+                setSuccessfulWorker(true);
+
                 doWork();
             } catch (RuntimeException ex) {
                 LOGGER.info("Worker {} threw an exception", getClass().getSimpleName());