@@ -25,93 +25,66 @@ internal class PowerShellServices : IPowerShellServices
25
25
private readonly ILogger _logger ;
26
26
27
27
private const string _setFunctionInvocationContextCommandTemplate = "{0}\\ Set-FunctionInvocationContext" ;
28
- private const string _internalDurableSdkName = "Microsoft.Azure.Functions.PowerShellWorker" ;
29
- private const string _externalDurableSdkName = "AzureFunctions.PowerShell.Durable.SDK" ;
30
28
31
29
// uses built-in SDK by default
32
30
private string SetFunctionInvocationContextCommand = string . Format (
33
31
_setFunctionInvocationContextCommandTemplate ,
34
- _internalDurableSdkName ) ;
32
+ Utils . InternalDurableSdkName ) ;
35
33
36
34
public PowerShellServices ( PowerShell pwsh , ILogger logger )
37
35
{
38
36
_pwsh = pwsh ;
39
37
_logger = logger ;
40
38
41
39
// Configure FunctionInvocationContext command, based on the select DF SDK
42
- var prefix = _internalDurableSdkName ;
40
+ var prefix = Utils . InternalDurableSdkName ;
43
41
SetFunctionInvocationContextCommand = string . Format ( _setFunctionInvocationContextCommandTemplate , prefix ) ;
44
42
}
45
43
46
- private bool tryImportingDurableSDK ( )
44
+ public bool isExternalDurableSdkLoaded ( )
47
45
{
48
- // Try to load/import the external Durable Functions SDK. If an error occurs, it is logged.
49
- var importSucceeded = false ;
50
- try
51
- {
52
- // attempt to import SDK
53
- _logger . Log ( isUserOnlyLog : false , LogLevel . Trace , String . Format (
54
- PowerShellWorkerStrings . LoadingDurableSDK , _externalDurableSdkName ) ) ;
55
-
56
- var results = _pwsh . AddCommand ( Utils . ImportModuleCmdletInfo )
57
- . AddParameter ( "FullyQualifiedName" , _externalDurableSdkName )
58
- . AddParameter ( "ErrorAction" , ActionPreference . Stop )
59
- . AddParameter ( "PassThru" )
60
- . InvokeAndClearCommands < PSModuleInfo > ( ) ;
61
-
62
- // Given how the command above is constructed, only 1 result should be possible
63
- var moduleInfo = results [ 0 ] ;
64
- _logger . Log ( isUserOnlyLog : false , LogLevel . Trace , String . Format (
65
- PowerShellWorkerStrings . ImportSucceeded , moduleInfo . Name , moduleInfo . Version ) ) ;
66
-
67
- importSucceeded = true ;
68
- }
69
- catch ( Exception e )
70
- {
71
- // If an error ocurred, we try to log the exception.
72
- var errorMessage = e . ToString ( ) ;
46
+ // Search for the external DF SDK in the current session
47
+ var matchingModules = _pwsh . AddCommand ( Utils . GetModuleCmdletInfo )
48
+ . AddParameter ( "FullyQualifiedName" , Utils . ExternalDurableSdkName )
49
+ . InvokeAndClearCommands < PSModuleInfo > ( ) ;
73
50
74
- // If a PowerShell error record is available through Get-Error, we log that instead.
75
- if ( e . InnerException is IContainsErrorRecord inner )
51
+ // If we get at least one result, we know the external SDK was imported
52
+ var numCandidates = matchingModules . Count ( ) ;
53
+ var isModuleInCurrentSession = numCandidates > 0 ;
54
+
55
+ if ( isModuleInCurrentSession )
56
+ {
57
+ var candidatesInfo = matchingModules . Select ( module => string . Format (
58
+ PowerShellWorkerStrings . FoundExternalDurableSdkInSession , module . Name , module . Version , module . Path ) ) ;
59
+ var externalSDKModuleInfo = string . Join ( '\n ' , candidatesInfo ) ;
60
+
61
+ if ( numCandidates > 1 )
62
+ {
63
+ // If there's more than 1 result, there may be runtime conflicts
64
+ // warn user of potential conflicts
65
+ _logger . Log ( isUserOnlyLog : false , LogLevel . Warning , String . Format (
66
+ PowerShellWorkerStrings . MultipleExternalSDKsInSession ,
67
+ numCandidates , Utils . ExternalDurableSdkName , externalSDKModuleInfo ) ) ;
68
+ }
69
+ else
76
70
{
77
- errorMessage = _errorRecordFormatter . Format ( inner . ErrorRecord ) ;
71
+ // a single external SDK is in session. Report its metadata
72
+ _logger . Log ( isUserOnlyLog : false , LogLevel . Trace , externalSDKModuleInfo ) ;
78
73
}
79
- _logger . Log ( isUserOnlyLog : false , LogLevel . Error , string . Format (
80
- PowerShellWorkerStrings . ErrorImportingDurableSDK ,
81
- _externalDurableSdkName , errorMessage ) ) ;
82
74
83
75
}
84
- return importSucceeded ;
76
+
77
+ return isModuleInCurrentSession ;
85
78
}
86
79
87
- public void tryEnablingExternalDurableSDK ( )
80
+ public void EnableExternalDurableSDK ( )
88
81
{
89
- // Search for the external DF SDK in the available modules
90
- var matchingModules = _pwsh . AddCommand ( Utils . GetModuleCmdletInfo )
91
- . AddParameter ( "ListAvailable" )
92
- . AddParameter ( "FullyQualifiedName" , _externalDurableSdkName )
93
- . InvokeAndClearCommands < PSModuleInfo > ( ) ;
94
-
95
- // If we get at least one result, we attempt to load it
96
- var numCandidates = matchingModules . Count ( ) ;
97
- if ( numCandidates > 0 )
98
- {
99
- // try to import the external DF SDK
100
- _usesExternalDurableSDK = tryImportingDurableSDK ( ) ;
101
- }
102
- else
103
- {
104
- // Log that the module was not found in worker path
105
- var workerPathContents = PowerShellWorkerConfiguration . GetString ( "PSModulePath" ) ;
106
- _logger . Log ( isUserOnlyLog : false , LogLevel . Trace , string . Format (
107
- PowerShellWorkerStrings . DurableNotInWorkerPath , _externalDurableSdkName ,
108
- workerPathContents ) ) ;
109
- }
82
+ _usesExternalDurableSDK = true ;
110
83
111
84
// assign SetFunctionInvocationContextCommand to the corresponding external SDK's CmdLet
112
85
SetFunctionInvocationContextCommand = string . Format (
113
86
_setFunctionInvocationContextCommandTemplate ,
114
- _externalDurableSdkName ) ;
87
+ Utils . ExternalDurableSdkName ) ;
115
88
}
116
89
117
90
public bool HasExternalDurableSDK ( )
0 commit comments