示例#1
0
        public override async Task Invoke(object[] parameters)
        {
            // TODO: Refactor common code for providers.
            object           input       = parameters[0];
            TraceWriter      traceWriter = (TraceWriter)parameters[1];
            Binder           binder      = (Binder)parameters[2];
            ExecutionContext functionExecutionContext = (ExecutionContext)parameters[3];
            string           invocationId             = functionExecutionContext.InvocationId.ToString();

            FunctionStartedEvent startedEvent = new FunctionStartedEvent(functionExecutionContext.InvocationId, Metadata);

            _metrics.BeginEvent(startedEvent);

            try
            {
                TraceWriter.Info(string.Format("Function started (Id={0})", 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>();

                string functionInstanceOutputPath = Path.Combine(Path.GetTempPath(), "Functions", "Binding", invocationId);
                await ProcessInputBindingsAsync(convertedInput, functionInstanceOutputPath, binder, _inputBindings, _outputBindings, bindingData, environmentVariables);

                InitializeEnvironmentVariables(environmentVariables, functionInstanceOutputPath, input, _outputBindings, functionExecutionContext);

                PSDataCollection <ErrorRecord> errors = await InvokePowerShellScript(environmentVariables, traceWriter);

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

                if (errors.Any())
                {
                    ErrorRecord error = errors.FirstOrDefault();
                    if (error != null)
                    {
                        throw new RuntimeException("PowerShell script error", error.Exception, error);
                    }
                }
                else
                {
                    TraceWriter.Info(string.Format("Function completed (Success, Id={0})", invocationId));
                }
            }
            catch (Exception)
            {
                startedEvent.Success = false;
                TraceWriter.Error(string.Format("Function completed (Failure, Id={0})", invocationId));
                throw;
            }
            finally
            {
                _metrics.EndEvent(startedEvent);
            }
        }
        private string BuildLog(PSDataCollection <ErrorRecord> errors, string successMessage)
        {
            var log = new StringBuilder();

            if (errors.Any())
            {
                foreach (ErrorRecord e in errors)
                {
                    log.AppendLine("Error: " + e.Exception.Message);
                }
            }
            else
            {
                log.AppendLine(successMessage);
            }

            return(log.ToString());
        }
示例#3
0
        public string RunDragonflySimulateCommand(
            bool dragonfly,
            string simulationDir,
            string modelFilePath,
            string epwFilePath,
            string simulationParamPath,
            string oswFilePath = "")
        {
            try
            {
                var path = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.User);
                if (path == null)
                {
                    return(string.Empty);
                }

                string pyPath = null;
                foreach (var p in path.Split(';'))
                {
                    var fullPath = Path.Combine(p, "python.exe");
                    if (!File.Exists(fullPath))
                    {
                        continue;
                    }

                    pyPath = fullPath;
                    break;
                }

                if (string.IsNullOrWhiteSpace(pyPath))
                {
                    return(string.Empty);
                }

                var pyDir = Path.GetDirectoryName(pyPath);
                if (pyDir == null)
                {
                    return(string.Empty);
                }

                var energyModel = dragonfly ? "dragonfly" : "honeybee";
                var dfPath      = Path.Combine(pyDir, "Scripts", $"{energyModel}-energy.exe");
                if (!File.Exists(dfPath))
                {
                    return(string.Empty);
                }

                var ps = PowerShell.Create();
                ps.AddCommand(pyPath)
                .AddParameter("-m")
                .AddCommand(dfPath)
                .AddArgument("simulate")
                .AddArgument("model")
                .AddArgument("--folder")
                .AddArgument(simulationDir);

                if (!string.IsNullOrWhiteSpace(oswFilePath))
                {
                    ps.AddArgument("--base-osw");
                    ps.AddArgument(oswFilePath);
                }

                ps.AddArgument("--sim-par-json")
                .AddArgument(simulationParamPath)
                .AddArgument(modelFilePath)
                .AddArgument(epwFilePath);

                ps.Streams.Error.DataAdded += ErrorOnDataAdded;

                var outputCollection = new PSDataCollection <PSObject>();
                outputCollection.DataAdded += OutputCollectionOnDataAdded;
                var result = ps.BeginInvoke <PSObject, PSObject>(null, outputCollection);

                while (result.IsCompleted == false)
                {
                    Thread.Sleep(1000);
                }

                ps.Streams.Error.DataAdded -= ErrorOnDataAdded;

                return(outputCollection.Any() ? "Success!" : "Failure!");
            }
            catch (Exception e)
            {
                _logger.Fatal(e);
                return("Failure!");
            }
        }