示例#1
0
        public int SaveGroupProcessingHistoryItem(VkGroupProcessingHistoryItem item)
        {
            if (!item.IsTransient())
            {
                return(0);
            }

            using (IDataGateway dataGateway = this.dataGatewayProvider.GetDataGateway())
            {
                item.Id = dataGateway.Connection.Query <int>(@"insert into vkgroupprocessinghistory(vkgroupid, feedtype, fetchingdate, fetchingserver, fetchingprocess, processingdate, processingserver, processingprocess) values (@VkGroupId, @FeedType, @FetchingDate, @FetchingServer, @FetchingProcess, @ProcessingDate, @ProcessingServer, @ProcessingProcess) RETURNING id", item).First();

                var state = dataGateway.Connection.Query <VkGroupProcessingState>("select * from vkgroupprocessingstate where vkgroupid = @vkGroupId and feedtype = @feedType", new { vkGroupId = item.VkGroupId, feedType = (int)item.FeedType }).FirstOrDefault() ?? new VkGroupProcessingState();
                state.FeedType          = item.FeedType;
                state.VkGroupId         = item.VkGroupId;
                state.FetchingDate      = item.FetchingDate;
                state.FetchingProcess   = item.FetchingProcess;
                state.FetchingServer    = item.FetchingServer;
                state.ProcessingDate    = item.ProcessingDate;
                state.ProcessingProcess = item.ProcessingProcess;
                state.ProcessingServer  = item.ProcessingServer;
                state.Version++;

                if (state.IsTransient())
                {
                    item.Id = dataGateway.Connection.Query <int>(@"insert into vkgroupprocessingstate(version, vkgroupid, feedtype, fetchingdate, fetchingserver, fetchingprocess, processingdate, processingserver, processingprocess) values (@Version, @VkGroupId, @FeedType, @FetchingDate, @FetchingServer, @FetchingProcess, @ProcessingDate, @ProcessingServer, @ProcessingProcess) RETURNING id", state).First();
                }
                else
                {
                    dataGateway.Connection.Execute(@"update vkgroupprocessingstate set version = @Version, vkgroupid = @VkGroupId, feedtype = @FeedType, fetchingdate = @FetchingDate, fetchingserver = @FetchingServer, fetchingprocess = @FetchingProcess, processingdate = @ProcessingDate, processingserver = @ProcessingServer, processingprocess = @ProcessingProcess where id = @Id", state);
                }

                return(state.Version);
            }
        }
        private void ProcessTerminator(DataFeed dataFeed)
        {
            VkGroupProcessingHistoryItem item = new VkGroupProcessingHistoryItem
            {
                FeedType          = dataFeed.Type,
                VkGroupId         = dataFeed.VkGroupId,
                FetchingDate      = dataFeed.SendingDate,
                FetchingServer    = dataFeed.FetchingServer,
                FetchingProcess   = dataFeed.FetchingProcess,
                ProcessingDate    = this.dateTimeHelper.GetDateTimeNow(),
                ProcessingServer  = this.webUtilities.GetServerName(),
                ProcessingProcess = this.webUtilities.GetApplicationPoolName()
            };

            using (var transaction = this.transactionProvider.CreateTransaction().Begin())
            {
                try
                {
                    int version = this.groupRepository.SaveGroupProcessingHistoryItem(item);
                    transaction.Commit();

                    IFeedProcessor processor = this.processorFactory.Create(dataFeed.Type);
                    processor.ProcessTerminator(dataFeed.VkGroupId, version);
                }
                catch (Exception exc)
                {
                    transaction.Rollback();
                    this.log.ErrorFormat("Terminator feed with VkGroupId={0} ({1}) is failed to be processed. Reason: {2}", dataFeed.VkGroupId, dataFeed.Type, exc.ToString());
                    throw;
                }
                finally
                {
                    dataFeed.MarkAsCompleted();
                }
            }
        }