예제 #1
0
        public IActionResult Invoke(string name, [FromBody] FunctionInvocation invocation, [FromServices] IScriptJobHost scriptHost)
        {
            if (invocation == null)
            {
                return(BadRequest());
            }

            FunctionDescriptor function = scriptHost.GetFunctionOrNull(name);

            if (function == null)
            {
                return(NotFound());
            }

            ParameterDescriptor         inputParameter = function.Parameters.First(p => p.IsTrigger);
            Dictionary <string, object> arguments      = new Dictionary <string, object>()
            {
                { inputParameter.Name, invocation.Input }
            };
            // LiveLogs session id is used to show only contextual logs in the "Code + Test" experience.
            string sessionId = this.Request?.Headers[ScriptConstants.LiveLogsSessionAIKey];

            using (System.Threading.ExecutionContext.SuppressFlow())
            {
                Task.Run(async() =>
                {
                    IDictionary <string, object> loggerScope = new Dictionary <string, object>
                    {
                        { "MS_IgnoreActivity", null }
                    };

                    using (_logger.BeginScope(loggerScope))
                    {
                        if (!string.IsNullOrWhiteSpace(sessionId))
                        {
                            // Current activity is null due to SuppressFlow. Start a new activity and set baggage so that it's included in the custom dimensions.
                            Activity activity = new Activity($"{nameof(FunctionsController)}.Invoke");
                            activity?.Start();
                            activity?.AddBaggage(ScriptConstants.LiveLogsSessionAIKey, sessionId);
                            try
                            {
                                await scriptHost.CallAsync(function.Name, arguments);
                            }
                            finally
                            {
                                activity?.Stop();
                            }
                        }
                        else
                        {
                            await scriptHost.CallAsync(function.Name, arguments);
                        }
                    }
                });
            }

            return(Accepted());
        }
예제 #2
0
        public IActionResult Invoke(string name, [FromBody] FunctionInvocation invocation, [FromServices] IScriptJobHost scriptHost)
        {
            if (invocation == null)
            {
                return(BadRequest());
            }

            FunctionDescriptor function = scriptHost.GetFunctionOrNull(name);

            if (function == null)
            {
                return(NotFound());
            }

            ParameterDescriptor         inputParameter = function.Parameters.First(p => p.IsTrigger);
            Dictionary <string, object> arguments      = new Dictionary <string, object>()
            {
                { inputParameter.Name, invocation.Input }
            };

            Task.Run(async() =>
            {
                IDictionary <string, object> loggerScope = new Dictionary <string, object>
                {
                    { "MS_IgnoreActivity", null }
                };

                using (_logger.BeginScope(loggerScope))
                {
                    await scriptHost.CallAsync(function.Name, arguments);
                }
            });

            return(Accepted());
        }
예제 #3
0
        public async Task ExecuteAsync(HttpRequest request, CancellationToken cancellationToken)
        {
            if (!CanExecute)
            {
                throw new InvalidOperationException("Unable to execute function without a target.");
            }

            JObject coldStartData = null;

            if (request.IsColdStart())
            {
                coldStartData = new JObject
                {
                    { "requestId", request.GetRequestId() },
                    { "language", Descriptor.Metadata.Language },
                    { "sku", _environment.GetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteSku) }
                };

                var dispatchStopwatch = request.GetItemOrDefault <Stopwatch>(ScriptConstants.AzureFunctionsColdStartKey);
                if (dispatchStopwatch != null)
                {
                    dispatchStopwatch.Stop();
                    coldStartData.Add("dispatchDuration", dispatchStopwatch.ElapsedMilliseconds);
                }
            }

            var sw = Stopwatch.StartNew();

            var arguments = new Dictionary <string, object>();

            if (_descriptor.IsWarmupFunction())
            {
                arguments.Add(_descriptor.TriggerParameter.Name, new WarmupContext());
            }
            else
            {
                arguments.Add(_descriptor.TriggerParameter.Name, request);
            }

            await _host.CallAsync(_descriptor.Name, arguments, cancellationToken);

            sw.Stop();

            if (coldStartData != null)
            {
                coldStartData.Add("functionDuration", sw.ElapsedMilliseconds);

                var logData = new Dictionary <string, object>
                {
                    [ScriptConstants.LogPropertyEventNameKey]  = ScriptConstants.ColdStartEventName,
                    [ScriptConstants.LogPropertyActivityIdKey] = request.GetRequestId()
                };
                _logger.Log(LogLevel.Information, 0, logData, null, (s, e) => coldStartData.ToString(Formatting.None));
            }
        }
예제 #4
0
        /// <summary>
        /// Try to invoke a warmup function if available
        /// </summary>
        /// <returns>
        /// A task that represents the asynchronous operation.
        /// The task results true if a warmup function was invoked, false otherwise.
        /// </returns>
        public static async Task <bool> TryInvokeWarmupAsync(this IScriptJobHost scriptJobHost)
        {
            var warmupFunction = scriptJobHost.GetWarmupFunctionOrNull();

            if (warmupFunction != null)
            {
                ParameterDescriptor inputParameter = warmupFunction.Parameters.First(p => p.IsTrigger);

                var arguments = new Dictionary <string, object>()
                {
                    { inputParameter.Name, new WarmupContext() }
                };

                await scriptJobHost.CallAsync(warmupFunction.Name, arguments);

                return(true);
            }

            return(false);
        }
        public IActionResult Invoke(string name, [FromBody] FunctionInvocation invocation, [FromServices] IScriptJobHost scriptHost)
        {
            if (invocation == null)
            {
                return(BadRequest());
            }

            FunctionDescriptor function = scriptHost.GetFunctionOrNull(name);

            if (function == null)
            {
                return(NotFound());
            }

            ParameterDescriptor         inputParameter = function.Parameters.First(p => p.IsTrigger);
            Dictionary <string, object> arguments      = new Dictionary <string, object>()
            {
                { inputParameter.Name, invocation.Input }
            };

            Task.Run(() => scriptHost.CallAsync(function.Name, arguments));

            return(Accepted());
        }