فهرست منبع

logic to handle same error consistently occurring towards a specific endpoint

randoman 6 سال پیش
والد
کامیت
d111aa30ab

+ 43 - 22
src/XUnity.AutoTranslator.Plugin.Core/AutoTranslationPlugin.cs

@@ -2124,28 +2124,37 @@ namespace XUnity.AutoTranslator.Plugin.Core
 
                   if( !job.AnyComponentsStillHasOriginalUntranslatedTextOrContextual() ) continue;
 
-                  jobs.Add( job );
-                  untranslatedTexts.Add( job.Key.GetDictionaryLookupKey() );
-
-                  _ongoingJobs[ key ] = job;
+                  var untranslatedText = job.Key.GetDictionaryLookupKey();
+                  if( _endpoint.CanTranslate( untranslatedText ) )
+                  {
+                     jobs.Add( job );
+                     untranslatedTexts.Add( untranslatedText );
+                     _ongoingJobs[ key ] = job;
+                  }
+                  else
+                  {
+                     XuaLogger.Current.Warn( $"Unqueued: '{untranslatedText}' because the current endpoint has already failed this translation 3 times." );
+                     job.State = TranslationJobState.Failed;
+                  }
                }
 
                if( jobs.Count > 0 )
                {
                   _availableBatchOperations--;
                   var jobsArray = jobs.ToArray();
+                  var endpoint = _endpoint;
 
                   foreach( var untranslatedText in untranslatedTexts )
                   {
                      XuaLogger.Current.Debug( "Started: '" + untranslatedText + "'" );
                   }
                   StartCoroutine(
-                     _endpoint.Translate(
+                     endpoint.Translate(
                         untranslatedTexts.ToArray(),
                         Settings.FromLanguage,
                         Settings.Language,
                         translatedText => OnBatchTranslationCompleted( jobsArray, translatedText ),
-                        ( msg, e ) => OnTranslationFailed( jobsArray, msg, e ) ) );
+                        ( msg, e ) => OnTranslationFailed( endpoint, jobsArray, msg, e ) ) );
                }
             }
          }
@@ -2164,17 +2173,26 @@ namespace XUnity.AutoTranslator.Plugin.Core
                // lets see if the text should still be translated before kicking anything off
                if( !job.AnyComponentsStillHasOriginalUntranslatedTextOrContextual() ) continue;
 
-               _ongoingJobs[ key ] = job;
-
                var untranslatedText = job.Key.GetDictionaryLookupKey();
-               XuaLogger.Current.Debug( "Started: " + untranslatedText );
-               StartCoroutine(
-                  _endpoint.Translate(
-                     new[] { untranslatedText },
-                     Settings.FromLanguage,
-                     Settings.Language,
-                     translatedText => OnSingleTranslationCompleted( job, translatedText ),
-                     ( msg, e ) => OnTranslationFailed( new[] { job }, msg, e ) ) );
+               if( _endpoint.CanTranslate( untranslatedText ) )
+               {
+                  var endpoint = _endpoint;
+                  _ongoingJobs[ key ] = job;
+
+                  XuaLogger.Current.Debug( "Started: " + untranslatedText );
+                  StartCoroutine(
+                     endpoint.Translate(
+                        new[] { untranslatedText },
+                        Settings.FromLanguage,
+                        Settings.Language,
+                        translatedText => OnSingleTranslationCompleted( job, translatedText ),
+                        ( msg, e ) => OnTranslationFailed( endpoint, new[] { job }, msg, e ) ) );
+               }
+               else
+               {
+                  XuaLogger.Current.Warn( $"Unqueued: '{untranslatedText}' because the current endpoint has already failed this translation 3 times." );
+                  job.State = TranslationJobState.Failed;
+               }
             }
          }
       }
@@ -2208,7 +2226,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
                job.State = TranslationJobState.Succeeded;
                _ongoingJobs.Remove( job.Key.GetDictionaryLookupKey() );
 
-               XuaLogger.Current.Debug( $"Completed: '{job.Key.GetDictionaryLookupKey()}' => '{translatedText}'" );
+               XuaLogger.Current.Info( $"Completed: '{job.Key.GetDictionaryLookupKey()}' => '{translatedText}'" );
             }
          }
          else
