Skip to content

Commit

Permalink
Fixing Metering bug in ScriptFunctionInvoker
Browse files Browse the repository at this point in the history
  • Loading branch information
mathewc committed Aug 8, 2016
1 parent 512f2d4 commit b903c91
Showing 1 changed file with 38 additions and 31 deletions.
69 changes: 38 additions & 31 deletions src/WebJobs.Script/Description/Script/ScriptFunctionInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,49 +85,56 @@ internal async Task ExecuteScriptAsync(string path, string arguments, object[] i
FunctionStartedEvent startedEvent = new FunctionStartedEvent(functionExecutionContext.InvocationId, Metadata);
_metrics.BeginEvent(startedEvent);

TraceWriter.Info(string.Format("Function started (Id={0})", invocationId));
try
{
TraceWriter.Info(string.Format("Function started (Id={0})", invocationId));

string workingDirectory = Path.GetDirectoryName(_scriptFilePath);
string functionInstanceOutputPath = Path.Combine(Path.GetTempPath(), "Functions", "Binding", invocationId);

Dictionary<string, string> environmentVariables = new Dictionary<string, string>();
InitializeEnvironmentVariables(environmentVariables, functionInstanceOutputPath, input, _outputBindings, functionExecutionContext);

string workingDirectory = Path.GetDirectoryName(_scriptFilePath);
string functionInstanceOutputPath = Path.Combine(Path.GetTempPath(), "Functions", "Binding", invocationId);
object convertedInput = ConvertInput(input);
ApplyBindingData(convertedInput, binder);
Dictionary<string, object> bindingData = binder.BindingData;
bindingData["InvocationId"] = invocationId;

Dictionary<string, string> environmentVariables = new Dictionary<string, string>();
InitializeEnvironmentVariables(environmentVariables, functionInstanceOutputPath, input, _outputBindings, functionExecutionContext);
await ProcessInputBindingsAsync(convertedInput, functionInstanceOutputPath, binder, _inputBindings, _outputBindings, bindingData, environmentVariables);

object convertedInput = ConvertInput(input);
ApplyBindingData(convertedInput, binder);
Dictionary<string, object> bindingData = binder.BindingData;
bindingData["InvocationId"] = invocationId;
// TODO
// - put a timeout on how long we wait?
// - need to periodically flush the standard out to the TraceWriter
Process process = CreateProcess(path, workingDirectory, arguments, environmentVariables);
process.Start();
process.WaitForExit();

await ProcessInputBindingsAsync(convertedInput, functionInstanceOutputPath, binder, _inputBindings, _outputBindings, bindingData, environmentVariables);
string output = process.StandardOutput.ReadToEnd();
TraceWriter.Info(output);
traceWriter.Info(output);

// TODO
// - put a timeout on how long we wait?
// - need to periodically flush the standard out to the TraceWriter
Process process = CreateProcess(path, workingDirectory, arguments, environmentVariables);
process.Start();
process.WaitForExit();
startedEvent.Success = process.ExitCode == 0;

string output = process.StandardOutput.ReadToEnd();
TraceWriter.Info(output);
traceWriter.Info(output);
if (!startedEvent.Success)
{
string error = process.StandardError.ReadToEnd();
throw new ApplicationException(error);
}

bool failed = process.ExitCode != 0;
startedEvent.Success = !failed;
_metrics.EndEvent(startedEvent);
await ProcessOutputBindingsAsync(functionInstanceOutputPath, _outputBindings, input, binder, bindingData);

if (failed)
TraceWriter.Info(string.Format("Function completed (Success, Id={0})", invocationId));
}
catch
{
startedEvent.Success = false;

TraceWriter.Error(string.Format("Function completed (Failure, Id={0})", invocationId));

string error = process.StandardError.ReadToEnd();
throw new ApplicationException(error);
throw;
}
finally
{
_metrics.EndEvent(startedEvent);
}

await ProcessOutputBindingsAsync(functionInstanceOutputPath, _outputBindings, input, binder, bindingData);

TraceWriter.Info(string.Format("Function completed (Success, Id={0})", invocationId));
}

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

0 comments on commit b903c91

Please sign in to comment.