Skip to content

Commit b903c91

Browse files
committed
Fixing Metering bug in ScriptFunctionInvoker
1 parent 512f2d4 commit b903c91

File tree

1 file changed

+38
-31
lines changed

1 file changed

+38
-31
lines changed

src/WebJobs.Script/Description/Script/ScriptFunctionInvoker.cs

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -85,49 +85,56 @@ internal async Task ExecuteScriptAsync(string path, string arguments, object[] i
8585
FunctionStartedEvent startedEvent = new FunctionStartedEvent(functionExecutionContext.InvocationId, Metadata);
8686
_metrics.BeginEvent(startedEvent);
8787

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);
8997

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;
92102

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);
95104

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();
100111

101-
await ProcessInputBindingsAsync(convertedInput, functionInstanceOutputPath, binder, _inputBindings, _outputBindings, bindingData, environmentVariables);
112+
string output = process.StandardOutput.ReadToEnd();
113+
TraceWriter.Info(output);
114+
traceWriter.Info(output);
102115

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;
109117

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+
}
113123

114-
bool failed = process.ExitCode != 0;
115-
startedEvent.Success = !failed;
116-
_metrics.EndEvent(startedEvent);
124+
await ProcessOutputBindingsAsync(functionInstanceOutputPath, _outputBindings, input, binder, bindingData);
117125

118-
if (failed)
126+
TraceWriter.Info(string.Format("Function completed (Success, Id={0})", invocationId));
127+
}
128+
catch
119129
{
120130
startedEvent.Success = false;
121-
122131
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);
126137
}
127-
128-
await ProcessOutputBindingsAsync(functionInstanceOutputPath, _outputBindings, input, binder, bindingData);
129-
130-
TraceWriter.Info(string.Format("Function completed (Success, Id={0})", invocationId));
131138
}
132139

133140
internal static Process CreateProcess(string path, string workingDirectory, string arguments, IDictionary<string, string> environmentVariables = null)

0 commit comments

Comments
 (0)