コード例 #1
0
ファイル: SendSalesOrderRequests.cs プロジェクト: msaleh/HRM
        public void Run()
        {
            try
                 {

                     //  Trace.TraceInformation("Job Run from " + DateTime.Now.AddMinutes(-30).ToString("u") + " TO " + DateTime.Now.ToString("u"));
                     IeBayServiceClient client = new IeBayServiceClient();
                     var orders = client.GetOrders(DateTime.Now.AddDays(-5).ToString("u"), DateTime.Now.ToString("u"));
                     client.Close();

                     string acsNamespace = ConfigurationManager.AppSettings["acsNamespace"].ToString();// "sdebay9480-bts";
                     string issuerName = ConfigurationManager.AppSettings["issuerName"].ToString();//"owner";
                     string issuerKey = ConfigurationManager.AppSettings["issuerKey"].ToString();//"WTS3ADQL5RqaLc/xuOzUahAIboTqRsQH+Ru5KzWu2qo=";
                     string runtimeAddress = ConfigurationManager.AppSettings["runtimeAddress"].ToString();//"https://sdebay.biztalk.windows.net/default/XmlOneWayBridge2";
                     string contentType = "application/xml";

                     XmlDocument x = new XmlDocument();

                     try
                     {

                         x.LoadXml(orders.ToString());

                         var elemnt = x.GetElementsByTagName("Build")[0];
                         elemnt.InnerText = DateTime.Now.ToFileTime() + ".txt";
                     }
                     catch { }

                     var fileBytes = Encoding.Default.GetBytes(x.OuterXml);
                     ValidateInput(contentType);
                     ebayRequestSenderWorkerRole.MessageSender.SendMessage(acsNamespace, issuerName, issuerKey, runtimeAddress, fileBytes, contentType);
                 }
                 catch (Exception ex)
                 {
                     Trace.WriteLine("Worker job scheduled Moe" + ex.Message);
                     Trace.TraceError("Worker job scheduled Moe" + ex.Message);
                 }
        }
