Esempio n. 1
0
        public static void ProcessQueueMessage([QueueTrigger("billingdatarequests")] BillingRequest billingRequest, TextWriter logWriter = null)
        {
            if (logWriter != null)
            {
                TextWriterTraceListener traceListener = new TextWriterTraceListener(logWriter, "LogWriter");
                Trace.Listeners.Remove("LogWriter");
                Trace.Listeners.Add(traceListener);
                Trace.TraceInformation("Azure WebJob Log Writer configured");
            }

            Trace.TraceInformation($"WebJob process started. {nameof(billingRequest.SubscriptionId)}: {billingRequest.SubscriptionId}");
            int       retriesLeft   = Convert.ToInt32(RetryCountToProcessMessage);
            Exception lastException = null;

            while (retriesLeft > 0)
            {
                --retriesLeft;

                if (retriesLeft < 1)
                {
                    Trace.TraceInformation($"Finished internal retries, time:{DateTime.UtcNow}");

                    if (lastException != null)
                    {
                        throw lastException;
                    }
                    else
                    {
                        return;
                    }
                }

                Trace.TraceInformation($"Start time:{DateTime.UtcNow}, retries Left: {retriesLeft}");

                try {
                    //Fetch RateCard information First
                    string rateCardUrl = AzureResourceManagerUtil.GetRateCardRestApiCallURL(billingRequest.SubscriptionId, OfferCode, Currency, Locale, RegionInfo);
                    Trace.TraceInformation("Request cost info from RateCard service.");

                    RateCardPayload rateCardInfo = GetRateCardInfo(rateCardUrl, billingRequest.OrganizationId);

                    if (rateCardInfo == null)
                    {
                        Trace.TraceWarning("Problem receiving cost info occured - see log for details.");
                        continue;
                    }
                    else
                    {
                        Trace.TraceInformation("Received cost info: " + rateCardInfo.ToString());
                    }

                    // if granularity=hourly then report up to prev. hour,
                    // if granularity=daily then report up to prev. day. Othervise will get 400 error
                    //DateTime sdt = DateTime.UtcNow.Date.AddYears(-3);
                    //DateTime edt = DateTime.UtcNow.Date.AddDays(-1);

                    // see: https://msdn.microsoft.com/en-us/library/azure/mt219004.aspx
                    string restUrl = AzureResourceManagerUtil.GetBillingRestApiCallUrl(billingRequest.SubscriptionId, true, true, billingRequest.StartDate, billingRequest.EndDate);

                    Trace.TraceInformation("Request usage data from Billing service.");
                    var usageRecords = GetUsageDetails(restUrl, billingRequest.OrganizationId, rateCardInfo);
                    Trace.TraceInformation($"Received record count: {usageRecords.Count}");

                    if (usageRecords.Count > 0)
                    {
                        Trace.TraceInformation("Inserting usage records into SQL database.");
                        Task <int> task        = InsertIntoSqlDbAsync(usageRecords, billingRequest.SubscriptionId, billingRequest.StartDate, billingRequest.EndDate);
                        int        recordCount = task.GetAwaiter().GetResult();
                        Trace.TraceInformation($"Total {recordCount} usage record(s) inserted.");
                    }
                    else
                    {
                        Trace.TraceInformation("No usage data found.");
                    }

                    break;
                } catch (Exception e) {
                    Trace.TraceError($"Exception: {nameof(ProcessQueueMessage)} -> e.Message: " + e.Message);
                    lastException = e;
                    if (retriesLeft == 0)
                    {
                        throw;
                    }
                }

                Trace.TraceInformation($"Sleeping in {nameof(ProcessQueueMessage)} while loop for 5 min. DateTime: {DateTime.UtcNow}");
                Thread.Sleep(1000 * 60 * 5);
            }               // while

            Utils.UpdateSubscriptionStatus(billingRequest.SubscriptionId, DataGenStatus.Completed, DateTime.UtcNow);

            Trace.TraceInformation($"WebJob process completed. SubscriptionId: {billingRequest.SubscriptionId}");
        }