@@ -80,7 +80,7 @@ class Threader {
8080 * @param name The name of the thread.
8181 * @return The macro expression to run the thread.
8282 */
83- public static macro function runInThread (expr : Expr , ? sleepDuration : Float = 0 , ? name : String = " " ): Expr {
83+ public static macro function runInThread (expr : Expr , ? sleepDuration : Float = 0 , ? name : String = " " , ? retryOnError : Bool = false , ? maxRetries : Int = 3 ): Expr {
8484 if (! usedthreads ) {
8585 trace (" Initializing Threader..." );
8686 Context .onAfterGenerate (function () {
@@ -99,30 +99,63 @@ class Threader {
9999 usedthreads = ! usedthreads ? true : usedthreads ;
100100 var sleepExpr = Context .makeExpr (sleepDuration , Context .currentPos ());
101101 var nameExpr = Context .makeExpr (name != " " && name != null ? name : " Thread_" + Std .random (1000000 ) + " _" + (stringRandomizer (8 )), Context .currentPos ());
102+ var retryExpr = Context .makeExpr (retryOnError , Context .currentPos ());
103+ var maxRetriesExpr = Context .makeExpr (maxRetries , Context .currentPos ());
102104 var generatedName : String = ExprTools .toString (nameExpr );
103105 generatedThreads .push (generatedName );
104106 trace (" Preparing a threaded section of code:\n " + expr + " \n with sleep duration: " + sleepDuration + " and name: " + generatedName );
105107 var threadExpr = macro {
106108 #if sys
107109 yutautil. Threader .quietThreads .push ($nameExpr );
108110 var thrd = sys.thread. Thread .create (function () {
109- try {
110- trace (" Set command to run in a thread..." );
111- if ($nameExpr != " " ) {
112- trace (" Thread name: " + $nameExpr );
113- }
114- $expr ;
115- if ($sleepExpr > 0 ) {
116- Sys .sleep ($sleepExpr );
117- }
118- trace (" Thread finished running command: " + $nameExpr );
119- yutautil. Threader .quietThreads .remove ($nameExpr );
120- } catch (e : Dynamic ) {
121- trace (" Exception in thread: " + e + " ... " + haxe. CallStack .toString (haxe. CallStack .exceptionStack ()));
122- if ($nameExpr != " " ) {
123- trace (" Errored Thread name: " + $nameExpr );
111+ var retryCount = 0 ;
112+ var shouldRetry = true ;
113+
114+ while (shouldRetry ) {
115+ try {
116+ trace (" Set command to run in a thread..." );
117+ if ($nameExpr != " " ) {
118+ trace (" Thread name: " + $nameExpr + (retryCount > 0 ? " (retry " + retryCount + " )" : " " ));
119+ }
120+ $expr ;
121+ if ($sleepExpr > 0 ) {
122+ Sys .sleep ($sleepExpr );
123+ }
124+ trace (" Thread finished running command: " + $nameExpr );
125+ yutautil. Threader .quietThreads .remove ($nameExpr );
126+ shouldRetry = false ; // Success, exit retry loop
127+ } catch (e : Dynamic ) {
128+ trace (" Exception in thread: " + e + " ... " + haxe. CallStack .toString (haxe. CallStack .exceptionStack ()));
129+ if ($nameExpr != " " ) {
130+ trace (" Errored Thread name: " + $nameExpr + " (attempt " + (retryCount + 1 ) + " )" );
131+ }
132+
133+ if ($retryExpr ) {
134+ retryCount ++ ;
135+ // If maxRetries is 0, allow infinite retries
136+ if ($maxRetriesExpr == 0 ) {
137+ trace (" Retrying thread " + $nameExpr + " (infinite retries enabled, attempt " + retryCount + " )" );
138+ continue ;
139+ }
140+ // If we haven't exceeded max retries, try again
141+ else if (retryCount < $maxRetriesExpr ) {
142+ trace (" Retrying thread " + $nameExpr + " (attempt " + (retryCount + 1 ) + " of " + $maxRetriesExpr + " )" );
143+ continue ;
144+ }
145+ // Max retries exceeded
146+ else {
147+ trace (" Max retries (" + $maxRetriesExpr + " ) exceeded for thread " + $nameExpr + " . Stopping thread." );
148+ shouldRetry = false ;
149+ }
150+ } else {
151+ // No retry enabled, exit immediately
152+ shouldRetry = false ;
153+ }
154+
155+ if (! shouldRetry ) {
156+ yutautil. Threader .quietThreads .remove ($nameExpr );
157+ }
124158 }
125- yutautil. Threader .quietThreads .remove ($nameExpr );
126159 }
127160 });
128161 #else
0 commit comments