コード例 #2
0
ファイル: WorkerRole.cs プロジェクト: msaleh/HRM
        public override void Run()
        {
            while (true)
            {
                //http://go.microsoft.com/fwlink/?LinkId=532517 tracking log
                // time stored in ebay UTC universal format
                // server Hosted  on EAST USA  time zone eastern UTC +4 hours that eastern Usa zone,
                // for example now in easter 05/12/2015 10:00 AM  UTC 05/12/2015 2:00 PM
                // in server  date.now return  universal UTC

                //run this at first
                RunUnPaidOrder();
                string PullingOrderRecurEveryDB = "30";// in minutes
                try
                {
                   bool RunPullingOrder = bool.Parse(HartvilleToolConfigManager.GetSetting("RunPullingOrder"));

                  # region RunPullingOrder

                if (RunPullingOrder)
                {    // get from db PullingOrderRecurEvery

                    string OrderPullInterval = HartvilleToolConfigManager.GetSetting("OrderPullInterval");
                     PullingOrderRecurEveryDB = HartvilleToolConfigManager.GetSetting("PullingOrderRecurEvery");
                     // TODO keep 5 minutes diff been cureent time and lasdateto handle in case
                    if (string.IsNullOrEmpty(PullingOrderRecurEveryDB))
                         PullingOrderRecurEveryDB = "30";// in minutes
                    if (string.IsNullOrEmpty(OrderPullInterval))
                        OrderPullInterval = "30";// in minutes default

                     int OrderAgeInMinutes = 10;
                     string StrOrderAgeInMinutes = HartvilleToolConfigManager.GetSetting("OrderAgeInMinutes");
                     if (!string.IsNullOrEmpty(StrOrderAgeInMinutes))
                     {
                        OrderAgeInMinutes = int.Parse(StrOrderAgeInMinutes);
                     }

                     bool IsErrorOccured = false;
                     XmlElement orders = null;
                     DateTime RequestedDateFrom = HartvilleToolConfigManager.GetLastDateFromRun();//new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
                     DateTime RequestedDateTo = RequestedDateFrom.AddMinutes(int.Parse(OrderPullInterval));
                     /*Note: If a GetOrders call is made within a few seconds after the creation of a multiple line item order, the caller runs the risk
                      * of retrieving orders that are in an inconsistent state, since the order consolidation involved in a multi-line item order may not have been completed. For this reason,
                      * it is recommended that sellers include the CreateTimeTo field in the call, and set its value to: Current Time - 2 minutes. */

                     // her will check and make sure RequestedDateTo less that current date 10 m at least
                     DateTime CurrentDatetime = DateTime.Now;
                     if (OrderAgeInMinutes > 0)
                     {
                         var timeDiffM = (CurrentDatetime - RequestedDateTo).TotalMinutes;
                         if (timeDiffM > OrderAgeInMinutes)
                         {
                             var ValueDifftoAdd = timeDiffM - OrderAgeInMinutes;
                             if (ValueDifftoAdd > 0)
                             {
                                 DateTime RequestedDateToNew = RequestedDateTo.AddMinutes(ValueDifftoAdd);
                                 RequestedDateTo = RequestedDateToNew;
                             }
                         }
                         else // in case dateto become future date always less tha current time -OrderAgeInMinutes
                         {
                             RequestedDateTo = CurrentDatetime.AddMinutes(-1 * OrderAgeInMinutes);
                         }
                     }

                     //in case dateto become future date always less tha current time -OrderAgeInMinutes
                     if (RequestedDateTo >= CurrentDatetime)
                     {
                         RequestedDateTo = CurrentDatetime.AddMinutes(-1 * OrderAgeInMinutes);
                     }

                     //in case the datefrom time become after dateto return -30 based on dateto
                     if (RequestedDateFrom >= RequestedDateTo)
                     {
                         RequestedDateFrom = RequestedDateTo.AddMinutes(-1 * int.Parse(OrderPullInterval));
                     }

                     try
                     {
                         //Trace.TraceInformation("Job started pull order", DateTime.Now.AddMinutes(-30).ToString("u") +
                         //    " TO " + DateTime.Now.ToString("u"));
                         IeBayServiceClient client = new IeBayServiceClient();

                         try
                         {
                             client.Endpoint.Address = new EndpointAddress(new Uri(HartvilleToolConfigManager.GetSetting("Biztalk.eBayService")), client.Endpoint.Address.Identity,
                                 client.Endpoint.Address.Headers);
                             //YYYY-MM-DDTHH:MM:SS.SSSZ
                             orders = client.GetOrders(RequestedDateFrom.ToString("u"), RequestedDateTo.ToString("u"));
                             client.Close();
                             HartvilleToolConfigManager.NotifyEmailInfo("Orders retrived from Ebay see attached orders", orders != null ? orders.OuterXml : "");

                             // check the reeposne from ebay make sure no error if error that failed requst
                             var IsFailed = orders.OuterXml.ToLower().IndexOf("<ack>failure</ack>") >= 0 || orders.OuterXml.ToLower().IndexOf("<ack>partialfailure</ack>") >= 0;
                             if (IsFailed)
                             {
                                 IsErrorOccured = true;
                                 string ErrorLongMessage = ExtractErrorLongMessage(orders.OuterXml);
                                 HartvilleToolConfigManager.NotifyEmailError("Error calling ebay Service " + ErrorLongMessage, orders != null ? orders.OuterXml : "");
                             }
                             //try
                             //{
                             //    // check if there any error occured
                             //    var SeverityCodeElemnt = orders.GetElementsByTagName("SeverityCode");
                             //    var ErrorCodeElemnt = orders.GetElementsByTagName("ErrorCode");
                             //    if (SeverityCodeElemnt != null && SeverityCodeElemnt.Count > 0 &&
                             //        ErrorCodeElemnt != null && ErrorCodeElemnt.Count > 0)// only submit if there is at least one order
                             //    {
                             //        throw new Exception("Error calling ebay Service see attached message for details");
                             //    }
                             //}
                             //catch { }
                         }
                         catch (Exception ex)
                         {
                             IsErrorOccured = true;
                             client.Abort();
                             Trace.TraceError("Error calling ebay Service ", ex.Message);
                             HartvilleToolConfigManager.NotifyEmailError("Error calling ebay Service " + ex.Message, orders != null ? orders.OuterXml : "");
                         }

                         if (orders != null && IsErrorOccured == false)
                         {
                             XmlDocument x = new XmlDocument();
                             x.LoadXml(orders.OuterXml);

                             var Orderselemnt = x.GetElementsByTagName("Order");

                             string SytemId = "";
                             try
                             {
                                 var SellingManagerSalesRecordNumber = x.GetElementsByTagName("SellingManagerSalesRecordNumber");
                                 if (SellingManagerSalesRecordNumber != null && SellingManagerSalesRecordNumber.Count > 0)
                                 {
                                     SytemId = SellingManagerSalesRecordNumber[0].InnerText;
                                 }
                             }
                             catch { }

                             if (Orderselemnt != null && Orderselemnt.Count > 0)// only submit if there is at least one order
                             {
                                 string FTPfilename = SytemId + "_" + DateTime.Now.ToFileTime() + ".txt";
                                 var elemnt = x.GetElementsByTagName("Build")[0];
                                 elemnt.InnerText = FTPfilename;
                                 var fileBytes = Encoding.Default.GetBytes(x.OuterXml);

                                 //Trace.TraceInformation("Finished called order");
                                 string acsNamespace = HartvilleToolConfigManager.GetSetting("Biztalk.acsNamespace"); // ConfigurationManager.AppSettings["acsNamespace"].ToString();
                                 string issuerName = HartvilleToolConfigManager.GetSetting("Biztalk.issuerName"); //ConfigurationManager.AppSettings["issuerName"].ToString();
                                 string issuerKey = HartvilleToolConfigManager.GetSetting("Biztalk.issuerKey"); // ConfigurationManager.AppSettings["issuerKey"].ToString();
                                 string runtimeAddress = HartvilleToolConfigManager.GetSetting("Biztalk.runtimeAddress"); //ConfigurationManager.AppSettings["runtimeAddress"].ToString();
                                 string contentType = "application/xml";

                                 // Trace.TraceInformation("Prepare for sending to Biztalk");
                                 ebayRequestSenderWorkerRole.MessageSender.SendMessage(acsNamespace,
                                     issuerName, issuerKey, runtimeAddress,
                                     fileBytes, contentType);

                                 HartvilleToolConfigManager.NotifyEmailInfoWithoutCheck("Orders Placed in FTP FileName: " + FTPfilename, orders != null ? orders.OuterXml : "");
                                 HartvilleToolConfigManager.NotifyEmailInfo("Orders Sent to biztalk to proccess", orders != null ? orders.OuterXml : "");
                             }
                         }
                         // Trace.TraceInformation("Sent for Biztalk");
                     }
                     catch (Exception ex)
                     {
                         IsErrorOccured = true;
                         //Trace.WriteLine("Exception during OnStart: " + ex.ToString());
                         Trace.TraceError("Stopped sending to biztalk ERROR ", ex.Message);
                         HartvilleToolConfigManager.NotifyEmailError("Stopped sending to biztalk ERROR " + ex.Message, orders != null ? orders.OuterXml : "");
                     }

                     // here will update last pull
                     // save time in local time not universal sicne what will we use for compare
                     // if no error occured
                     if ( IsErrorOccured == false)
                     {
                         HartvilleToolConfigManager.InsertIntoeBayServiceRun(RequestedDateFrom, RequestedDateTo, orders != null ? orders.OuterXml : null);
                        // HartvilleToolConfigManager.UpdateLastRunDateTime(RequestedDateFrom.ToString(), RequestedDateTo.ToString());
                     }
                     // reset for next time
                      IsErrorOccured = false;
                 }

                # endregion

                }
                catch (Exception ex)
                {
                    HartvilleToolConfigManager.NotifyEmailError("Error Error in Schedule program service not called " + ex.Message, "");
                }
                //schedule for running for coming 30 m
                int PullingOrderRecurEvery = int.Parse(PullingOrderRecurEveryDB) * 60 * 1000;
                Thread.Sleep(PullingOrderRecurEvery);
            }
        }