@@ -2250,12 +2268,12 @@ namespace XUnity.AutoTranslator.Plugin.Core
          }
       }
 
-      private void OnSingleTranslationCompleted( TranslationJob job, string[] translatedTextArray )
+      private void OnSingleTranslationCompleted( TranslationJob job, string[] translatedTexts )
       {
-         var translatedText = translatedTextArray[ 0 ];
+         var translatedText = translatedTexts[ 0 ];
 
          Settings.TranslationCount++;
-         XuaLogger.Current.Debug( $"Completed: '{job.Key.GetDictionaryLookupKey()}' => '{translatedText}'" );
+         XuaLogger.Current.Info( $"Completed: '{job.Key.GetDictionaryLookupKey()}' => '{translatedText}'" );
 
          _consecutiveErrors = 0;
 
@@ -2290,7 +2308,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
          }
       }
 
-      private void OnTranslationFailed( TranslationJob[] jobs, string error, Exception e )
+      private void OnTranslationFailed( ConfiguredEndpoint endpoint, TranslationJob[] jobs, string error, Exception e )
       {
          if( e == null )
          {
@@ -2308,8 +2326,11 @@ namespace XUnity.AutoTranslator.Plugin.Core
             Settings.TranslationCount++; // counts as a translation
             foreach( var job in jobs )
             {
+               var untranslatedText = job.Key.GetDictionaryLookupKey();
                job.State = TranslationJobState.Failed;
-               _ongoingJobs.Remove( job.Key.GetDictionaryLookupKey() );
+               _ongoingJobs.Remove( untranslatedText );
+
+               endpoint.RegisterTranslationFailureFor( untranslatedText );
             }
          }
          else

+ 1 - 0
src/XUnity.AutoTranslator.Plugin.Core/Configuration/Settings.cs

@@ -14,6 +14,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Configuration
    internal static class Settings
    {
       // cannot be changed
+      public static readonly int MaxFailuresForSameTextPerEndpoint = 3;
       public static readonly string PluginFolder = "Translators";
       public static readonly int MaxMaxCharactersPerTranslation = 500;
       public static readonly string DefaultLanguage = "en";

+ 28 - 0
src/XUnity.AutoTranslator.Plugin.Core/Endpoints/ConfiguredEndpoint.cs

@@ -1,17 +1,21 @@
 using System;
 using System.Collections;
+using System.Collections.Generic;
+using XUnity.AutoTranslator.Plugin.Core.Configuration;
 
 namespace XUnity.AutoTranslator.Plugin.Core.Endpoints
 {
    internal class ConfiguredEndpoint
    {
       private int _ongoingTranslations;
+      private Dictionary<string, byte> _failedTranslations;
 
       public ConfiguredEndpoint( ITranslateEndpoint endpoint, Exception error )
       {
          Endpoint = endpoint;
          Error = error;
          _ongoingTranslations = 0;
+         _failedTranslations = new Dictionary<string, byte>();
       }
 
       public ITranslateEndpoint Endpoint { get; }
@@ -20,6 +24,30 @@ namespace XUnity.AutoTranslator.Plugin.Core.Endpoints
 
       public bool IsBusy => _ongoingTranslations >= Endpoint.MaxConcurrency;
 
+      public bool CanTranslate( string untranslatedText )
+      {
+         if( _failedTranslations.TryGetValue( untranslatedText, out var count ) )
+         {
+            return count < Settings.MaxFailuresForSameTextPerEndpoint;
+         }
+         return true;
+      }
+
+      public void RegisterTranslationFailureFor( string untranslatedText )
+      {
+         byte count;
+         if( !_failedTranslations.TryGetValue( untranslatedText, out count ) )
+         {
+            count = 1;
+         }
+         else
+         {
+            count++;
+         }
+
+         _failedTranslations[ untranslatedText ] = count;
+      }
+
       public IEnumerator Translate( string[] untranslatedTexts, string from, string to, Action<string[]> success, Action<string, Exception> failure )
       {
          var context = new TranslationContext( untranslatedTexts, from, to, success, failure );