Exemplo n.º 1
0
        private void DoRefreshInternal()
        {
            var businessList = Database.GetBusinessLineList(null);

            List <Task> tasks = new List <Task>();

            foreach (var biz in businessList)
            {
                foreach (var ln in biz.Line)
                {
                    log.DebugFormat("Creating cache task for business {0}, line {1}", biz.Id, ln.Id);

                    tasks.Add(
                        new Task(() =>
                    {
                        var items = ExternalData.Fetch(biz.Id, ln.Id, 2);
                        if (items.Count == 0)
                        {
                            log.DebugFormat("Nothing found in external data for line {0}", ln.Id);
                        }
                        else
                        {
                            var lastLineQueueId       = items.OrderByDescending(i => i.QueueId).First().QueueId;
                            var lastStoredLineQueueId = Database.GetLastLineQueueId(ln.Id);

                            if (!lastStoredLineQueueId.HasValue)
                            {
                                log.DebugFormat("Nothing in database for line {0}, going to merge {1} items", ln.Id, items.Count);
                                var dbItems = Mapper.Map <IEnumerable <Models.Shared.DataItem>, IEnumerable <AdapterDb.DataItem> >(items).ToList();
                                Database.MergeDataItems(dbItems);
                                log.DebugFormat("Line {0} merged", ln.Id);
                            }
                            else if (lastLineQueueId.Value > lastStoredLineQueueId.Value)
                            {
                                log.DebugFormat("External data has new items for line {0}, going to merge {1} items", ln.Id, items.Count);
                                var dbItems = Mapper.Map <IEnumerable <Models.Shared.DataItem>, IEnumerable <AdapterDb.DataItem> >(items).ToList();
                                Database.MergeDataItems(dbItems);
                                log.DebugFormat("Line {0} merged", ln.Id);
                            }
                            else if (lastLineQueueId.Value == lastStoredLineQueueId.Value)
                            {
                                log.DebugFormat("Nothing to refresh for line {0}, last queue id: {1}, skip refresh from db", ln.Id, lastQueueId);
                                return;
                            }
                            else if (lastLineQueueId.Value < lastStoredLineQueueId.Value)
                            {
                                log.ErrorFormat("Line {0}, last external data queue id ({1}) is older than last database queue id ({2})", ln.Id, lastLineQueueId.Value, lastStoredLineQueueId.Value);
                            }
                        }


                        SetItemList(biz.Id, ln.Id, items);

                        log.DebugFormat("Finished cache task for business {0}, line {1}", biz.Id, ln.Id);
                    }));
                }
            }
        }
Exemplo n.º 2
0
        public void Execute(IJobExecutionContext context)
        {
            log.Debug("executing job: cache refresh...");

            try
            {
                var businessList = Database.GetBusinessLineList(null);

                List <Task> tasks = new List <Task>();

                foreach (var biz in businessList)
                {
                    foreach (var ln in biz.Line)
                    {
                        log.DebugFormat("starting cache task for business {0}, line {1}...", biz.Id, ln.Id);

                        Task t = new Task(() =>
                        {
                            var items = ExternalData.Fetch(biz.Id, ln.Id, 2);
                            CacheEngine.Instance.SetItemList(biz.Id, ln.Id, items);
                            log.DebugFormat("finished cache task for business {0}, line {1}", biz.Id, ln.Id);
                        });

                        tasks.Add(t);
                    }
                }

                Task ending = Task.WhenAll(tasks);
                try
                {
                    ending.Wait();
                    CacheEngine.Instance.State = CacheEngineState.Ready;
                }
                catch (Exception ex)
                {
                    log.ErrorFormat("cache tasks failed: {0}", ex);
                }

                if (ending.Status == TaskStatus.RanToCompletion)
                {
                    CacheEngine.Instance.State = CacheEngineState.Ready;
                }
                else if (ending.Status == TaskStatus.Faulted)
                {
                    CacheEngine.Instance.State = CacheEngineState.Invalid;
                }
            }
            catch (Exception ex)
            {
                log.ErrorFormat("job failed: {0}", ex);
            }

            log.Debug("executed job: cache refresh");
        }
Exemplo n.º 3
0
        private static void DoBackup()
        {
            log.Debug("executing task: database backup from web service...");

            try
            {
                var dbCfgItems = Database.GetConfigItems();
                var backupDays = int.Parse(dbCfgItems.First(i => i.Name.Equals("BackupDays")).Value);

                Database.DoBackup((lineId, businessId) =>
                {
                    var dataItems   = ExternalData.Fetch(businessId, lineId, backupDays);
                    var dbDataItems = Mapper.Map <List <AdapterDb.DataItem> >(dataItems);
                    return(dbDataItems);
                });
            }
            catch (Exception ex)
            {
                log.ErrorFormat("job failed: {0}", ex);
            }
        }