@@ -75,7 +75,7 @@ public static WorkerLogSnapshot SnapshotWorkerLogs(string assetPath)
7575 return snapshot ;
7676 }
7777
78- var logFiles = Directory . GetFiles ( logsDirectory , "AssetImportWorker *.log" )
78+ var logFiles = Directory . GetFiles ( logsDirectory , "AssetImportWorkerHW *.log" )
7979 . Where ( f => ! Path . GetFileName ( f ) . Contains ( "-prev" ) ) // Ignore previous run logs
8080 . ToArray ( ) ;
8181
@@ -103,7 +103,7 @@ public static WorkerLogSnapshot SnapshotWorkerLogs(string assetPath)
103103 private static int ExtractWorkerIdFromFilename ( string logFilePath )
104104 {
105105 var filename = Path . GetFileName ( logFilePath ) ;
106- var match = Regex . Match ( filename , @"AssetImportWorker (\d+)\.log" ) ;
106+ var match = Regex . Match ( filename , @"AssetImportWorkerHW (\d+)\.log" ) ;
107107 return match . Success ? int . Parse ( match . Groups [ 1 ] . Value ) : - 1 ;
108108 }
109109
@@ -124,45 +124,12 @@ private static int CountAssetImportsInLog(string logFilePath, string assetPath)
124124 return 0 ;
125125
126126 int count = 0 ;
127- try
127+ const int maxRetries = 2 ;
128+ for ( int attempt = 0 ; attempt < maxRetries ; attempt ++ )
128129 {
129- // Use FileStream with FileShare.ReadWrite to allow reading while the worker has the file open for writing
130- using ( var fileStream = new FileStream ( logFilePath , FileMode . Open , FileAccess . Read , FileShare . ReadWrite ) )
131- using ( var reader = new StreamReader ( fileStream ) )
132- {
133- var lines = new List < string > ( ) ;
134- string line ;
135- while ( ( line = reader . ReadLine ( ) ) != null )
136- {
137- lines . Add ( line ) ;
138- }
139-
140- for ( int i = 0 ; i < lines . Count ; i ++ )
141- {
142- // Look for the "Received Import Request." line
143- if ( lines [ i ] . Contains ( "Received Import Request." ) )
144- {
145- // Check the next few lines for the path
146- for ( int j = i + 1 ; j < Math . Min ( i + 5 , lines . Count ) ; j ++ )
147- {
148- if ( lines [ j ] . Contains ( $ "path: { assetPath } ") )
149- {
150- count ++ ;
151- break ; // Found the path, move to next import request
152- }
153- }
154- }
155- }
156- }
157- }
158- catch ( IOException ex ) when ( ex . Message . Contains ( "sharing violation" ) || ex . Message . Contains ( "being used by another process" ) )
159- {
160- Debug . LogWarning ( $ "Worker log { logFilePath } is currently in use by another process. Retrying...") ;
161-
162- // Retry after a short delay - the worker might have just finished writing
163- System . Threading . Thread . Sleep ( 100 ) ;
164130 try
165131 {
132+ // Use FileStream with FileShare.ReadWrite to allow reading while the worker has the file open for writing
166133 using ( var fileStream = new FileStream ( logFilePath , FileMode . Open , FileAccess . Read , FileShare . ReadWrite ) )
167134 using ( var reader = new StreamReader ( fileStream ) )
168135 {
@@ -175,28 +142,34 @@ private static int CountAssetImportsInLog(string logFilePath, string assetPath)
175142
176143 for ( int i = 0 ; i < lines . Count ; i ++ )
177144 {
145+ // Look for the "Received Import Request." line
178146 if ( lines [ i ] . Contains ( "Received Import Request." ) )
179147 {
148+ // Check the next few lines for the path
180149 for ( int j = i + 1 ; j < Math . Min ( i + 5 , lines . Count ) ; j ++ )
181150 {
182- if ( lines [ j ] . Contains ( $ "path : { assetPath } ") )
151+ if ( lines [ j ] . Contains ( $ "Path : { assetPath } ") )
183152 {
184153 count ++ ;
185- break ;
154+ break ; // Found the path, move to next import request
186155 }
187156 }
188157 }
189158 }
190159 }
160+ break ; // Success, exit retry loop
191161 }
192- catch ( Exception retryEx )
162+ catch ( IOException ex ) when ( ( ex . Message . Contains ( "sharing violation" ) || ex . Message . Contains ( "being used by another process" ) ) && attempt < maxRetries - 1 )
193163 {
194- Debug . LogWarning ( $ "Failed to read worker log { logFilePath } after retry: { retryEx . Message } ") ;
164+ Debug . LogWarning ( $ "Worker log { logFilePath } is currently in use by another process. Retrying...") ;
165+ // Retry after a short delay - the worker might have just finished writing
166+ System . Threading . Thread . Sleep ( 100 ) ;
167+ }
168+ catch ( Exception ex )
169+ {
170+ Debug . LogError ( $ "Failed to read worker log { logFilePath } { ( attempt > 0 ? " after retry" : "" ) } : { ex . Message } ") ;
171+ break ; // Non-retryable exception, exit loop
195172 }
196- }
197- catch ( Exception ex )
198- {
199- Debug . LogWarning ( $ "Failed to read worker log { logFilePath } : { ex . Message } ") ;
200173 }
201174
202175 return count ;
@@ -223,28 +196,5 @@ public static void AssertSingleWorkerUsedAtLeastTwice(WorkerLogSnapshot importDi
223196 Assert . AreEqual ( expectedImports , sumImports ,
224197 $ "Expected a total of { expectedImports } imports for { assetPath } , but found { sumImports } imports across all workers") ;
225198 }
226-
227- /// <summary>
228- /// Asserts that all imports in the difference snapshot occurred on a single worker
229- /// without checking the exact count (useful when the expected count is unknown).
230- /// </summary>
231- /// <param name="importDifference">The difference snapshot showing new imports</param>
232- /// <param name="assetPath">The asset path being imported</param>
233- /// <returns>The worker ID that performed all imports</returns>
234- public static int AssertSingleWorkerUsed ( WorkerLogSnapshot importDifference , string assetPath )
235- {
236- var workersWithImports = importDifference . WorkerImportCounts . Where ( kvp => kvp . Value > 0 ) . ToList ( ) ;
237-
238- Assert . IsTrue ( workersWithImports . Count > 0 ,
239- $ "Expected at least one worker to have processed imports for { assetPath } , but found none") ;
240-
241- Assert . AreEqual ( 1 , workersWithImports . Count ,
242- $ "Expected all imports for { assetPath } to occur on a single worker, but found imports on { workersWithImports . Count } workers: { string . Join ( ", " , workersWithImports . Select ( kvp => $ "Worker{ kvp . Key } ({ kvp . Value } imports)") ) } ") ;
243-
244- var singleWorker = workersWithImports . First ( ) ;
245- Debug . Log ( $ "✓ All { singleWorker . Value } imports for { assetPath } occurred on Worker{ singleWorker . Key } as expected") ;
246-
247- return singleWorker . Key ;
248- }
249199 }
250200}
0 commit comments