Esempio n. 1
0
 protected void Application_End(object sender, EventArgs e)
 {
     using (stockdbaEntities db = new stockdbaEntities())
     {
         db.ServiceLog.Add(new ServiceLog()
         {
             updateTime = DateTime.Now, updateLog = "MainService_End"
         });
         db.SaveChanges();
     }
 }
Esempio n. 2
0
 void Application_End(object sender, EventArgs e)
 {
     //  應用程式關閉時執行的程式碼
     using (stockdbaEntities db = new stockdbaEntities())
     {
         db.ServiceLog.Add(new ServiceLog()
         {
             updateTime = DateTime.Now, updateLog = "MyStock_End"
         });
         db.SaveChanges();
     }
 }
        public WorkItem GetWork()
        {
            WorkItem  item = null;
            DateTime  receiveDate;
            DailyWork dailyWork;

            try
            {
                if (workStack.Count == 0)
                {
                    using (stockdbaEntities db = new stockdbaEntities())
                    {
                        receiveDate = db.DailyWork.OrderByDescending(o => o.receiveDate).First().receiveDate;
                        workStack.PushRange(db.DailyWork.Where(o => o.currentPage != o.totalPage && o.receiveDate == receiveDate).ToArray());
                    }
                }

                if (workStack.Count != 0)
                {
                    if (workStack.TryPop(out dailyWork) == true)
                    {
                        item             = new WorkItem();
                        item.receiveDate = dailyWork.receiveDate;
                        item.stockId     = dailyWork.stockId;
                        item.current     = (int)dailyWork.currentPage;
                        item.total       = (int)dailyWork.totalPage;

                        using (stockdbaEntities db = new stockdbaEntities())
                        {
                            db.ServiceLog.Add(new ServiceLog()
                            {
                                updateTime = DateTime.Now, updateLog = string.Format("GetWork:{0}", item.stockId)
                            });
                            db.SaveChanges();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                using (stockdbaEntities db = new stockdbaEntities())
                {
                    db.ServiceLog.Add(new ServiceLog()
                    {
                        updateTime = DateTime.Now, updateLog = String.Format("GetWork:{0}", ex.Message)
                    });
                    db.SaveChanges();
                }
            }

            return(item);
        }
Esempio n. 4
0
        void Application_Start(object sender, EventArgs e)
        {
            // 應用程式啟動時執行的程式碼
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterOpenAuth();

            using (stockdbaEntities db = new stockdbaEntities())
            {
                db.ServiceLog.Add(new ServiceLog()
                {
                    updateTime = DateTime.Now, updateLog = "MyStock_Start"
                });
                db.SaveChanges();
            }
        }
Esempio n. 5
0
        static void DetailQuery()
        {
            StockQuery worker = new StockQuery();
            StockAnalyser analyser = new StockAnalyser();

            DateTime receiveDate;
            List<DailyWork> workList = new List<DailyWork>();

            ParallelOptions option = new ParallelOptions();
            option.MaxDegreeOfParallelism = 10;

            int retryCnt = 0;
            Random rnd = new Random();

            while (true)
            {
                using (stockdbaEntities db = new stockdbaEntities())
                {
                    receiveDate = db.DailyWork.OrderByDescending(o => o.receiveDate).First().receiveDate;
                    workList.AddRange(db.DailyWork.Where(o => o.currentPage != o.totalPage && o.receiveDate == receiveDate));
                }

                if (workList.Count() > 0)
                {
                    Parallel.ForEach(workList, option, item =>
                    {
                        try
                        {
                            Thread.Sleep(rnd.Next(100, 1000));
                            for (int i = (int)item.currentPage + 1; i <= (int)item.totalPage; i++)
                            {
                                worker.QuerySinglePage(i, item.stockId);
                                using (stockdbaEntities db = new stockdbaEntities())
                                {
                                    db.DailyWork.Where(o => o.receiveDate == item.receiveDate && o.stockId == item.stockId).First().currentPage = i;
                                    db.ServiceLog.Add(new ServiceLog() { updateTime = DateTime.Now, updateLog = string.Format("Run:{0}:{1}/{2}", item.stockId, i, item.totalPage) });
                                    db.SaveChanges();

                                    Logger(string.Format("Run:{0}:{1}/{2}", item.stockId, i, item.totalPage));

                                    if (i == item.totalPage)
                                    {
                                        string stockName = "-";
                                        var obj = db.Company.Where(o => o.stockId == item.stockId).FirstOrDefault();
                                        if (obj != null)
                                        {
                                            stockName = obj.name;
                                        }
                                        // 結算
                                        analyser.DoSettlement(item.receiveDate, item.stockId);
                                        // 集中率
                                        analyser.DoDailyRate(item.receiveDate, item.stockId, stockName);
                                        analyser.DoWeeklyRate(item.receiveDate, item.stockId, stockName);
                                        analyser.DoTotalRate(item.receiveDate, item.stockId, stockName);

                                        Logger(string.Format("Run:{0}: Settlement", item.stockId));
                                    }
                                }
                                Thread.Sleep(10000);
                            }
                        }
                        catch (Exception ex)
                        {
                            using (stockdbaEntities db = new stockdbaEntities())
                            {
                                db.ServiceLog.Add(new ServiceLog() { updateTime = DateTime.Now, updateLog = String.Format("Run:{0}", ex.Message) });
                                db.SaveChanges();
                            }
                        }
                    });
                    workList.Clear();
                }
                else
                {
                    if (retryCnt < 60)
                    {
                        retryCnt++;
                        Thread.Sleep(60000); // 1min
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
        public string Run()
        {
            Task nodeTask = new Task(() =>
            {
                MainServiceReference.MainServiceSoapClient client = new MainServiceReference.MainServiceSoapClient();
                MainServiceReference.WorkItem item;

                StockQuery worker = new StockQuery();

                while ((item = client.GetWork()) != null)
                {
                    try
                    {
                        using (stockdbaEntities db = new stockdbaEntities())
                        {
                            db.ServiceLog.Add(new ServiceLog()
                            {
                                updateTime = DateTime.Now, updateLog = string.Format("Run:{0}", item.stockId)
                            });
                            db.SaveChanges();
                        }

                        for (int i = item.current + 1; i <= item.total; i++)
                        {
                            worker.QuerySinglePage(i, item.stockId);
                            using (stockdbaEntities db = new stockdbaEntities())
                            {
                                db.DailyWork.Where(o => o.receiveDate == item.receiveDate && o.stockId == item.stockId).First().currentPage = i;
                                db.ServiceLog.Add(new ServiceLog()
                                {
                                    updateTime = DateTime.Now, updateLog = string.Format("Run:{0}:{1}/{2}", item.stockId, i, item.total)
                                });
                                db.SaveChanges();
                            }
                            Thread.Sleep(10000);
                        }
                    }
                    catch (Exception ex)
                    {
                        using (stockdbaEntities db = new stockdbaEntities())
                        {
                            db.ServiceLog.Add(new ServiceLog()
                            {
                                updateTime = DateTime.Now, updateLog = String.Format("Run:{0}", ex.Message)
                            });
                            db.SaveChanges();
                        }
                    }
                }
                Interlocked.Decrement(ref taskNum);
            });

            if (taskNum < 10)
            {
                nodeTask.Start();
                Interlocked.Increment(ref taskNum);
                return(String.Format("New Task, TaskNum:{0}", taskNum));
            }

            return(String.Format("TaskNum:{0}", taskNum));
        }