Exemplo n.º 1
0
        public static void Run([TimerTrigger("*/30 * * * * *")] TimerInfo myTimer, ILogger logger)
        {
            // CRON expression syntax: <second> <minute> <hour> <day-of-month> <month> <day-of-week> <year> <command>

            AppInsightsLogger appInsightsLogger = new AppInsightsLogger(logger, LOGGING_SERVICE_NAME, LOGGING_SERVICE_VERSION);
            QueueLogger       queueLogger       = new QueueLogger(appInsightsLogger);

            queueLogger.LogQueueLength(BACKEND_STATUS_CREATED_PATTERN, adjustment: 1);  // Add 1 so that we account for > 1 waiting.
        }
        public static void Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, ILogger logger)
        {
            // CRON expression syntax: <second> <minute> <hour> <day-of-month> <month> <day-of-week> <year> <command>

            AppInsightsLogger appInsightsLogger = new AppInsightsLogger(logger, LOGGING_SERVICE_NAME, LOGGING_SERVICE_VERSION);
            QueueLogger       queueLogger       = new QueueLogger(appInsightsLogger);

            queueLogger.LogQueueLength(BACKEND_STATUS_COMPLETED_PATTERN);
            queueLogger.LogQueueLength(BACKEND_STATUS_RUNNING_PATTERN);
            queueLogger.LogQueueLength(BACKEND_STATUS_FAILED_PATTERN);
        }
        public static IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req, ILogger logger)
        {
            IDatabase         db = null;
            AppInsightsLogger appInsightsLogger = new AppInsightsLogger(logger, LOGGING_SERVICE_NAME, LOGGING_SERVICE_VERSION);

            string uuid      = "nil";
            var    apiParams = req.GetQueryParameterDictionary();

            if (apiParams != null && apiParams.Keys.Contains(UUID_KEYNAME))
            {
                uuid = apiParams[UUID_KEYNAME];
                appInsightsLogger.LogInformation("Getting status for taskId: " + uuid, URL, uuid);
            }
            else
            {
                appInsightsLogger.LogWarning("Called without a taskId", URL);
                return(new BadRequestObjectResult("The taskId parameter is requried."));
            }

            try
            {
                db = RedisConnection.GetDatabase();
            }
            catch (Exception ex)
            {
                appInsightsLogger.LogError(ex, URL, uuid);
                return(new StatusCodeResult(500));
            }

            RedisValue storedStatus = RedisValue.Null;

            try
            {
                storedStatus = db.StringGet(uuid);

                if (storedStatus.HasValue)
                {
                    appInsightsLogger.LogInformation("Found status in cache", URL, uuid);

                    return(new OkObjectResult(storedStatus.ToString()));
                }
                else
                {
                    appInsightsLogger.LogWarning("Cannot find status in cache", URL, uuid);
                    return(new StatusCodeResult(204));
                }
            }
            catch (Exception ex)
            {
                appInsightsLogger.LogError(ex, URL, uuid);
                return(new StatusCodeResult(500));
            }
        }
Exemplo n.º 4
0
 public QueueLogger(AppInsightsLogger appInsightsLogger)
 {
     this.appInsightsLogger = appInsightsLogger;
 }
        public static IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, ILogger logger)
        {
            AppInsightsLogger appInsightsLogger = new AppInsightsLogger(logger, LOGGING_SERVICE_NAME, LOGGING_SERVICE_VERSION);

            string       response = string.Empty;
            const string SubscriptionValidationEvent = "Microsoft.EventGrid.SubscriptionValidationEvent";

            string requestContent = new StreamReader(req.Body).ReadToEnd();

            EventGridEvent[] eventGridEvents = JsonConvert.DeserializeObject <EventGridEvent[]>(requestContent);

            // We should only have 1 event
            foreach (EventGridEvent eventGridEvent in eventGridEvents)
            {
                JObject dataObject = eventGridEvent.Data as JObject;

                // Deserialize the event data into the appropriate type based on event type
                if (string.Equals(eventGridEvent.EventType, SubscriptionValidationEvent, StringComparison.OrdinalIgnoreCase))
                {
                    var eventData = dataObject.ToObject <SubscriptionValidationEventData>();
                    appInsightsLogger.LogInformation($"Got SubscriptionValidation event data, validation code: {eventData.ValidationCode}, topic: {eventGridEvent.Topic}", string.Empty);
                    // Do any additional validation (as required) and then return back the below response
                    var responseData = new SubscriptionValidationResponse();
                    responseData.ValidationResponse = eventData.ValidationCode;
                    return(new OkObjectResult(responseData));
                }
                else
                {
                    appInsightsLogger.LogInformation($"Backend webhook data: id: {eventGridEvent.Id}, eventType: {eventGridEvent.EventType}, subject: {eventGridEvent.Subject}, time: {eventGridEvent.EventTime}", eventGridEvent.Subject, eventGridEvent.Id);

                    var backendUri = new Uri(eventGridEvent.Subject);

                    var client        = new HttpClient(new RetryHandler(new HttpClientHandler()));
                    var stringContent = new StringContent(eventGridEvent.Data.ToString(), Encoding.UTF8, "application/json");

                    try
                    {
                        appInsightsLogger.LogInformation($"Sending request to {backendUri} for taskId {eventGridEvent.Id}.", eventGridEvent.Subject, eventGridEvent.Id);
                        client.DefaultRequestHeaders.Add("taskId", eventGridEvent.Id);
                        var res = client.PostAsync(backendUri, stringContent).GetAwaiter().GetResult();

                        if (res.StatusCode == (System.Net.HttpStatusCode) 429) // Special return value indicating that the service is busy.  Let event grid handle the retries.
                        {
                            appInsightsLogger.LogInformation("Backend service is busy. Event grid will retry with backoff.", eventGridEvent.Subject, eventGridEvent.Id);
                            return(new StatusCodeResult(429));
                        }
                        else if (!res.IsSuccessStatusCode)
                        {
                            appInsightsLogger.LogWarning($"Unable to send request to backend. Status: {res.StatusCode.ToString()}, Reason: {res.ReasonPhrase}", eventGridEvent.Subject, eventGridEvent.Id);
                            return(new StatusCodeResult(500));
                        }
                    }
                    catch (Exception ex)
                    {
                        appInsightsLogger.LogError(ex, eventGridEvent.Subject, eventGridEvent.Id);
                        return(new StatusCodeResult(500));
                    }

                    appInsightsLogger.LogInformation("Request has successfully been pushed to the backend.", eventGridEvent.Subject, eventGridEvent.Id);
                    return(new OkResult());
                }
            }

            return(new OkResult());
        }