@@ -85,49 +85,56 @@ internal async Task ExecuteScriptAsync(string path, string arguments, object[] i
85
85
FunctionStartedEvent startedEvent = new FunctionStartedEvent ( functionExecutionContext . InvocationId , Metadata ) ;
86
86
_metrics . BeginEvent ( startedEvent ) ;
87
87
88
- TraceWriter . Info ( string . Format ( "Function started (Id={0})" , invocationId ) ) ;
88
+ try
89
+ {
90
+ TraceWriter . Info ( string . Format ( "Function started (Id={0})" , invocationId ) ) ;
91
+
92
+ string workingDirectory = Path . GetDirectoryName ( _scriptFilePath ) ;
93
+ string functionInstanceOutputPath = Path . Combine ( Path . GetTempPath ( ) , "Functions" , "Binding" , invocationId ) ;
94
+
95
+ Dictionary < string , string > environmentVariables = new Dictionary < string , string > ( ) ;
96
+ InitializeEnvironmentVariables ( environmentVariables , functionInstanceOutputPath , input , _outputBindings , functionExecutionContext ) ;
89
97
90
- string workingDirectory = Path . GetDirectoryName ( _scriptFilePath ) ;
91
- string functionInstanceOutputPath = Path . Combine ( Path . GetTempPath ( ) , "Functions" , "Binding" , invocationId ) ;
98
+ object convertedInput = ConvertInput ( input ) ;
99
+ ApplyBindingData ( convertedInput , binder ) ;
100
+ Dictionary < string , object > bindingData = binder . BindingData ;
101
+ bindingData [ "InvocationId" ] = invocationId ;
92
102
93
- Dictionary < string , string > environmentVariables = new Dictionary < string , string > ( ) ;
94
- InitializeEnvironmentVariables ( environmentVariables , functionInstanceOutputPath , input , _outputBindings , functionExecutionContext ) ;
103
+ await ProcessInputBindingsAsync ( convertedInput , functionInstanceOutputPath , binder , _inputBindings , _outputBindings , bindingData , environmentVariables ) ;
95
104
96
- object convertedInput = ConvertInput ( input ) ;
97
- ApplyBindingData ( convertedInput , binder ) ;
98
- Dictionary < string , object > bindingData = binder . BindingData ;
99
- bindingData [ "InvocationId" ] = invocationId ;
105
+ // TODO
106
+ // - put a timeout on how long we wait?
107
+ // - need to periodically flush the standard out to the TraceWriter
108
+ Process process = CreateProcess ( path , workingDirectory , arguments , environmentVariables ) ;
109
+ process . Start ( ) ;
110
+ process . WaitForExit ( ) ;
100
111
101
- await ProcessInputBindingsAsync ( convertedInput , functionInstanceOutputPath , binder , _inputBindings , _outputBindings , bindingData , environmentVariables ) ;
112
+ string output = process . StandardOutput . ReadToEnd ( ) ;
113
+ TraceWriter . Info ( output ) ;
114
+ traceWriter . Info ( output ) ;
102
115
103
- // TODO
104
- // - put a timeout on how long we wait?
105
- // - need to periodically flush the standard out to the TraceWriter
106
- Process process = CreateProcess ( path , workingDirectory , arguments , environmentVariables ) ;
107
- process . Start ( ) ;
108
- process . WaitForExit ( ) ;
116
+ startedEvent . Success = process . ExitCode == 0 ;
109
117
110
- string output = process . StandardOutput . ReadToEnd ( ) ;
111
- TraceWriter . Info ( output ) ;
112
- traceWriter . Info ( output ) ;
118
+ if ( ! startedEvent . Success )
119
+ {
120
+ string error = process . StandardError . ReadToEnd ( ) ;
121
+ throw new ApplicationException ( error ) ;
122
+ }
113
123
114
- bool failed = process . ExitCode != 0 ;
115
- startedEvent . Success = ! failed ;
116
- _metrics . EndEvent ( startedEvent ) ;
124
+ await ProcessOutputBindingsAsync ( functionInstanceOutputPath , _outputBindings , input , binder , bindingData ) ;
117
125
118
- if ( failed )
126
+ TraceWriter . Info ( string . Format ( "Function completed (Success, Id={0})" , invocationId ) ) ;
127
+ }
128
+ catch
119
129
{
120
130
startedEvent . Success = false ;
121
-
122
131
TraceWriter . Error ( string . Format ( "Function completed (Failure, Id={0})" , invocationId ) ) ;
123
-
124
- string error = process . StandardError . ReadToEnd ( ) ;
125
- throw new ApplicationException ( error ) ;
132
+ throw ;
133
+ }
134
+ finally
135
+ {
136
+ _metrics . EndEvent ( startedEvent ) ;
126
137
}
127
-
128
- await ProcessOutputBindingsAsync ( functionInstanceOutputPath , _outputBindings , input , binder , bindingData ) ;
129
-
130
- TraceWriter . Info ( string . Format ( "Function completed (Success, Id={0})" , invocationId ) ) ;
131
138
}
132
139
133
140
internal static Process CreateProcess ( string path , string workingDirectory , string arguments , IDictionary < string , string > environmentVariables = null )
0 commit comments