1- using System . Diagnostics . CodeAnalysis ;
1+ using System . Diagnostics . Contracts ;
22using Microsoft . Extensions . Logging ;
33using Rhino ;
44using Rhino . Runtime . InProcess ;
99
1010namespace Speckle . Importers . Rhino . Internal ;
1111
12- internal sealed class ImporterInstance ( Sender sender , ILogger < ImporterInstance > logger ) : IDisposable
12+ internal sealed class ImporterInstance ( ImporterArgs args , Sender sender , ILogger < ImporterInstance > logger ) : IDisposable
1313{
14- private readonly ILogger _logger = logger ;
1514 private readonly RhinoCore _rhinoInstance = new ( [ "/netcore-8" ] , WindowStyle . NoWindow ) ;
1615
17- private RhinoDoc ? _rhinoDoc ;
16+ private readonly RhinoDoc _rhinoDoc = OpenDocument ( args , logger ) ;
1817
19- public async Task < ImporterResponse > Run ( ImporterArgs args , CancellationToken cancellationToken )
20- {
21- using var scopeJobId = ActivityScope . SetTag ( "jobId" , args . JobId ) ;
22- // using var scopeJobType = ActivityScope.SetTag("jobType", a.JobType);
23- using var scopeAttempt = ActivityScope . SetTag ( "job.attempt" , args . Attempt . ToString ( ) ) ;
24- using var scopeServerUrl = ActivityScope . SetTag ( "serverUrl" , args . Account . serverInfo . url ) ;
25- using var scopeProjectId = ActivityScope . SetTag ( "projectId" , args . Project . id ) ;
26- using var scopeModelId = ActivityScope . SetTag ( "modelId" , args . ModelId ) ;
27- using var scopeBlobId = ActivityScope . SetTag ( "blobId" , args . BlobId ) ;
28- using var scopeFileType = ActivityScope . SetTag ( "fileType" , Path . GetExtension ( args . FilePath ) . TrimStart ( '.' ) ) ;
29- UserActivityScope . AddUserScope ( args . Account ) ;
30-
31- var result = await TryImport ( args , cancellationToken ) ;
32- return result ;
33- }
18+ private readonly IReadOnlyList < IDisposable > _scopes =
19+ [
20+ ActivityScope . SetTag ( "jobId" , args . JobId ) ,
21+ ActivityScope . SetTag ( "job.attempt" , args . Attempt . ToString ( ) ) ,
22+ // ActivityScope.SetTag("jobType", args.JobType),
23+ ActivityScope . SetTag ( "serverUrl" , args . Account . serverInfo . url ) ,
24+ ActivityScope . SetTag ( "projectId" , args . Project . id ) ,
25+ ActivityScope . SetTag ( "modelId" , args . ModelId ) ,
26+ ActivityScope . SetTag ( "blobId" , args . BlobId ) ,
27+ ActivityScope . SetTag ( "fileType" , Path . GetExtension ( args . FilePath ) . TrimStart ( '.' ) ) ,
28+ UserActivityScope . AddUserScope ( args . Account ) ,
29+ ] ;
3430
35- [ SuppressMessage ( "Design" , "CA1031:Do not catch general exception types" , Justification = "IPC" ) ]
36- private async Task < ImporterResponse > TryImport ( ImporterArgs args , CancellationToken cancellationToken )
31+ public async Task < Version > RunRhinoImport ( CancellationToken cancellationToken )
3732 {
3833 try
3934 {
40- var version = await RunRhinoImport ( args , cancellationToken ) ;
41- return new ImporterResponse { Version = version , ErrorMessage = null } ;
42- }
43- catch ( Exception ex )
44- {
45- _logger . LogWarning ( ex , "Import attempt failed with exception" ) ;
46- return new ImporterResponse { ErrorMessage = ex . Message , Version = null } ;
47- }
48- }
49-
50- private async Task < Version > RunRhinoImport ( ImporterArgs args , CancellationToken cancellationToken )
51- {
52- try
53- {
54- using var config = GetConfig ( Path . GetExtension ( args . FilePath ) ) ;
55- logger . LogInformation ( "Opening file {FilePath}" , args . FilePath ) ;
56-
57- _rhinoDoc = config . OpenInHeadlessDocument ( args . FilePath ) ;
5835 RhinoDoc . ActiveDoc = _rhinoDoc ;
5936
60- var version = await sender . Send ( args . Project , args . ModelId , args . Account , cancellationToken ) ;
37+ var version = await sender
38+ . Send ( args . Project , args . ModelId , args . Account , cancellationToken )
39+ . ConfigureAwait ( false ) ;
6140 return version ;
6241 }
6342 finally
@@ -66,6 +45,14 @@ private async Task<Version> RunRhinoImport(ImporterArgs args, CancellationToken
6645 }
6746 }
6847
48+ private static RhinoDoc OpenDocument ( ImporterArgs args , ILogger logger )
49+ {
50+ using var config = GetConfig ( Path . GetExtension ( args . FilePath ) ) ;
51+ logger . LogInformation ( "Opening file {FilePath}" , args . FilePath ) ;
52+ return config . OpenInHeadlessDocument ( args . FilePath ) ;
53+ }
54+
55+ [ Pure ]
6956 private static IFileTypeConfig GetConfig ( string extension ) =>
7057 extension . ToLowerInvariant ( ) switch
7158 {
@@ -83,5 +70,9 @@ public void Dispose()
8370 // https://discourse.mcneel.com/t/rhino-inside-fatal-app-crashes-when-disposing-headless-documents/208673
8471 _rhinoDoc ? . Dispose ( ) ;
8572 _rhinoInstance . Dispose ( ) ;
73+ foreach ( var scope in _scopes )
74+ {
75+ scope . Dispose ( ) ;
76+ }
8677 }
8778}
0 commit comments