Example #1
0
        public static void ProcessQueueMessage([QueueTrigger("billingdatarequests")] BillingRequest br)
        {
            Console.WriteLine("Start webjob process. SubscriptionID: {0}", br.SubscriptionId);
            int retriesLeft = Convert.ToInt32(ConfigurationManager.AppSettings["ida:RetryCountToProcessMessage"].ToString());

            while (retriesLeft > 0)
            {
                --retriesLeft;
                if (retriesLeft < 1)
                {
                    Console.WriteLine("Finished internal retries, throwing exception. Time:{0}", DateTime.UtcNow.ToString());
                    throw new Exception();
                }

                Console.WriteLine("Start time:{0} Retries Left: {1}", DateTime.UtcNow.ToString(), retriesLeft);

                try
                {
                    //Fetch RateCard information First
                    string rateCardURL = AzureResourceManagerUtil.GetRateCardRestApiCallURL(br.SubscriptionId,
                                                                                            ConfigurationManager.AppSettings["ida:OfferCode"].ToString(),
                                                                                            ConfigurationManager.AppSettings["ida:Currency"].ToString(),
                                                                                            ConfigurationManager.AppSettings["ida:Locale"].ToString(),
                                                                                            ConfigurationManager.AppSettings["ida:RegionInfo"].ToString());
                    Console.WriteLine("Request cost info from RateCard service.");
                    RateCardPayload rateCardInfo = GetRateCardInfo(rateCardURL, br.OrganizationId);
                    if (rateCardInfo == null)
                    {
                        Console.WriteLine("Problem receiving cost info occured - see log for details.");
                        continue;
                    }
                    else
                    {
                        Console.WriteLine("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.Now.AddYears(-3);
                    //DateTime edt = DateTime.Now.AddDays(-1);

                    string restURL = AzureResourceManagerUtil.GetBillingRestApiCallURL(br.SubscriptionId, false, true, br.StartDate, br.EndDate);

                    Console.WriteLine("Request usage data from Billing service.");
                    List <UsageRecord> urs = GetUsageDetails(restURL, br.OrganizationId, rateCardInfo);
                    Console.WriteLine("Received record count: {0}", urs.Count);

                    Console.WriteLine("Insert usage data into SQL Server.");
                    InsertIntoSQLDB(urs);

                    break;
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception: ProcessQueueMessage->e.Message: " + e.Message);

                    if (retriesLeft == 0)
                    {
                        throw;
                    }
                }

                Console.WriteLine("Sleeping in ProcessQueueMessage while loop for 5 min. DateTime: {0}", DateTime.Now.ToString());
                Thread.Sleep(1000 * 60 * 5);
            }   // while

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

            Console.WriteLine("Complete webjob process. SubscriptionID: {0}", br.SubscriptionId);
        }
Example #2
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}");
        }