Пример #1
0
        public void StartCreatingFeeds(Func <FeedFileCreationContext, bool> createFeed, string secondFileName = null)
        {
            try
            {
                using (var scope = new DbContextScope(autoDetectChanges: false, validateOnSave: false, forceNoTracking: true))
                {
                    _cachedPathes     = null;
                    _cachedCategories = null;

                    var storeService = _ctx.Resolve <IStoreService>();
                    var stores       = new List <Store>();

                    if (BaseSettings.StoreId != 0)
                    {
                        var storeById = storeService.GetStoreById(BaseSettings.StoreId);
                        if (storeById != null)
                        {
                            stores.Add(storeById);
                        }
                    }

                    if (stores.Count == 0)
                    {
                        stores.AddRange(storeService.GetAllStores());
                    }

                    var context = new FeedFileCreationContext()
                    {
                        StoreCount = stores.Count,
                        Progress   = new Progress <FeedFileCreationProgress>(x =>
                        {
                            AsyncState.Current.Set(x, SystemName);
                        })
                    };

                    foreach (var store in stores)
                    {
                        var feedFile = GetFeedFileByStore(store, secondFileName);
                        if (feedFile != null)
                        {
                            AppPath.Delete(feedFile.FileTempPath);

                            if (secondFileName.HasValue())
                            {
                                AppPath.Delete(feedFile.CustomProperties["SecondFileTempPath"] as string);
                            }

                            using (var stream = new FileStream(feedFile.FileTempPath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
                                using (var logger = new TraceLogger(feedFile.LogPath))
                                {
                                    context.Stream      = stream;
                                    context.Logger      = logger;
                                    context.Store       = store;
                                    context.FeedFileUrl = feedFile.FileUrl;

                                    if (secondFileName.HasValue())
                                    {
                                        context.SecondFilePath = feedFile.CustomProperties["SecondFileTempPath"] as string;
                                    }

                                    if (!createFeed(context))
                                    {
                                        break;
                                    }
                                }

                            AppPath.Copy(feedFile.FileTempPath, feedFile.FilePath);

                            if (secondFileName.HasValue())
                            {
                                AppPath.Copy(context.SecondFilePath, feedFile.CustomProperties["SecondFilePath"] as string);
                            }
                        }
                    }
                }
            }
            finally
            {
                AsyncState.Current.Remove <FeedFileCreationProgress>(SystemName);
            }
        }